0

I see

Warning: Invalid argument supplied for foreach()

Heres the line: foreach ($jobs_by_category as $category_name => $job_items) and here's the rest of my code.

function display_job_list ()
{       
    global $db;
    global $jobs_by_category;
    global $category_name;
    global $job_items;
    global $item;

    foreach ($jobs_by_category as $category_name => $job_items)
    {
        // display job category
        output_div ('job_category', $category_name);
        // print_r($category_name);
        // browse through job items
        foreach ($job_items as $item)
        {
            // output the 3 fields of this item
            output_div ('job_qty'        , $item['item_quantity'   ]);
            output_div ('job_unit'       , $item['item_unit'       ]);
            output_div ('job_description', $item['line_description']);
        }
    }
}
moopet
  • 6,014
  • 1
  • 29
  • 36
user3135712
  • 57
  • 1
  • 7
  • What do you get if you do a var_dump($jobs_by_category) before the foreach block? – grim Jan 11 '14 at 05:22
  • Did you check the value of ``$jobs_by_category``? You should validate that its an array using something like ``is_array($jobs_by_category)`` – SameOldNick Jan 11 '14 at 05:22
  • Hypothetically it could also be an object, you can use is_object($jobs_by_category) in addition to is_array($jobs_by_category) as mentioned by @ub3rst4r. – grim Jan 11 '14 at 05:24
  • May be the your array is empty try printing the whole array first – Ashoka Jan 11 '14 at 05:26
  • @grim After the var_dump($jobs_by_category)I got NULL – user3135712 Jan 11 '14 at 06:00
  • Seems like you found your answer :) figure out why $jobs_by_catagory is null and you no longer have a problem. your code _looks_ fine as it is otherwise – bobkingof12vs Jan 11 '14 at 06:04
  • @sparatan117 I get another error. Parse error: syntax error, unexpected '{' at this line if(!empty($job_items){ – user3135712 Jan 11 '14 at 06:08
  • @bobkingof12vs I believe that you or correct. I have a file with 4 different functions in it. It seems to work when I comment the functions out. I don't think that the functions are calling one another. Should I post the all the functions. My HTML file uses the functions. – user3135712 Jan 11 '14 at 06:49
  • We wont be able to help you further unless you do. A general tip for debugging though is to just follow your variables. Just backtrace to where `$jobs_by_category` is set to see why it is being set as null. – bobkingof12vs Jan 11 '14 at 20:55

3 Answers3

1

You must ensure variable is array before foreach.

function display_job_list ()
{

    global $db;
    global $jobs_by_category;
    global $category_name;
    global $job_items;
    global $item;

    if (is_array($jobs_by_category)){
        foreach ($jobs_by_category as $category_name => $job_items)
        {
            // display job category
            output_div ('job_category', $category_name);
            //print_r($category_name);
            // browse through job items
            if (is_array($job_items)){
                foreach ($job_items as $item)
                {
                    // output the 3 fields of this item
                    output_div ('job_qty'        , $item['item_quantity']);
                    output_div ('job_unit'       , $item['item_unit']);
                    output_div ('job_description', $item['line_description']);
                }
            }
        }
    }

}
?>
Poom
  • 36
  • 2
0

Whoops, lets try this again

    <?php 
    function display_job_list ()
    {

        global $db;
        global $jobs_by_category;
        global $category_name;
        global $job_items;
        global $item;

        foreach ($jobs_by_category as $category_name => $job_items){
    // display job category
        output_div ('job_category', $category_name);
    //print_r($category_name);
    // browse through job items
        if(!empty($job_items)){
            foreach ($job_items as $item)
            {
    // output the 3 fields of this item
                output_div ('job_qty'        , $item['item_quantity'   ]);
                output_div ('job_unit'       , $item['item_unit'       ]);
                output_div ('job_description', $item['line_description']);
            }
        }
        else{
            continue;
        }

    }
}
?>
Sparatan117
  • 138
  • 8
  • How do you know ``$job_items`` is the problem? He references the problem being with iterating ``$jobs_by_category`` – SameOldNick Jan 11 '14 at 05:39
  • See I would have thought that, but then I thought about it for a second and if the first loop didn't work, then he wouldn't have written the second loop. I'm betting that there's an empty value somewhere in that array / object – Sparatan117 Jan 11 '14 at 05:40
  • @Sparatan117 I think that the problem is that I'm not connecting to the other functions that this function is relying on. I used global $category_name; ect but it doesn't seem to be connecting. – user3135712 Jan 11 '14 at 07:01
  • I noticed you didn't pass any variables into the function. Wouldn't it just be easier to pass it in? – Sparatan117 Jan 11 '14 at 07:47
0
$jobs_by_category = array();
some_code;
some_other_code;

function display_job_list ()
{

    :
    global $jobs_by_category;
    :
    if count($jobs_by_category > 0) {
       foreach ($jobs_by_category as $category_name => $job_items)
       {
           // processing code
           :
       }
    }
    else {
       print_a_message('No records to process');
    }
 }
crafter
  • 6,246
  • 1
  • 34
  • 46