d3vilr3d Posted October 8, 2007 Share Posted October 8, 2007 Hi, Im having problem making my background alternate between colors. Following is my code, the result alternates 2 colors ok, but prints each entry from my database 30 times. Please help me fix, thanks soo much. $color1 = "#CCFFCC"; $color2 = "#BFD8BC"; $row_count = 0; $sql_events = mysql_query("SELECT * FROM ad ORDER BY id ASC") or die (mysql_error()); if (!preg_match("/[a-z]/i",$price)&&!preg_match("/\\$/",$price)) $price = "$".$price; while ($row = mysql_fetch_array($sql_events)) { $row_color = ($row_count % 2) ? $color1 : $color2; echo "<TR bgcolor='$row_color'>\n"; $row_count++; echo "<TD><A href='$ad_url'>".$ads[$i][title]."</A>"; if (strlen($ads[$i][city])<1) { echo ""; }else{ echo "(".$ads[$i][city].")";} if (strlen($ads[$i][price])<1) { echo ""; }else{ echo "<SPAN class='adlist_price'>".substr($price,0,10)."</SPAN> ";} if (intval($ads[$i][num_pics])>0) echo "<IMG src='images/picture.gif' border='0' style='vertical-align:middle'>"; echo "<br>"; echo "$content"; echo "</TD>"; echo "</TR>"; } } Quote Link to comment Share on other sites More sharing options...
AV1611 Posted October 8, 2007 Share Posted October 8, 2007 I use this approach: $color1=somecolor $color2=someothercolor $c=1; while...{ if($c==1){ $cssvar=$color1; $c=2; //use the $cssvar } elseif($c==2){ $cssvar=$color2; $c=1; //use the $cssvar } } Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2007 Share Posted October 8, 2007 Is that code inside a loop which executes 30 times? What is in $content? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 8, 2007 Share Posted October 8, 2007 I think the code below is a little more eficcient in selecting the row color. However, the problem you describe is not due to that. In fact, I am a littel confused by your code alltogether. You have a while loop which sets $row to a record from the result set. But nowhere int he while loop do you use that data! Instead you are using an array $ads which isn't set anywhere in that code. I think that is the root of your problem <?php //$color1 = "#CCFFCC"; //$color2 = "#BFD8BC"; //$row_count = 0; $sql_events = mysql_query("SELECT * FROM ad ORDER BY id ASC") or die (mysql_error()); if (!preg_match("/[a-z]/i",$price)&&!preg_match("/\\$/",$price)) $price = "$".$price; while ($row = mysql_fetch_array($sql_events)) { $row_color = ($row_color!='#CCFFCC') ? '#CCFFCC' : '#BFD8BC'; echo "<TR bgcolor='$row_color'>\n"; //$row_count++; echo "<TD><A href='$ad_url'>".$ads[$i][title]."</A>"; if (strlen($ads[$i][city])<1) { echo ""; }else{ echo "(".$ads[$i][city].")";} if (strlen($ads[$i][price])<1) { echo ""; }else{ echo "<SPAN class='adlist_price'>".substr($price,0,10)."</SPAN> "; } if (intval($ads[$i][num_pics])>0) { echo "<IMG src='images/picture.gif' border='0' style='vertical-align:middle'>"; } echo "<br>"; echo "$content"; echo "</TD>"; echo "</TR>"; } } ?> Quote Link to comment Share on other sites More sharing options...
d3vilr3d Posted October 9, 2007 Author Share Posted October 9, 2007 well, im fixing the php file from the previous programmer. Here is the whole file: <?PHP if (!defined("MC_KEY")) die("Security Error."); include_once('includes/database.php'); require_once('config.php'); if ($_REQUEST[action]=="showads") { ?> <DIV id="ads_search_form"> <?PHP include('search_form.php'); ?> </DIV> <DIV id="ads_list"> <?PHP $sql = ""; if (isset($_REQUEST[regionid]) && isset($_REQUEST[subcategoryid])) { $url_params = "regionid=".intval($_REQUEST[regionid])."&subcategoryid=".intval($_REQUEST[subcategoryid]); } else { // SCRIPT TO BAIL OUT!!! //echo "<SCRIPT></SCRIPT>"; } $ads = $info->getAds($_REQUEST[regionid],$_REQUEST[subcategoryid]); ?> <TABLE id="ad_table"> <?PHP $cur_date = ""; for ($i = 0; $i < sizeof($ads); $i++) { $ad_url = "?action=display&adid=".$ads[$i][id]."&$url_params"; if ($cur_date!=date("D M d",$ads[$i][epoch_date])) { $cur_date = date("D M d",$ads[$i][epoch_date]); echo "<TR><TD colspan='3'> </TD></TR>"; /*echo "<TR class='ad_date_separator'><TD colspan='3'>".date("D M d",$ads[$i][epoch_date])."</TD></TR>";*/ } $price = $ads[$i][price]; $content = $ads[$i][content]; if (!preg_match("/[a-z]/i",$price)&&!preg_match("/\\$/",$price)) $price = "$".$price; echo "<TR>\n"; echo "<TD><A href='$ad_url'>".$ads[$i][title]."</A>"; if (strlen($ads[$i][city])<1) { echo ""; }else{ echo "(".$ads[$i][city].")";} if (strlen($ads[$i][price])<1) { echo ""; }else{ echo "<SPAN class='adlist_price'>".substr($price,0,10)."</SPAN> ";} if (intval($ads[$i][num_pics])>0) echo "<IMG src='images/picture.gif' border='0' style='vertical-align:middle'>"; echo "<br>"; echo "$content"; echo "</TD>"; echo "</TR>"; } ?> </TABLE> </DIV> <?PHP } ?> Please help me insert the alternating color code. The code i posted earlier, either it doesnt return the 2nd color, or it loops 30 times. So confused. thanks~ Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2007 Share Posted October 9, 2007 There are a lot of bad standards in that code. Try this: <?php if (!defined("MC_KEY")) die("Security Error."); include_once('includes/database.php'); require_once('config.php'); if ($_REQUEST[action]=="showads") { echo "<div id=\"ads_search_form\">\n"; include('search_form.php'); echo "</div>\n"; echo "<div id=\"ads_list\">\n"; if (isset($_REQUEST[regionid]) && isset($_REQUEST[subcategoryid])) { $url_params = "regionid=".intval($_REQUEST[regionid])."&subcategoryid=".intval($_REQUEST[subcategoryid]); } else { // SCRIPT TO BAIL OUT!!! //echo "<script></script>"; } $ads = $info->getAds($_REQUEST[regionid],$_REQUEST[subcategoryid]); echo "<table id=\"ad_table\">\n"; $cur_date = ""; foreach ($ads as $adRecord) { $ad_url = "?action=display&adid=".$adRecord[id]."&$url_params"; if ($cur_date!=date("D M d",$adRecord[epoch_date])) { $cur_date = date("D M d",$adRecord[epoch_date]); echo "<tr><td colspan='3'> </td></tr>"; /*echo "<tr class='ad_date_separator'><td colspan='3'>".date("D M d",$adRecord[epoch_date])."</td></tr>";*/ } $price = $adRecord[price]; if (!preg_match("/[a-z]/i",$price)&&!preg_match("/\\$/",$price)) { $price = "$".$price; } $row_color = ($row_color!='#ccffcc') ? '#ccffcc' : '#bfd8bc'; echo "<tr style=\"background-color:$row_color;\">\n"; echo " <td>\n"; echo " <a href='$ad_url'>".$adRecord[title]."</a>\n"; if (strlen($adRecord[city])) { echo " (".$adRecord[city].")\n"; } if (strlen($adRecord[price])) { echo " <span class='adlist_price'>".substr($price,0,10)."</span>\n"; } if (intval($adRecord[num_pics])) { echo " <img src='images/picture.gif' border='0' style='vertical-align:middle'>\n"; } echo " <br>$adRecord[content]\n"; echo " </td>"; echo "</tr>"; } echo "</table>\n"; echo "</div>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
d3vilr3d Posted October 9, 2007 Author Share Posted October 9, 2007 wow, works like a charm. thanks so much! I am studying your coding Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2007 Share Posted October 9, 2007 Don't forget to make the thread as solved. But post back any questins if you have them. Although I made a lot of changes for consistency and standards adhereance, this was all you needed to add to the original code: $row_color = ($row_color!='#ccffcc') ? '#ccffcc' : '#bfd8bc'; echo "<tr style=\"background-color:$row_color;\">\n"; Quote Link to comment Share on other sites More sharing options...
d3vilr3d Posted October 9, 2007 Author Share Posted October 9, 2007 Don't forget to make the thread as solved. But post back any questins if you have them. Although I made a lot of changes for consistency and standards adhereance, this was all you needed to add to the original code: $row_color = ($row_color!='#ccffcc') ? '#ccffcc' : '#bfd8bc'; echo "<tr style=\"background-color:$row_color;\">\n"; ya, didnt think couldda been that easy hehe. thanks so much man Quote Link to comment Share on other sites More sharing options...
d3vilr3d Posted October 10, 2007 Author Share Posted October 10, 2007 Now Im trying to build a page navigation for this page, limiting 30 entries showing per page, with Previous 1 2 3 4 5... Next Last page links at bottom of the page. Anyone know a tutorial or code example I can study up on? Quote Link to comment Share on other sites More sharing options...
supanoob Posted October 10, 2007 Share Posted October 10, 2007 check out the pagination tut on this site. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.