Jump to content

Archived

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

aviatorisu

Looping help...I think

Recommended Posts

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~

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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~

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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~

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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~

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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~

Share this post


Link to post
Share on other sites
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]<?php
if ($bgcol == $bg2) {
    $bgcol = $bg1;
} else {
    $bgcol = $bg2;
}?>[/code]

Ken

Share this post


Link to post
Share on other sites
or, in the context of the script,
[pre]
if the last bg color was grey
  set it to white
otherwise
  set it to grey[/pre]

Share this post


Link to post
Share on other sites

×

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.