Jump to content

echo inside of print (alternate row colors)


ren

Recommended Posts

I saw a thread for print inside of echo - saying it can't be done - but I'm wondering about a echo inside of a print command.

 

I'm essentially trying to get alternating colors for each row and this is what I've found so far (for not being a PHP Freak yet, as it were).  Ultimately, I would also like a case senario where if a boolean is true (inside the MySQL db), the row is, say, GREEN, otherwise it's alternating colors.  So if you're bored and wanna come up with this for me, props to ya.  Otherwise, can anyone help with what I am doing wrong below?

 

Here's what I have for making the table portion

 

$rct = 0;  // counter for color rows in table

        //get each row
        while($dbRow = mysql_fetch_array($result, MYSQL_ASSOC))
        {
                print( "<tr bgcolor =\"<? php echo ((++$rct)%2==0) ? '#0000FF' : '#C0C0C0'?>\">\n");
                print("<td><a href=\"update.php?ref=" ."{$dbRow['id']}" ."\">{$dbRow['id']}</a></td>\n");
                print("<td><a href=\"" ."{$dbRow['link']}" ."\" target=\"_blank\">{$dbRow['ynum']}</a></td>\n");
                print("<td>{$dbRow['duedate']}</td>\n");
                print("<td>{$dbRow['emaildate']}</td>\n");
                print("<td>{$dbRow['notes']}</td>\n");

                print("</tr>");
        }
        
        echo '<br>';

        //end table     
        print("</table>\n");

 

I don't get an error; instead all the rows are BLACK - that's despite the HEX above being a shade of BLUE and a shade of GRAY.

 

Thanks for any help/advice/code

-ren

A much more easier to understand code to do the exact same thing:

 

<?php
$rct = 1;
while($dbRow = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if ($rct % 2 == 0)
            $bg_color =  "#0000FF";
        else
            $bg_color = "#C0C0C0";

        
        print( "\t<tr bgcolor =$bg_color>\n");
        print("\t\t<td><a href=\"update.php?ref=" ."{$dbRow['id']}" ."\">{$dbRow['id']}</a></td>\n");
        print("\t\t<td><a href=\"" ."{$dbRow['link']}" ."\" target=\"_blank\">{$dbRow['ynum']}</a></td>\n");
        print("\t\t<td>{$dbRow['duedate']}</td>\n");
        print("\t\t<td>{$dbRow['emaildate']}</td>\n");
        print("\t\t<td>{$dbRow['notes']}</td>\n");

        print("\t</tr>");
        $rct++;
}
?>

Actually, one more thing...

 

I was trying to add that second part - if the boolean is true, etc.. - and it's not allowing a '{' in the if statement

 

$rct = 1;
61         //get each row
62         while($dbRow = mysql_fetch_array($result, MYSQL_ASSOC))
63         {
64         if ({$dbRow['got_it']} == '1'){
65                 $bg_color == "#90EE90";
66         }else{
67         if ($rct % 2 == 0)
68             $bg_color =  "#F5F5F5";
69         else
70             $bg_color = "#F0F8FF";
71         }

 

So on line 64, I want to say if $dbRow['got_it'] is 1 then use this bg_color, otherwise do your thing with the alternating.  If you remove the '{' brackets, then it's just the second bg_color.

 

TIA,

-ren

I see two syntax error with the code.

 

1.  The only time you need to escape your array call is in a print statement.

 

So, the fix is to remove the {} brackets around the if statement.

 

2.  On line 65, when setting the variable to equal something, only use one = sign.  Otherwise you are comparing instead of setting the value equal.

 

The code should look like:

 

<?php
$rct = 1;
61         //get each row
62         while($dbRow = mysql_fetch_array($result, MYSQL_ASSOC))
63         {
64         if ($dbRow['got_it'] == '1'){
65                 $bg_color = "#90EE90";
66         }else{
67         if ($rct % 2 == 0)
68             $bg_color =  "#F5F5F5";
69         else
70             $bg_color = "#F0F8FF";
71         }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.