Jump to content

script error - Warning: Invalid argument supplied for foreach()


Recommended Posts

Posted

Hi all,

I'm trying to pull pilots awards in but having some trouble. If a pilot has an award this code works perfectly. However, if a pilot has NOT received an award yet I get an error. Does anyone know how to create an "if" statement that addresses an empty array?

error received:     Warning: Invalid argument supplied for foreach()

 

<strong>Awards</strong>
<li class="list-group-item"><?php echo "<table>";
 echo "<tr>" ;
 $i = 0;
 foreach ($allawards as $award) {
	 if ($i % 5 === 0) {
		 echo '</tr><tr>';
	 }
	 echo "<td>&nbsp;&nbsp;<img src=".$award->image." alt=".$award->name." /><br>".$award->name." &nbsp;&nbsp;</td>";
	 $i++;
 }
 echo "</tr>" ;
 echo "</table> "; ?>							
</ul>

 

  • Members
Posted
if (isset($allawards)) {
   foreach ($allawards as $award) {
	 if ($i % 5 === 0) {
		 echo '</tr><tr>';
	 }
	 echo "<td>&nbsp;&nbsp;<img src=".$award->image." alt=".$award->name." /><br>".$award->name." &nbsp;&nbsp;</td>";
	 $i++;
 }
}
 

Try this code

  • 2 weeks later...
  • Moderators
Posted
On 5/28/2017 at 11:00 PM, ToiletDuck said:

Vangelis,

Outstanding! That works great!

Thank you so much!

<big thumbs up!>

Alternatively, you can do the following:

if(!$allawards)
	{
		echo '<td align="center"> No Rewards Yet! </td>';
	}
else
	{
		foreach ($allawards as $award) 
			{
				if ($i % 5 === 0) 
					{
						cho '</tr><tr>';
					}
				echo "<td>&nbsp;&nbsp;<img src=".$award->image." alt=".$award->name." /><br>".$award->name." &nbsp;&nbsp;</td>";
				$i++;
			}
	}

This will show a message that the pilot has no awards yet instead.

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.

Loading...
×
×
  • Create New...