Jump to content

foreach problem


BrianM

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.