Jump to content

What variable do I use?


BrianM

Recommended Posts

Here is a script I'm using to display a table from a database and when you click on any field in a row which has content, the entire row becomes editable. After having a bit of trouble I finally got the database to update just using a set number/text I put in the update page, but now I'm trying to figure out which variable to use from the page displaying the table to get the update.php page to update the database as it should.

 

index.php

<!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>Untitled Document</title>
<?php
mysql_connect("localhost", "brian", "");
mysql_select_db("example");

function returnheaders() {
$sql = mysql_query("SELECT * FROM example WHERE ID=1") or die(mysql_error());
$array = mysql_fetch_array($sql);
//print_r($array);
foreach($array as $key => $value) {
	if(!is_numeric($key) && $key != "ID") {
		echo("<td id=\"header_$key\">$key</td>\n");
	}
}
}
function returndata() {
$sql = mysql_query("SELECT * FROM example");
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() {
echo("<script type=\"text/javascript\">\nvar readylight = true;\nfunction edit_row(row) {\nvar currentval;\n");
$sql = mysql_query("SELECT * FROM example 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 == "PermitProcess")
			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(\"System 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("}");
echo("}\nfunction nowready() { ready = true; }</script>");
}
function returnformfields() {
$sql = mysql_query("SELECT * FROM example LIMIT 1");
$array = mysql_fetch_array($sql);
foreach($array as $key => $value)
	if(!is_numeric($key))
		echo("<input type=\"hidden\" name=\"".$key."\" id=\"".$key."\">\n");
}
?>
<?php loaddynamicjava(); ?>
</head>

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

 

update.php

<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<?php

mysql_connect("localhost", "brian", "");
mysql_select_db("example");

$sql = "UPDATE example SET Required = 'what_var_do_i_place_here??' WHERE ID = '1'";
mysql_query($sql);

?>

<body>
</body>
</html>

Link to comment
Share on other sites

You can do it by either adding a <form> around each row and use the (name="ID_'+row+'") for the update variable, or you can put a form around the whole page, then use arrays (name="ID[]") for your variables and loop them on your update page - using ID as the key of course.

Link to comment
Share on other sites

First of all, this is a free forum, if you want a response on your terms, you can advertise for some paid help. Please don't complain about not getting an answer after less than an hour.

 

The variable that goes there is defined by you. I hope that makes sense. When you grab the data from the form, you save it to a variable. Correct? What data do you want to go in there?

Link to comment
Share on other sites

Now that I look at it again, you'll probably want to use ID[] array type names for your variable names, because it looks like you want to edit multiple rows then update them all, no? If so, you'll need to define the variables as arrays.

 

<form>

<input type="text" name="ID[]" value="54">

<input type="text" name="ID[]" value="43">

<input type="text" name="ID[]" value="12">

</form>

 

then loop it on your update page:

 

$ID[] = $_POST['ID'];

 

foreach ($ID as $temp) {

sql = "update mytable where ID = $temp ";

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.