Jump to content

Looping help...I think


aviatorisu

Recommended Posts

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
Link to comment
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];
Link to comment
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~
Link to comment
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
Link to comment
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~
Link to comment
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]
Link to comment
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
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.