BrianM Posted June 20, 2008 Share Posted June 20, 2008 I have a dynamic table written with PHP and Javascript, and if it doesn't return any rows from the table it gives this output as an error: Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\new_mps\report\view.php on line 21 Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\new_mps\report\view.php on line 76 Instead, I want to assign my own error output with an echo statement. How would I say replace the automatic error with my own echo statement errors? Here is my code for the page, and I've commented lines 21 and 76: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>MPS Project Tracking - by Brian Medley</title> </head> <?php if(!isset($_GET['table'])) { die("You must have a table name set in the query string."); } $table = $_GET['table']; mysql_connect("localhost", "brian", ""); mysql_select_db("reports"); function returnheaders() { global $table; $sql = mysql_query("SELECT * FROM `$table` LIMIT 1") or die(mysql_error()); $array = mysql_fetch_array($sql); foreach($array as $key => $value) { // line 21 if(!is_numeric($key) && $key != "id") { echo("<td id=\"header_$key\">$key</td>\n"); } } } function returndata() { global $table; $sql = mysql_query("SELECT * FROM `$table`"); while($row = mysql_fetch_array($sql)) { echo("<tr id=\"dataset_row".$row['id']."\">\n"); foreach($row as $key => $value) { if(!is_numeric($key) && $key != "id") { echo("<td id=\"".$key.$row['id']."\" nowrap><a id=\"".$key."_".$row['id']."_value\" href=\"javascript:edit_row(".$row['id'].");\">".$value."</a></td>\n"); } } echo("</tr>\n"); } } function loaddynamicjava() { global $table; echo("<script type=\"text/javascript\">\nvar readylight = true;\nfunction edit_row(row) {\nvar currentval;\n"); $sql = mysql_query("SELECT * FROM `$table` WHERE id=1") or die(mysql_error()); $array = mysql_fetch_array($sql); foreach($array as $key => $value) { if(!is_numeric($key) && $key != "id") { echo("currentval = document.getElementById(\"".$key."_\"+row+\"_value\").innerHTML;\n"); echo("document.getElementById(\"".$key."\"+row).innerHTML = "); if($key == "date") echo("'<input type=\"button\" name=\"save\" value=\"Save\" onClick=\"savedata('+row+');\">"); else echo("'"); echo("<input type=\"text\" id=\"".$key."_'+row+'\" name=\"".$key."_'+row+'\" value=\"'+currentval+'\" />';\n"); } } echo("}\nfunction savedata(row) {\nif(!ready) { alert(\"Database is still saving other data!\"); return; }\nready = false;\nvar currentdata;"); foreach($array as $key => $value) if(!is_numeric($key)) if($key == "id") echo("document.getElementById(\"id\").value = row;\n"); else echo("document.getElementById(\"".$key."\").value = document.getElementById(\"".$key."_\"+row).value;\n"); echo("document.getElementById(\"theform\").submit();\n"); foreach($array as $key => $value) if(!is_numeric($key) && $key != "id") { echo("currentdata = document.getElementById(\"".$key."_\"+row).value;\n"); echo("document.getElementById(\"".$key."\"+row).innerHTML = '<a href=\"javascript:edit_row('+row+');\" id=\"".$key."_'+row+'_value\">'+currentdata+'</a>';\n"); } echo("}\nfunction nowready() { ready = true; }</script>"); } function returnformfields() { global $table; $sql = mysql_query("SELECT * FROM `$table` LIMIT 1"); $array = mysql_fetch_array($sql); foreach($array as $key => $value) // line 76 if(!is_numeric($key)) echo("<input type=\"hidden\" name=\"".$key."\" id=\"".$key."\" />\n"); echo("<input type=\"hidden\" name=\"table\" value=\"$table\" />\n"); } ?> <?php loaddynamicjava(); ?> <body> <table border="1" cellpadding="0" cellspacing="0"> <tr class="header"><?php returnheaders(); ?></tr> <?php returndata(); ?> </table> <form action="update.php" method="post" target="hiddenframe" name="theform" id="theform"> <?php returnformfields(); ?> </form> Go <a href="javascript:history.back();">back</a> a page. <iframe src="about:blank" style="display: none;" name="hiddenframe" id="hiddenframe" onLoad="nowready();">This page requires iFrames which your browser does not support.</iframe> </body> </html> Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/ Share on other sites More sharing options...
hitman6003 Posted June 20, 2008 Share Posted June 20, 2008 have a test to make sure it's an array... if (is_array($array)) { foreach($array as $key => $value) { // line 21 if(!is_numeric($key) && $key != "id") { echo("<td id=\"header_$key\">$key</td>\n"); } } } else { echo '<td>There were no rows returned</td>'; } Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/#findComment-569736 Share on other sites More sharing options...
BrianM Posted June 20, 2008 Author Share Posted June 20, 2008 I added an if statement suggesting that .. <?php if (!$array) { echo "error blah blah .."; } ?> But that still doesn't get rid of the automated PHP error output, is disabling display_errors the only way to get rid of this? Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/#findComment-569737 Share on other sites More sharing options...
Stephen Posted June 20, 2008 Share Posted June 20, 2008 That wouldn't check if it's an array or not though. Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/#findComment-569743 Share on other sites More sharing options...
hitman6003 Posted June 20, 2008 Share Posted June 20, 2008 That only checks to see if it exists (or is boolean false if it's of that datatype). As stated by Stephen, it does not check to see if it is an array, or if it has any elements. Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/#findComment-569749 Share on other sites More sharing options...
DarkWater Posted June 20, 2008 Share Posted June 20, 2008 Please carefully read hitman6003's post. He showed you what you need to do. =/ Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/#findComment-569751 Share on other sites More sharing options...
BrianM Posted June 20, 2008 Author Share Posted June 20, 2008 It worked perfectly! Thank you, hitman6003. Link to comment https://forums.phpfreaks.com/topic/111028-foreach-problem/#findComment-569756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.