Solved

PHP IF or ELSEIF Statements

Posted on 2013-01-14
14
241 Views
Last Modified: 2013-01-15
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

0
Comment
Question by:wantabe2
  • 8
  • 3
  • 3
14 Comments
 
LVL 1

Expert Comment

by:joylene6
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:joylene6
Comment Utility
IIf([active]=0,"1",[active])

forgort ) at the end
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
What line would that go on in the code I posted above...I get errors
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
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?
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
Still having issues with this..... :(
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
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
0
 
LVL 1

Expert Comment

by:joylene6
Comment Utility
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.....
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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.
?>
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
oh I think I got it....let me test it
0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
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

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
Comment Utility
If I understand, the code has to reset all the fields back to 1 when all of them (10 records) are 0, right ?

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

// i have removed the 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

0
 
LVL 15

Author Comment

by:wantabe2
Comment Utility
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
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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 ? )
0
 
LVL 15

Author Closing Comment

by:wantabe2
Comment Utility
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!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Read about why website design really matters in today's demanding market.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now