Solved

PHP IF or ELSEIF Statements

Posted on 2013-01-14
14
243 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
ID: 38775755
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
ID: 38775760
IIf([active]=0,"1",[active])

forgort ) at the end
0
 
LVL 15

Author Comment

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

Author Comment

by:wantabe2
ID: 38775824
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
ID: 38776058
Still having issues with this..... :(
0
 
LVL 15

Author Comment

by:wantabe2
ID: 38776124
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
ID: 38776818
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38777178
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
ID: 38778051
oh I think I got it....let me test it
0
 
LVL 15

Author Comment

by:wantabe2
ID: 38778133
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
ID: 38778167
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
ID: 38778189
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
ID: 38778391
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
ID: 38778734
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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 …

943 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