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!

TD colors in search results output

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.

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 -->
 
This should work: (It works fine for me....)

$counter = 0;

while ($row = mysql_fetch_assoc($result))
{
print &quot;<tr>\n&quot;;
if($counter % 2 == 0)
{$bg_color=&quot;gray&quot;;}
else
{$bg_color=&quot;white&quot;;}

print(&quot;<td colspan=\&quot;3\&quot; bgcolor=\&quot;&quot;.$bg_color.&quot;\&quot;>&quot;);
// I prefer : echo&quot;<td colspan=\&quot;3\&quot; bgcolor=\&quot;$bg_color\&quot;>&quot;;
printf(&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++;
}
Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top