Simple Machines Community Forum

General Community => Scripting Help => Topic started by: CoolMan on August 20, 2004, 06:31:29 PM

Title: PHP help needed :)
Post by: CoolMan on August 20, 2004, 06:31:29 PM
Hey fellas, thanks for reading this :)

I got this script:

<?php
include("db_conn.php"); <- all DB connection info
$sql
= "SELECT * FROM users";
$result = mysql_query($sql);
echo
'<table>';
while(
$row = mysql_fetch_object($result)) {
        echo "<tr><td>$row->name<td>$row->email<td>$row->status</tr>";
}
echo
'</table>';
mysql_free_result($result);
?>


Now i got over 100 or so rows in the users table, and I would like that each time it enters another row in the "while" it will change the color in the <tr>... like in the posts in this forum, first post gets X color and the next  one gets Y, then back to X and so on..
How did you guys do that?
Title: Re: PHP help needed :)
Post by: [Unknown] on August 20, 2004, 07:11:02 PM

<?php

// Load the database connection information.
include("db_conn.php");

$result = mysql_query('
  SELECT *
  FROM users'
);

echo
'
<table>'
;
$alternate = true;
while (
$row = mysql_fetch_object($result))
{
  echo
'
  <tr style="background-color: '
, $alternate ? 'red' : 'blue', '">
     <td>'
, $row->name, '</td>
     <td>'
, $row->email, '</td>
     <td>'
, $row->status, '</td>
  </tr>'
;

 
$alternate != $alternate;
}
echo
'
</table>'
;

mysql_free_result($result);

?>


-[Unknown]
Title: Re: PHP help needed :)
Post by: CoolMan on August 20, 2004, 07:44:18 PM
Thanks buddy :)
Title: Re: PHP help needed :)
Post by: CoolMan on August 20, 2004, 07:50:21 PM
Oh hold on, its just putting it as 'Red' all the time... i put the Alternate = true before the while then the Alternate != Alternate in the while and its not changing....
Title: Re: PHP help needed :)
Post by: CoolMan on August 20, 2004, 07:52:59 PM
and i fixed it using:


if($alternate == "true") {
$alternate = false;
} else {
$alternate = true;
}


instead of the 2nd one:

$alternate != $alternate;
Title: Re: PHP help needed :)
Post by: [Unknown] on August 20, 2004, 10:40:32 PM
You should definitely not put "true" in quotes..

-[Unknown]
Title: Re: PHP help needed :)
Post by: whiterabbit on August 21, 2004, 01:21:39 AM
Ahh.. well, I believe just change
$alternate != $alternate;

to

$alternate = !$alternate;

should work .... or isn't it  :-
Title: Re: PHP help needed :)
Post by: [Unknown] on August 21, 2004, 01:27:12 AM
Oops, see, that was the typo... good eye.

-[Unknown]