forumnz Posted February 17, 2007 Share Posted February 17, 2007 Hello there, The code below is in a document that is meant to display one image if it == 0 and another if == 1. It works ok but the problem is that the first image in the code shows up no matter what and then the changing image next to it. What do I do? <?php $database=mysql_connect("localhost","name", "pass"); mysql_select_db("test",$database); $query="select * from members"; $result=mysql_query($query); while($img=mysql_fetch_assoc($result)){ $image_num=$img['stg1']; if($image_num==0){ echo"<img src='images/not_completed.gif'></img>"; }elseif($image_num==1){ echo"<img src='images/completed.gif'></img>"; }else { echo" sorry there is no images to show"; } } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 17, 2007 Share Posted February 17, 2007 um, do you have one result that comes to 0 and another that comes to 1? You're selecting more than one row... Quote Link to comment Share on other sites More sharing options...
forumnz Posted February 17, 2007 Author Share Posted February 17, 2007 I have a row named stg1 and it can either be set to 1 or 0. Make sense? Quote Link to comment Share on other sites More sharing options...
forumnz Posted February 17, 2007 Author Share Posted February 17, 2007 Anyone please? Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 Well i guess that you write either 0 or 1 in the database which would be strings so you also have to refer to strings, like this: <?php $database=mysql_connect("localhost","name", "pass"); mysql_select_db("test",$database); $query="select * from members"; $result=mysql_query($query); while($img=mysql_fetch_assoc($result)){ $image_num=$img['stg1']; if($image_num=="0"){ echo"<img src='images/not_completed.gif'></img>"; }elseif($image_num=="1"){ echo"<img src='images/completed.gif'></img>"; }else { echo" sorry there is no images to show"; } } ?> You also could use NULL then you would have to do something like this: if($image_num==NULL){ echo"<img src='images/not_completed.gif'></img>"; }elseif($image_num!=NULL){ echo"<img src='images/completed.gif'></img>"; }else { Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 17, 2007 Share Posted February 17, 2007 Yes, and if you have one entry in your DB which has a 0, and one which has a 1, you'll get the results you're getting. JJ: PHP uses weak typing so 0 == "0" == '0' == false. Quote Link to comment Share on other sites More sharing options...
forumnz Posted February 17, 2007 Author Share Posted February 17, 2007 It still comes up with multiple images? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 17, 2007 Share Posted February 17, 2007 What are the entries in your database? By the way, there is no </img> tag - img is one tag. <img src="image.gif" /> Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 aaah... allright thanks... didnt know that, nice to know ... Quote Link to comment Share on other sites More sharing options...
forumnz Posted February 17, 2007 Author Share Posted February 17, 2007 This is what I have - also, is there any way to get different results for each user based on their id? -- -- Table structure for table `members` -- CREATE TABLE `members` ( `id` int(4) NOT NULL auto_increment, `username` varchar(45) default NULL, `password` varchar(45) default NULL, `active` int(11) NOT NULL default '0', `stg1` int(1) NOT NULL default '1', `stg2` int(1) NOT NULL default '0', `stg3` int(1) NOT NULL default '0', `stg4` int(1) NOT NULL default '0', `stg5` int(1) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- -- Dumping data for table `members` -- INSERT INTO `members` VALUES (1, 'admin', 'pass', 0, 0, 0, 0, 0, 0); INSERT INTO `members` VALUES (2, 'admin1234', 'pass', 1, 1, 0, 1, 0, 1); INSERT INTO `members` VALUES (3, 'admin112', 'pass', 1, 1, 0, 0, 0, 0); Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 17, 2007 Share Posted February 17, 2007 See, as I said like 85 times, you have some with a 0 and some with a 1 - you're looping through all of them, so you get each one. Read the tutorial in the beginner section on mysql. Quote Link to comment Share on other sites More sharing options...
forumnz Posted February 17, 2007 Author Share Posted February 17, 2007 Ooo i got it. Ok thanks. I have one other problem - how can i get it to display only one users info based on their unique id? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 17, 2007 Share Posted February 17, 2007 use WHERE id=$id Like I said, check out the tutorial. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.