Jump to content

PHP IF Statement Help Needed


wantabe3
 Share

Recommended Posts

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. Basically, once the 10 active field is marked to a 0 with the code below I just need something to mark all of them back to a 1 in the active field.........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);

 

}

 

mysql_close(); // Close the database connection.

?>

Link to comment
Share on other sites

I'm a newbe at PHP so can you help me out :) I just googled the count function & it does not make sense to me...

 

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/COUNT statement would say something like, "IF all the ACTIVE fields are 0, I'm going to set all 10 back to 1"

 

It sounds simple but I can't get it to save my life!!! Thanks for any help you can give.

Link to comment
Share on other sites

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. Can anyone else willing to help see where my problem is?

 

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

?>

Link to comment
Share on other sites

@Jessica

I don't need your ignorant comments nor does anyone else. If you have nothing else better to do I'd suggest you get a life & leave such comments or suggestions to moderators in which I plan to report you to. Please do not reply or respond to any more of my post. You are useless to me & from what I've read from others about you, you useless to them as well. Good Riddance!

Link to comment
Share on other sites

The only useless reply in this thread was your "I'm stumped".

 

Go ahead, report her to the moderators if it makes you feel better, but before that you should read the FAQ:

http://forums.phpfreaks.com/topic/273121-readme-php-resources-faqs/#entry1405506

 

As well as this bit:

You've come to this forum asking for free help. You don't pay anyone any money, neither PHP Freaks nor the volunteer who is spending his/her spare time on helping you. We think that when you are getting such an awesome service for free, it is only fair that the help you have received is publicly archived for anyone to use. We also think that is a reasonable and very small price to pay.

 

In short: You're entitled to absolutely nothing, what so ever. Everything you've been given, is from Jessica's kindness. Way to be thankful...

 

So yeah, she's not going to reply to you any more. Neither am I, nor a whole lot of people, if that's how you appreciate us taking time from our very busy lives, to help you out for free.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.