ren Posted May 1, 2008 Share Posted May 1, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/ Share on other sites More sharing options...
Rohan Shenoy Posted May 1, 2008 Share Posted May 1, 2008 http://www.w3hobbyist.com/view.php?id=16 Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531132 Share on other sites More sharing options...
phorman Posted May 1, 2008 Share Posted May 1, 2008 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531139 Share on other sites More sharing options...
ren Posted May 1, 2008 Author Share Posted May 1, 2008 Cheers phorman! That's exactly what I was looking for. -ren Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531154 Share on other sites More sharing options...
ren Posted May 1, 2008 Author Share Posted May 1, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531162 Share on other sites More sharing options...
rhodesa Posted May 1, 2008 Share Posted May 1, 2008 Just a note...the "if short hand" you are using: ((++$rct)%2==0) ? '#0000FF' : '#C0C0C0' can be inside a print, you just have to do it a little different: print( "<tr bgcolor =\"".(((++$rct)%2 == 0) ? '#0000FF' : '#C0C0C0')."\">\n"); Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531167 Share on other sites More sharing options...
phorman Posted May 2, 2008 Share Posted May 2, 2008 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 } ?> Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531478 Share on other sites More sharing options...
ren Posted May 2, 2008 Author Share Posted May 2, 2008 thanks again phorman. it's working just as I want -ren Link to comment https://forums.phpfreaks.com/topic/103736-echo-inside-of-print-alternate-row-colors/#findComment-531731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.