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
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts

    error >>> Invalid argument supplied for foreach()

    I am working on a inventory control system and i want to edit user who assigned to stores. I user can be assigned to one or more stores. Iam using multiple seletc option.My problem here is in edit user page i wanna show user assigned to stores as selected. But when i am going to do that i got this error(Invalid argument supplied for foreach() on line 194 ) only when user is not assigned for all stores.
    eg- say there is 3 stores called A, B and C
    1.)if iam going to edit user who assigned to all three stores i won't get any error.
    2.)If iam going to edit user who assigned to 2 stores I will get above error twice.
    3.)If iam going to edit user who assigned to only one store I will get above error only once.

    Pls help me to correct this

    this is my code
    PHP Code:
    $emp_id $_GET['emp_id'];
    $result1 mysql_query("Select *
                                from members
                                JOIN store_employee on members.emp_id = store_employee.emp_id
                                JOIN stores on store_employee.store_id = stores.store_id
                                where members.emp_id=$emp_id"
    )
                   or die(
    mysql_error());
    $query="SELECT * FROM stores";
            
    $dropdown "<select name='store_id[]' multiple='multiple'  style='height:80px;' size='5' id='store'>";
            
    $result2 mysql_query ($query);
            while(
    $row2 mysql_fetch_array($result2)) 
            {
            
      
    $dropdown .= "\r\n<option value='{$row2['store_id']}'";
      
                foreach(
    mysql_fetch_array($result1) as $row1)     //this is line 194
                  
    {
                     if((
    $row1['store_id'])==($row2['store_id']))
                      {
                          
    $dropdown .=" selected='selected'";
                      }
                      
                 }
      
    $dropdown .= ">{$row2['store_name']}</option>";
            }
            
    $dropdown .= "\r\n</select>";
            echo 
    $dropdown

  • #2
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    I have used
    PHP Code:
    while ($rows mysql_fetch_array($result1)) { 
    instead of foreach but it doesnt work tooo but then it doesnt show all selected items.It shows only ane selected item. dats y i use this way to get results
    Last edited by viddz; 07-30-2012 at 11:05 PM.

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    My understanding is that mysql_fetch_array won't restart once it has exhausted the rows.

    You should perhaps consider storing the store names in an array, which you can then loop over repeatedly. There's some code samples here.

    I feel sure that you could do this with one query though.

    I assume you have a good reason not to use mysqli
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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