Jump to content

Recommended Posts

I'm getting PHP Warnings about an "Invalid argument supplied for foreach()". Below is my code and the warning relates to the last section which is trying to loop the relevant array. Could someone give me a hand in fixing the error? Thanks!

 

<?php 
if($page == '/home.php' || $page == '/about.php' || $path == '/work/'){
   $links [0] = "<a href=\"/#.php\">#</a>";
   $links [1] = "<a href=\"/#.php\">#</a>";
   $links [2] = "<a href=\"/#.php\">#</a>";
   $links [3] = "<a href=\"/#.php\">#</a>";
}

elseif($page == '/blog.php') {
   $links [0] = "<a href=\"/#.php\">#</a>";
   $links [1] = "<a href=\"/#.php\">#</a>";
}

elseif($page == '/contact.php') {
   $links [0] = "<a href=\"/#.php\">#</a>";
   $links [1] = "<a href=\"/#.php\">#</a>";
}
?>
      <div id="links">
         <h3><img src="#" alt="#" /></h3>
         <ul>
<?php
foreach ($links as $value)
{
echo '<li>' . $value . '</li>
';
}
?>
         </ul>
      </div>

their can be times when the links isn't an array.. (ie $page = "")

 

so change

<?php
foreach ($links as $value)
{
echo '<li>' . $value . '</li>
';
}
?>

 

to

<?php
if(is_array($links))
{
foreach ($links as $value)
{
echo '<li>' . $value . '</li><br>'; //added <br> to put the break inn
}
}
?>

 

as a side note

 

try this

   $links[] = "<a href=\"/#.php\">#</a>";
   $links[] = "<a href=\"/#.php\">#</a>";
   $links[] = "<a href=\"/#.php\">#</a>";
   $links[] = "<a href=\"/#.php\">#</a>";

will all of them

 

as it will build the array (no sense adding the numbers when PHP will do it for you)

Thank you, that seems to have done the trick.

 

The php warning was also showing up in SOME instances where I was using the code below:

 

<?php
if(strpos($page, 'home.php') !== false) {
   $links [] = '<a href="#">#</a>';
   $links [] = '<a href="#">#</a>';
   $links [] = '<a href="#">#</a>';
}

elseif(strpos($page, 'about.php') !== false) {
   $links [] = '<a href="#">#</a>';
   $links [] = '<a href="#">#</a>';
   $links [] = '<a href="#">#</a>';
}
?>

      <div id="links">
         <h3>#</h3>
         <ul>
<?php
foreach ($links as $value)
{
echo '            <li>' . $value . '</li>
';
}
?>
         </ul>
      </div>

 

But I was in the process of replacing this kind of code with the code $page == '#' , because I wanted to use OR logic but didn't know how to do that while using strpos.

 

//added <br> to put the break inn

 

I wanted the new line to appear in the source code, not in the browser...I like having the final xhtml properly layed out :). Is there a better way of doing it than getting it to echo the necessary spaces/new lines for me?

for the newlines (\n) see below (basically how i would would do it)

 

if you do a

var_dump($page);

what is returned?

 

 

<?php
if(strpos($page, 'home.php') !== false)
{
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
}elseif(strpos($page, 'about.php') !== false){
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
}
echo '      <div id="links">\n';
echo '         <h3>#</h3>\n';
if(is_array($links))
{
echo "         <ul>\n";
foreach ($links as $value)
{
	echo "            <li>' . $value . '</li>\n";
}
echo "         </ul>\n";
}
echo "      </div>";
?>

OK. cool, maybe this would be a better solution ..

<?php
switch(strtolower($page))
{
default:
//break; //not using break here to the default will be home 
case "/home.php":
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
break;
case "/about.php":
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
   $links[] = '<a href="#">#</a>';
break;
}

echo '      <div id="links">\n';
echo '         <h3>#</h3>\n';
echo "         <ul>\n";
//don't need to check to see if its an array as the default create the array 
foreach ($links as $value)
{
echo "            <li>' . $value . '</li>\n";
}
echo "         </ul>\n";
echo "      </div>";
?>

for small sections on HTML i use the echo,

i wouldn't go as far as saying it better but i find it easier to read

anything over about 10 lines of static html i'll probably use the php close tag ?> then reopen it after the html

 

 

EDIT: just noticed a mistake

change

echo "            <li>' . $value . '</li>\n";

to

echo "            <li>$value</li>\n";

Yeah, I found that putting that xhtml into the php did make it easier to read and format.

Also noticed that /n needs to be in " " rather than ' '

 

What was wrong with

' . $value . '

I was given that line of code by another member, and since I wasn't getting any php warnings/errors, I'm curious as to why it is incorrect.

 

Thanks all the tips and cleanup of my code!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.