Page 136 - PHP Novice to Ninja

Hi,
I am studying on Novice to Ninja 5th Edition, in particular the four Chapter page 136.

I can to add and delete jokes fine, but when delete the last one, I have these errors:

Notice: Undefined variable: jokes in /Applications/MAMP/htdocs/deletejoke/jokes.html.php on line 10

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/deletejoke/jokes.html.php on line 10

I am using the source code download from website.

Thank you

Post the lines the errors are on

It’s caused by there not being any jokes returned by the database. When there’s jokes in the database they get placed into the $jokes array but if there is no jokes then $jokes is just an empty string. To solve it, just before the line where you access the result set, add this line:

$jokes=array();

That will set $jokes up as an empty array, if there are jokes they’ll be added to the $jokes array, if there aren’t any jokes then the $jokes array will be empty and the foreach loop will see an empty array.

@HAWK can the appropriate person at HQ please fix this error? It seems to come up with every edition of Kevin’s book

Ok thank you.
I have try to add $jokes=array( ); before <?php foreach ($jokes as $joke): ?>, but when add a new joke and refresh the page, the joke not view.

Calling in @Simon_Mackie

It’s just before when the result set is fetched that it needs to go

Ok perfect thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.