aviatorisu Posted September 8, 2006 Share Posted September 8, 2006 I know I have seen this on the forum before, but I can't seem to find it in the search function because I don't know exactly what to enter.I need to find how to alternate background colors when showing all the results of a query if that makes sense.Thanks,~Z~ Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 8, 2006 Share Posted September 8, 2006 Here's one way:[code]<?php$color[0] = 'red';$color[1] = 'green';$bkgr = '';while($rw=mysql_fetch_assoc($rs)) { $bkgr = ($bkgr == $color[0])?$color[1]:$color[0]; echo '<span style="background-color:' . $bkgr . '">' . $rw['var'] . '</span><br>';}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted September 8, 2006 Author Share Posted September 8, 2006 I kinda see how that should work, but my knowledge is limited thus far. Can anyone explain it to a dummy like me? :)I just need to alternate from a white background to a light gray background...over and over and over...lol.~Z~ Quote Link to comment Share on other sites More sharing options...
radalin Posted September 9, 2006 Share Posted September 9, 2006 he just defined an array with two elements as the colors of your rows with[code=php:0]$color[0] = 'red';$color[1] = 'green';[/code]then he defined another empty string variable $bkgr. After that he started to pull the data from database. While echoing he set the background-color of the span with the style property as the bgcolor it should be.the meaning[code=php:0]$bkgr = ($bkgr == $color[0])?$color[1]:$color[0];[/code]is equivalent to:if ( $bkgr == $color[0] )$bkgr = $color[1];else$bkgr = $color[0]; Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted September 9, 2006 Author Share Posted September 9, 2006 Okay...still kinda getting it.If my code looks like this:[code]<?php$c = @$_GET['c'] ;$a = @$_GET['a'] ;$db = MYSQL_CONNECT(); mysql_select_db(); $query = "select * from aircraft_specs where (($c = 1) AND ($a = 1))ORDER BY manufacturer ASC, model ASC, subtype ASC"; $result3 = mysql_query($query,$db); while ($myrow3 = mysql_fetch_array($result3)) { $id_specs = $myrow3["id"]; $manufacturer_specs = $myrow3["manufacturer"]; $model_specs = $myrow3["model"]; $subtype_specs = $myrow3["subtype"]; ?><FONT FACE="verdana" SIZE="2" COLOR="black"><A HREF='http://www.aviationhistoryonline.com/aircraft/index.php?id=<?php echo "$id_specs";?>'><?php echo "$manufacturer_specs";?> <?php echo "$model_specs";?><?php echo "$subtype_specs";?></A><BR></FONT> <?php}?>[/code]Where would all of that fit in?~Z~ Quote Link to comment Share on other sites More sharing options...
radalin Posted September 9, 2006 Share Posted September 9, 2006 your code should change to something like:[code]<table bgcolor="<?php echo $bkgr ;?>"><tr><td><FONT FACE="verdana" SIZE="2" COLOR="black"><A HREF='http://www.aviationhistoryonline.com/aircraft/index.php?id=<?php echo "$id_specs";?>'><?php echo "$manufacturer_specs";?> <?php echo "$model_specs";?><?php echo "$subtype_specs";?></A></FONT></td></tr></table>[/code]you should check css by the way Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted September 9, 2006 Author Share Posted September 9, 2006 [quote author=radalin link=topic=107421.msg431017#msg431017 date=1157764190]you should check css by the way[/quote]Tell me about it...I've thumbed through it, but thought it more important to pick up more PHP and SQL first. From what I've picked up so far, it does look helpful.Where does the rest of the stuff go?!Thanks,~Z~ Quote Link to comment Share on other sites More sharing options...
Barand Posted September 9, 2006 Share Posted September 9, 2006 Try[code]<?php$c = @$_GET['c'] ;$a = @$_GET['a'] ;$db = MYSQL_CONNECT(); mysql_select_db(); $bg1 = '#FFFFFF'; $bg2 = '#E0E0E0'; echo '<table>'; $query = "select * from aircraft_specs where (($c = 1) AND ($a = 1))ORDER BY manufacturer ASC, model ASC, subtype ASC"; $result3 = mysql_query($query,$db); while ($myrow3 = mysql_fetch_array($result3)) { $id_specs = $myrow3["id"]; $manufacturer_specs = $myrow3["manufacturer"]; $model_specs = $myrow3["model"]; $subtype_specs = $myrow3["subtype"]; $bgcol = ($bgcol==$bg2)? $bg1: $bg2; ?><tr><td style="background-color: <?php echo $bgcol ?>"><FONT FACE="verdana" SIZE="2" COLOR="black"><A HREF='http://www.aviationhistoryonline.com/aircraft/index.php?id=<?php echo "$id_specs";?>'><?php echo "$manufacturer_specs";?> <?php echo "$model_specs";?><?php echo "$subtype_specs";?></A><BR></FONT></td></tr> <?php}?> </table>[/code] Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted September 9, 2006 Author Share Posted September 9, 2006 To be honest, I still don't fully understand WHY this works, but I will give it thought still. Thank you very much to everyone for their help.~Z~ Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 9, 2006 Share Posted September 9, 2006 Which part of the code don't you understand?Ken Quote Link to comment Share on other sites More sharing options...
aviatorisu Posted September 9, 2006 Author Share Posted September 9, 2006 Just the part that switches the color. I get everything else, just a little fuzzy about why it does that.~Z~ Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 9, 2006 Share Posted September 9, 2006 The code[code]<?php $bgcol = ($bgcol==$bg2)? $bg1: $bg2; ?>[/code]uses the ternary operator. The manual (http://www.php.net/manual/en/language.operators.comparison.php) states[quote] The expression [i](expr1) ? (expr2) : (expr3)[/i] evaluates to [i]expr2[/i] if [i]expr1[/i] evaluates to [b]TRUE[/b], and [i]expr3[/i] if [i]expr1[/i] evaluates to [b]FALSE[/b].[/quote]It is a shorthand way of saying:[code]<?phpif ($bgcol == $bg2) { $bgcol = $bg1;} else { $bgcol = $bg2;}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Barand Posted September 9, 2006 Share Posted September 9, 2006 or, in the context of the script,[pre]if the last bg color was grey set it to whiteotherwise set it to grey[/pre] 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.