PHP IF Statement Help

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]

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

[/php]

need more info. not sure what you are trying to do. are you just wanting to pull a random sql record up? or is there a reason that you have set a value of 0 when it has been viewed

There are only 10 records in my DB. When a record is displayed on the form, it is someones name, & they are assigned something at their work place. Once employee A is assigned something their value gets changed to a 0 so the SQL won’t pull it again. Once the last name is pulled & displayed & the value is changed to 0, I need all of the 10 to be changed back to a 1 so they’ll be back in rotation for assignments…How could I add the COUNT statement to this existing SQL so that it would count the 0’s & once it see’s 10 it would reset all of them back to 1 in the active field? Thanks for any help provided.

Here is what I have & it works great except once the last active record is changed to 0, it does not change them all back to 1…please help!

[php]

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

[/php]

I was able to fix it by editing my code by taking the ELSE statement out! Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service