Jump to content

Database if else


forumnz

Recommended Posts

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

}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/38923-database-if-else/
Share on other sites

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 {

Link to comment
https://forums.phpfreaks.com/topic/38923-database-if-else/#findComment-187209
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/38923-database-if-else/#findComment-187228
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.