Solved

PHP IF or ELSEIF Statements

Posted on 2013-01-14
14
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
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:
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…

691 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