ToiletDuck Posted May 28, 2017 Report Posted May 28, 2017 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> <img src=".$award->image." alt=".$award->name." /><br>".$award->name." </td>"; $i++; } echo "</tr>" ; echo "</table> "; ?> </ul> Quote
flyalaska Posted May 28, 2017 Report Posted May 28, 2017 (edited) Has that pilot been awarded an award yet? Edited May 28, 2017 by flyalaska Quote
Members Vangelis Posted May 28, 2017 Members Report Posted May 28, 2017 if (isset($allawards)) { foreach ($allawards as $award) { if ($i % 5 === 0) { echo '</tr><tr>'; } echo "<td> <img src=".$award->image." alt=".$award->name." /><br>".$award->name." </td>"; $i++; } } Try this code Quote
ToiletDuck Posted May 28, 2017 Author Report Posted May 28, 2017 Vangelis, Outstanding! That works great! Thank you so much! <big thumbs up!> Quote
Moderators Parkho Posted June 7, 2017 Moderators Report Posted June 7, 2017 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> <img src=".$award->image." alt=".$award->name." /><br>".$award->name." </td>"; $i++; } } This will show a message that the pilot has no awards yet instead. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.