while() meeting 2 expectations

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

while() meeting 2 expectations

Post by tristanlee85 »

To start, I'm going to post the code of my issue.

Code: Select all

//Used for the employee menu and evaluations left to be completed
$employee_list="SELECT * FROM employees";
$total_employees=mysql_query($employee_list);
$employees_left=mysql_numrows($total_employees);
$eval_remaining=($employees_left - $eval_complete);

//Evaluates a variable for 'state' == 1
$state = mysql_query('SELECT state FROM employees');
$state_menu=mysql_result($state,"state");

if ($employees_left==0)
{
	echo "<option>--No employees in database--</option>";
}
else
{
$count=0;
while ($count < $employees_left && $state_menu == '1') {

$name_menu=mysql_result($total_employees,$count,"name");

echo "<option>$name_menu</option>";
$count++;
}
}
Basically, my main focus is on this:

Code: Select all

while ($count < $employees_left && $state_menu == '1')
What I'm wanting it to do is only run through the while() loop while $count < $employees_left and as long as the $state of the employee is "1". For example, I have the following in my table:

Code: Select all

Name    | State
------------------
Tristan |   1
Ben     |   0
Aaron   |   1
In this case, I would only want it to echo out

Code: Select all

<option>Tristan</option>
and

Code: Select all

<option>Aaron</option>
and not echo out

Code: Select all

<option>Ben</option>
since Ben's state is "0".

I'm sure you guys can think of something to make this simple and make it work. I've been up too long. Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the loop terminates when the expression fails one time. If you want the loop to only process the data found for each iteration under certain conditions like you've said, an if will often be used inside the loop to sense where the data corresponds to the needs.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

So I'd use an 'if' inside the 'while' loop? Can you give me an example? I tried a few and nothing worked.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the if would check the state, if it's one, do whatever processing, otherwise don't process for that iteration.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Yeh I'm really not understanding what you are meaning.

Code: Select all

if ($employees_left==0)
{
	echo "<option>--No employees in database--</option>";
}
else
{
$count=0;
if ($state == '1')
{
	while ($count < $employees_left) {
	$name_menu=mysql_result($total_employees,$count,"name");


	echo "<option>$name_menu</option>";
	$count++;
}
}
}
It doesn't work. Of course I am missing the 'else', but I don't know what it's supposed to return if state != 1.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The if must be inside the loop to have an effect.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Alright, so:

Code: Select all

if ($employees_left==0)
{
	echo "<option>--No employees in database--</option>";
}
else
{
$count=0;
while ($count < $employees_left) {
$name_menu=mysql_result($total_employees,$count,"name");
if ($state == '1')
{
	echo "<option>$name_menu</option>";
}
else
{
	//what goes here to skip the "state 0" field, increase the count by 1, and re-execute the while loop?
}
$count++;
}
}
ntbd
Forum Newbie
Posts: 21
Joined: Wed Apr 12, 2006 6:42 am

Post by ntbd »

Would the easiest way not be to use a different query?

Code: Select all

$employee_list="SELECT * FROM employees WHERE state='1'";
$total_employees=mysql_query($employee_list);
$employees=mysql_numrows($total_employees);
$eval_remaining=($employees_left - $eval_complete);
if (!$employees) {  echo "<option>--No employees in database--</option>"; } else {
$count=0;
while ($count < $employees) {
$name_menu=mysql_result($total_employees,$count,"name");
echo "<option>$name_menu</option>";
$count++;
}
}
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

That would work, but where does it check to see if the employee's state is 1?
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Thanks everyone. I figured it out. Instead of the if, I called the query different.

Now that this is off topic, here's my other issue:

Code: Select all

//Insert the evaluation into the database
$sql = mysql_query("SELECT * FROM eval_num WHERE name='$name'");
$result = mysql_fetch_array($sql);
$query = "INSERT INTO eval_num VALUES ('','$name','$manager','$comment','$a','$b','$c','$d','$e','$f','$g','$h','$i','$j','$k','$l','$m','$n','$o','$p','$q','$r','$s','$t','$u','$flowa','$flowb')";

//Set the state of the employee of the entered evaluation to '0'
$sql_state = mysql_query("SELECT * FROM employees WHERE state = 1");
$result_state = mysql_fetch_array($sql_state);
$query = "UPDATE employees SET name = '$name', state = '0' WHERE name = '{$result_state['name']}'";
Whenever an evaluation is submitted, the first block of code is assign to that function. Now, whenever I submit an evaluation for Ben, it's going to insert "Ben" into the eval_num table along with the other values as well. With the second block of code, whenever an evaluation is submitted, I want the "state" of the person whose evaluation was submitted to change from "1" to "0" and this is in table "employees".

With the way the code is right now, instead of setting the "state" to '0' of the $name that was just submitted, it just goes right down the list and sets the "state" of the next person to '0'. For example:

Code: Select all

Name     |     State
------------------------
Ben      |     1
Josh     |     1
Drew     |     1
Say I submit an evaluation for Ben. The table changes like this:

Code: Select all

Name     |     State
------------------------
Ben      |     0
Josh     |     1
Drew     |     1
Now let's say I submit on for drew. The table changes like this:

Code: Select all

Name     |     State
------------------------
Ben      |     0
Josh     |     0
Drew     |     1
Instead of setting Drew's state to 0, it just goes right now the list. What gives?
Post Reply