Page 1 of 1

Echo image

Posted: Thu Nov 04, 2010 9:49 am
by ewannnn
Hi, I need some help with some PHP code please.
I have a database with a table 'users' with an attribute 'status', set to default '0', and when a user logs in this changes to '1'.
I have then echoed the results of this table with something like so:

Code: Select all

$query=mysql_query("SELECT * FROM userTable;");	
	echo "<table>";
		//while loop for rows					       
           	while($rows=mysql_fetch_array($query)){
                echo "<tr>";			
                    echo "<td>" . $rows['username'] . "</td>" . "<td>" . $rows['status'] . "</td>";   
                echo "</tr>";							
         	}
...what I want to do is, if a user is online, echo a <td> with an image in it (rather than $rows['status']), say a green square, and a red square if they are offline.
To echo the image it's obviously (something like):

Code: Select all

echo '<img src="someLocation/thisImage.jpg">';
...what I am asking is how do i defferentiate between the '1' for online and the '0' for offline in the code?

Thanks in advance for any replies...

Re: Echo image

Posted: Thu Nov 04, 2010 10:25 am
by requinix
Look at that code, make a couple educated guesses, then learn to use the if control structure.

Re: Echo image

Posted: Thu Nov 04, 2010 1:35 pm
by ewannnn
Thanks, sorry I should have made it clearer - I tried that first...in the while loop a bit like this:

Code: Select all

while($rows=mysql_fetch_array($query)){
                if($rows['status']='1'){
                    echo "<tr>";                    
                        echo "<td>" . $rows['username'] . "</td>" . "<td>" . <img src GREEN> . "</td>";   
                    echo "</tr>";  
                } else {  
                    echo "<tr>";                    
                        echo "<td>" . $rows['username'] . "</td>" . "<td>" . <img src RED> . "</td>";   
                    echo "</tr>";  
                                    
Obviously the red/green image src is not the exact syntax, but you get the idea.

Any other suggestions I would be greatful, thanks.

Re: Echo image

Posted: Thu Nov 04, 2010 1:45 pm
by requinix

Code: Select all

if($rows['status']='1'){
One = means assignment, two ==s means comparison.

Re: Echo image

Posted: Thu Nov 04, 2010 1:58 pm
by ewannnn
Thanks I have actually tried both, none work...