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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.