Jump to content

Archived

This topic is now archived and is closed to further replies.

tristanlee85

**SOLVED** while() meeting 2 conditions

Recommended Posts

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

[code]//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++;
}
}[/code]

Basically, my main focus is on this:

[code=php:0]while ($count < $employees_left && $state_menu == '1')[/code]

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]Name    | State
------------------
Tristan |   1
Ben     |   0
Aaron   |   1[/code]

In this case, I would only want it to echo out [code]<option>Tristan</option>[/code] and [code]<option>Aaron</option>[/code] and not echo out [code]<option>Ben</option>[/code] 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.

Share this post


Link to post
Share on other sites
Try

[code]$employee_list="SELECT name FROM employees WHERE state = 1";

$result = mysql_query($employee_list);

while ($row = mysql_fetch_row($result)) {

         echo "<option> $row[0] </option>";
}[/code]

Share this post


Link to post
Share on other sites
Oh wow. Perfect! Thanks for the help. I was trying to use if() statements and everything else. Thank you so much. Sometimes the simpliest things get you.

Share this post


Link to post
Share on other sites

×

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.