Solved

PHP IF or ELSEIF Statements

Posted on 2013-01-14
14
246 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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 …

821 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