Jump to content


Photo

**SOLVED** while() meeting 2 conditions


  • Please log in to reply
2 replies to this topic

#1 tristanlee85

tristanlee85
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 April 2006 - 07:17 AM

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

//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:

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:

Name    | State
------------------
Tristan |   1
Ben     |   0
Aaron   |   1

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 14 April 2006 - 08:43 AM

Try

$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>";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 tristanlee85

tristanlee85
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 April 2006 - 03:59 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users