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

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\>

 

Has that pilot been awarded an award yet?

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

Vangelis,

Outstanding! That works great!

Thank you so much!

<big thumbs up!>

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.