Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

every other entry change colour 1

Status
Not open for further replies.

coolicus

Programmer
May 15, 2007
50
GB
I am trying to add a database to our website to make updating etc easier using php. I have managed to get it to output the results but they all blend in, can someone help in making the class change for every other entry please?

Here is what I have

Code:
<?php include("dbconn.php"); ?>

<?php
$query  = "SELECT * FROM properties";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "<div class='first'>{$row['name']}, {$row['address']}</div>" ;

}

//now grab the number of results for the field below
$query  = "SELECT COUNT(*) FROM properties";
$result = mysql_query($query);
$returned = mysql_fetch_array($result, MYSQL_ASSOC)

?>

There are <?php echo {$returned['*']} ?> results.

The above will make all of the results the same, to change the background color of every other result I can change the class to 'second'. As you can see I am also trying to grab the number of results, am I on the right track with that?

Thanks
 
Code:
$query  = "SELECT COUNT(*) [red]as c[/red] FROM properties";
$result = mysql_query($query);
$returned = mysql_fetch_array($result, MYSQL_ASSOC)

?>
There are <?php echo {$returned['[red]c[/red]']} ?> results.

Code:
$style = '';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $style = 'first' ? 'second' : 'first';
    echo "<div class='$style'>{$row['name']}, {$row['address']}</div>" ;
}
 
hey thanks, the count worked perfectly thanks I was quite close with that, the style one hasn`t though, every style is being outputted as 'second'
 
Code:
 $style = $style === 'first' ? 'second' : 'first';
sorry, under the weather with the noro virus at present...
 
hey thanks, I am beginning to understand this now, just looking into paging :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top