Jump to content


Photo

Looping help...I think


  • Please log in to reply
12 replies to this topic

#1 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 08 September 2006 - 11:35 PM

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~

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 September 2006 - 11:45 PM

Here's one way:
<?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>';
}
?>

Ken

#3 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 08 September 2006 - 11:56 PM

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~

#4 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 09 September 2006 - 12:44 AM

he just defined an array with two elements as the colors of your rows with
$color[0] = 'red';
$color[1] = 'green';
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
$bkgr = ($bkgr == $color[0])?$color[1]:$color[0];
is equivalent to:
if ( $bkgr == $color[0] )
$bkgr = $color[1];
else
$bkgr = $color[0];
Roy Simkes
Yet Another Parkyeri Developer

#5 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 September 2006 - 01:01 AM

Okay...still kinda getting it.

If my code looks like this:

<?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
}
?>

Where would all of that fit in?

~Z~

#6 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 09 September 2006 - 01:09 AM

your code should change to something like:

<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>

you should check css by the way
Roy Simkes
Yet Another Parkyeri Developer

#7 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 September 2006 - 01:45 AM

you should check css by the way


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~

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 09 September 2006 - 10:19 AM

Try

<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 September 2006 - 03:03 PM

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~

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 September 2006 - 03:16 PM

Which part of the code don't you understand?

Ken

#11 aviatorisu

aviatorisu
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 September 2006 - 04:25 PM

Just the part that switches the color.  I get everything else, just a little fuzzy about why it does that.

~Z~

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 September 2006 - 05:52 PM

The code
<?php $bgcol = ($bgcol==$bg2)? $bg1: $bg2; ?>
uses the ternary operator.  The manual (http://www.php.net/m....comparison.php) states

The expression (expr1) ? (expr2) : (expr3)  evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

It is a shorthand way of saying:
<?php
if ($bgcol == $bg2) {
    $bgcol = $bg1;
} else {
    $bgcol = $bg2;
}?>

Ken


#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 09 September 2006 - 06:00 PM

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]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users