Jump to content

[SOLVED] background alternating colour code problem


d3vilr3d

Recommended Posts

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>";
					}
	}

Link to comment
Share on other sites

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>";
   }
}

?>

Link to comment
Share on other sites

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~

 

Link to comment
Share on other sites

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";
}

?>

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.