Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: foreach problem

  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    foreach problem

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

    on the first viewing of the page, $action is not set.

    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...
    PHP Code:
    <?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.
    Last edited by chastro; 07-27-2007 at 10:12 PM.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    post your actual code.. not example code... or we could potentially miss something that is breaking the code

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    post your actual code.. not example code... or we could potentially miss something that is breaking the code

    that is the actual code

    here is the query if you meant that

    $sql = "SELECT `id`, `name` FROM `employees` ORDER BY `name` DESC";
    $result = mysql_query ($sql, $connection) or die ("Couldn't execute query.");
    Last edited by chastro; 07-27-2007 at 10:34 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •