foreach problem (i think)

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
chastro
Forum Newbie
Posts: 2
Joined: Fri Jul 27, 2007 5:04 pm

foreach problem (i think)

Post by chastro »

Hi there, a little stumped on a form I am making for work...

on the first viewing of the page, $action is not set. (The form submits to itself)

when this is shown in the page, a list of checkboxes along side the employees' name is shown like so...

i.e.

[] Bob
[] Jim
[] Mary
[] Sara
[] Tim

...then the user is to make his/her selections of these employees, and click submit, this time with the action $final_confirm.

When the page now runs through, i need it to make the same list of employees, however if they had selected an employee before, it needs to be checked.

i.e.

[X] Bob
[] Jim
[X] Mary
[X] Sara
[] Tim

My problem is that I've gotten it to populate the check boxes on submit, but if I select three names, it returns this:

[X] Bob
[] Bob
[] Bob
[] Jim
[] Jim
[] Jim
[X] Mary
[] Mary
[] Mary
[X] Sara
[] Sara
[] Sara
[] Tim
[] Tim
[] Tim

if I select two names, it does the same thing, just with two lines each...

Code: Select all

<?php
//$sql = "SELECT bla bla bla..."
//$result = mysql_query bla bla bla...

while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$employee_name = $row['name'];

   if ($action == "final_confirm"){
      if (isset($employee)){
         foreach ($employee as $employee_selected) {
            if($employee_selected == $id){
            echo "<INPUT TYPE=\"CHECKBOX\" NAME=\"employee[]\" VALUE=\"$id\" CHECKED>$employee_name<BR>\n";
            } else {
            echo "<INPUT TYPE=\"CHECKBOX\" NAME=\"employee[]\" VALUE=\"$id\">$employee_name<BR>\n";
            }
         }
      }
   } else {
   echo "<INPUT TYPE=\"CHECKBOX\" NAME=\"employee[]\" VALUE=\"$id\">$employee_name<BR>\n";
   }
}

?>
In theory I see why the error is happening, I just dont see any other way to do this. I'm sure the answer is right in front of me or I just need to reorganize something, but I've been trying to do this for 5 hours and reading the foreach documentation on php.net isnt helping. Any help is appreciated.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

$employee, if it's an array, does not need to be looped through.

Instead, do this:

Code: Select all

if(!empty($employee['name']))
{
    //check box checked
} else
{
     //check box not checked
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
chastro
Forum Newbie
Posts: 2
Joined: Fri Jul 27, 2007 5:04 pm

Post by chastro »

scottayy wrote:$employee, if it's an array, does not need to be looped through.

Instead, do this:

Code: Select all

if(!empty($employee['name']))
{
    //check box checked
} else
{
     //check box not checked
}
yes, $employee is an array... but the value of the is not by name, it'd by the $id (i.e. $employee[$id])

this wont work though, (i've tried a variation before this thread...)

I have to loop through the DB to put the whole employee list out...but while that query is running, i need to check it to what was submitted from the form. if they match, CHECKED in the html.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

in_array() may be of interest.
Post Reply