Jump to content

Warning: Invalid argument supplied for foreach() in /home/scrynet/public_html/do


tlavelle

Recommended Posts

Warning: Invalid argument supplied for foreach() in /home/scrynet/public_html/do

 

How do I re-engineer this code to get rid of the above warning?

 

  function template ($file, $array)
  {
    $handle = fopen ('theme/' . gen ('theme') . ('' . '/templates/' . $file), 'r');
    $template = fread ($handle, 1024 * 1024);
    fclose ($handle);
    foreach ($array as $code => $value)
    {
      $template = str_replace ('{{' . $code . '}}', $value, $template);
    }

    return $template;
  }

Not sure how to use is_array with this function.  Don't I need to check to see if it is array before it gets to the function?  I attempted to implement it and got a blank page

 

  

function template ($file, $array)
  {
    $handle = fopen ('theme/' . gen ('theme') . ('' . '/templates/' . $file), 'r');
    $template = fread ($handle, 1024 * 1024);
    fclose ($handle);
    foreach ($array as $code => $value)
    {
      $template = str_replace ('{{' . $code . '}}', $value, $template);
    }

    return $template;
  }

function template ($file, $array)
  {
    $handle = fopen ('theme/' . gen ('theme') . ('' . '/templates/' . $file), 'r');
    $template = fread ($handle, 1024 * 1024);
    fclose ($handle);
    if (is_array($array))
    {
      foreach ($array as $code => $value)
      {
        $template = str_replace ('{{' . $code . '}}', $value, $template);
      }
    }

    return $template;
  }

 

this will only stop the error from arising.  if you REQUIRE $array to be an array, you're right, you'll need to check before it's sent (or define a default array).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.