Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

PHP IF or ELSEIF Statements

The code below works great. It pulls data from a MySQL database & displays it in a form. The DB consist of a table named clerk_names & the fields are clerk_id, names, and active. There will always be only 10 entries in this DB. The code below pulls the name & marks the active field from a 1 to a 0 for the particular random name it pulls. Like I said this code works great except I need to add an IF or ELSE or ELSEIF statement so if all the active fields are set to 0, it will mark all 10 of them back to a 1. I've got a script that already will mark them all back to a 1 & am unsure if I need to just add this script to the current code via an IF statement or call the script. It would make it easier if I could add the php code I already have to the code below...is this possible?

<?php
$mysqli = new mysqli('localhost', 'uname', 'password', 'flow'); 
$sql = "SELECT names, active, clerk_id FROM clerk_names WHERE active = '1' ORDER BY RAND() LIMIT 1";
$res = $mysqli->query($sql); 
$row = $res->fetch_row(); 
$randomName = $row[0];  
$res->free(); 
$sql = "UPDATE clerk_names SET active = 0 WHERE clerk_id = " . $row[2] . " " ;
$res = $mysqli->query($sql); 

} 

 else
{ 

}

mysql_close(); // Close the database connection.
?> 

Open in new window

Avatar of joylene6
joylene6
Flag of United States of America image

IIf([active]=0,"1",[active]

This states if the active field =0 then place a 1 in the field, if not 0, then whatever is in the active field will show.
IIf([active]=0,"1",[active])

forgort ) at the end
Avatar of wantabe2

ASKER

What line would that go on in the code I posted above...I get errors
Also, I only need the ACTIVE field in the DB to be set to a 1 only if all 10 entries are set to a 0. So basically, the IF statement woulod say something like, "IF all the ACTIVE fields are 0, I'm going to set all 10 back to 1"

Is this possible?
Still having issues with this..... :(
Could I use COUNT to get the count of the rows which are not active. If its zero, run the command to update them all active?? Can I get some type of example please. Thanks
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

Here is a good example of the if then..

basically the statement is IF(condition, true code, false code)

Can really be on any line.....
Try with this

<?php
$mysqli = new mysqli('localhost', 'uname', 'password', 'flow'); 
$sql = "SELECT names, active, clerk_id FROM clerk_names WHERE active = '1' ORDER BY RAND() LIMIT 1";
$res = $mysqli->query($sql); 
$row = $res->fetch_row(); 
$randomName = $row[0];  
$res->free(); 
$sql = "UPDATE clerk_names SET active = 0 WHERE clerk_id = " . $row[2] . " " ;
$res = $mysqli->query($sql); 

} 

 else
{ 
 // why else ?
 $sql = "SELECT count(clerk_id) FROM clerk_names WHERE active = 0";
 $res = $mysqli->query($sql); 
 $row = $res->fetch_row(); 
 $numzeros = $row[0];  
 $res->free(); 

 if($numzeros == 10){
   $sql = "UPDATE clerk_names SET active = 1" ;
   $res = $mysqli->query($sql);
 }
}

Open in new window


mysql_close(); // Close the database connection.
?>
oh I think I got it....let me test it
Well, I thought I had it but don't (with your help).. This code works great except it is not resetting the active field back to 1 for all 10 fields once they are all randomly displayed.

<?php
$mysqli = new mysqli('localhost', 'uname', 'pword', 'flow');
$sql = "SELECT names, active, clerk_id FROM clerk_names WHERE active = '1' ORDER BY RAND() LIMIT 1";
$res = $mysqli->query($sql);
$row = $res->fetch_row();
$randomName = $row[0]; 
$res->free();
$sql = "UPDATE clerk_names SET active = 0 WHERE clerk_id = " . $row[2] . " " ;
$res = $mysqli->query($sql);
}
else
{
$sql = "SELECT count(clerk_id) FROM clerk_names WHERE active = 0";
$res = $mysqli->query($sql);
$row = $res->fetch_row();
$numzeros = $row[0]; 
$res->free();
if($numzeros == 10){
   $sql = "UPDATE clerk_names SET active = 1" ;
   $res = $mysqli->query($sql);
}
}
mysql_close(); // Close the database connection.
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Correct,
Once a record is displayed the code sets that ACTIVE field to 0. And once all 10 records show 0's I'd like for the code to reset all 10 of the ACTIVE fields back to a 1. I've attached a snip it of the DB.

Thanks again for your assistance.
clerks.JPG
That is what the last posted code should do.
I think yours did not work because of the else statement (where does it come from ? )
That was it! The ELSE statement was causing it to fail. I edited it & it works great now. Thanks again for your help & have a good day!