Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php results display issue 1

Status
Not open for further replies.

yerfdog7

Technical User
Sep 6, 2002
10
US
The code below works like a charm, but I have been having trouble getting each result row to alternate colors. For example I would like each odd numbered row to have a white BG color and each even result row a gray BG color.

I believe if memory serves that the spot that mentions gray & white are part of my attempt at accomplishing this, but it is not working.... something is wrong.

If someone could show me the coding for this and tell me where to place it in the current code, it would be greatly appreciated.

Thanks - vellum

code starts here:

<!-- php starts here -->
<?php
trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo &quot;<p align=center><b>You have not entered search details. Please go back and try again.</b></p>&quot;;
exit;
}
/* Connecting, selecting database */
@ $db = mysql_connect(&quot;hostsite&quot;, &quot;user&quot;, &quot;password&quot;)
or die(&quot;Could not connect&quot;);
mysql_select_db(&quot;churches&quot;) or die(&quot;Could not select database&quot;);

/* Performing SQL query */
$query = &quot;SELECT * FROM church WHERE &quot;.$searchtype.&quot; LIKE '%&quot;.$searchterm.&quot;%' order by 'name' asc&quot;;
$result = mysql_query($query) or die(&quot;Query failed&quot;);
$num_results = mysql_num_rows($result);
$counter = 0;
print &quot;<p><b>Your search produced &quot;.$num_results.&quot; matches.</b></p>&quot;;
/* Printing results in HTML */
print &quot;<table border=\&quot;0\&quot; cellspacing=\&quot;0\&quot; cellpadding=\&quot;4\&quot; width=\&quot;500\&quot; align=\&quot;center\&quot;>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td bgcolor=#0099FF colspan=\&quot;3\&quot; width=\&quot;50%\&quot; align=\&quot;center\&quot;><p><b>Organization</b></p></td>\n&quot;;
print &quot;<td bgcolor=#0099FF colspan=\&quot;5\&quot; align=\&quot;center\&quot;><p><b>Information</b></p></td>\n&quot;;
print &quot;</tr>\n&quot;;
while ($row = mysql_fetch_assoc($result)) {
print &quot;<tr>\n&quot;;
//print ($counter % 2 == 0)? &quot;gray&quot; : &quot;white&quot;;
printf(&quot;<td colspan=\&quot;3\&quot;><p><b>%s</b><br> %s<br> %s</td><td align=\&quot;right\&quot; colspan=\&quot;5\&quot;><p>%s<br> %s, %s. %s<br> %s</td>\n&quot;, $row['Name'], $row['Pastor'], $row['Denomination'], $row['Address'], $row['City'], $row['State'], $row['Zip'], $row['Phone']);
print &quot;\t</tr>\n&quot;;
$counter++;
}
print &quot;</table>\n&quot;;

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($db);
?>
<!-- php ends here -->
 
maybe im reading it wrong, but it looks like it prints out like this:
Code:
<tr>
gray
<td>
with gray or white in between the <tr> and <td>. delete the line above the one in question, and change this line to:
Code:
print ($counter % 2 == 0)?&quot;<tr bgcolor=\&quot;gray\&quot;>\n&quot;:&quot;<tr bgcolor=\&quot;white\&quot;>\n&quot;;
what we see depends mainly on what we're looking for.
--unknown
 
Might that help you,
before the while loop initialize $color = 0;

if($color == 0)
{
echo &quot;<tr align='center'>&quot;;
$color++;
}
else
{
echo &quot;<tr align='center' bgcolor='#E1E9FD'>&quot;;
$color = 0;
}

That's working in my case very efficiently.
 
bedrock that worked perfectly ... yabba-dabba-do

dotcomengg thanks but i used bedrocks

no longer,
vexed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top