Click to See Complete Forum and Search --> : EDIT html/table online


Daria
10-01-2003, 10:08 AM
Pyro, thanks for helping out in General (http://forums.webdeveloper.com/showthread.php?s=&threadid=18438), but I think I need more help.
I'll rephraze now:
how do I make the following page (content of the cells):
http://siriustechnology.com/test/table_text.htm
editable online?

Daria
10-01-2003, 10:19 AM
and don't mind the html source, I've been playing around with the file...
my question is re: content only.

pyro
10-01-2003, 12:33 PM
Well, it's a bit more complex than I first thought it'd be, but here you go. I commented it up fairly well...

writetable.php
<?PHP

$filename = "table_text.htm";

$contents = file($filename);

$file = "";
foreach ($contents as $line) {
$file .= $line; #get the contents of the file
}

$file = str_replace("\n", "", $file); #get rid of newline characters...

preg_match("/(<table(.*?)>)(.*?)(<\\/table>)/",$file, $matches); #match the table
$table_start = $matches[1]; #<table(.*?)>
$table_body = $matches[3]; #(.*?) (tables contents <tr><td> etc...)
$table_end = $matches[4]; #</table>

preg_match_all("/(<tr(.*?)>)(.*?)(<\\/tr>)/", $table_body, $table_rows); #match the rows
$tr_start = $table_rows[1]; #<tr(.*?)>
$tr_body = $table_rows[3]; #(.*?) (tr contents <td>contents</td> etc...)
$tr_end = $table_rows[4]; #</tr>

$rows = array(); #set up an array for the rows

for ($i=0; $i<count($tr_body); $i++) { #loop through the rows
$rows[count($rows)] = $tr_start[$i].$tr_body[$i].$tr_end[$i]; #add each row to the $rows array
}

#get number of cells
preg_match_all("/(<td(.*?)>)(.*?)(<\\/td>)/", $rows[0], $cell_contents); #match the cells
$num_cells = count($cell_contents)-1; #number of cells

if (isset($_POST['rownum'])) {
$_GET['row'] = $_POST['rownum']; #set up the $_GET variable again, after we submit the form
}


if (isset($_GET['row'])) {
$rownum = $_GET['row']; #get the index of the row
$row = $rows[$rownum]; #get the rows contents
preg_match_all("/(<td(.*?)>)(.*?)(<\\/td>)/", $row, $cell_contents); #match the cells
$td_start = $cell_contents[1]; #<td(.*?)>
$td_body = $cell_contents[3]; #(.*?) (td contents...)
$td_end = $cell_contents[4]; #</td>
$num_cells = count($cell_contents)-1;
}

if (isset($_POST['submit_edit'])) { #if we submitted the edited form
$newrow = "";
for ($i=0; $i<count($_POST)-2; $i++) { #loop through the $_POST variables
$newrow .= $td_start[$i].$_POST["column_$i"].$td_end[$i]."\n";
}
$newcontents = "";
for ($i=0; $i<count($rows); $i++) { #loop though the rows (<tr>...</tr>)
if ($i == $_POST['rownum']) { #if the row is the one we just edited
$newcontents .= "\n".$tr_start[$i]."\n".$newrow.$tr_end[$i];
}
else { #if it is any other row
$newcontents .= $rows[$i];
}
}
$newtable = $table_start.$newcontents.$table_end;

list($start, $end) = preg_split("/<table(.*?)>(.*?)<\\/table>/", $file);

$data = $start.$newtable.$end;
}

if (isset($_POST['submit'])) { #if we submitted the new row form
list($start, $end) = split("</table>", $file);
$end = "</table>".$end;

$new = "<tr>";
for ($i=0; $i<count($_POST)-1; $i++) { #loop through the $_POST variables
$new .= "<td>".$_POST["column_$i"]."</td>";
}
$new .= "</tr>";

$data = $start.$new.$end;
}

if (isset($data)) {
$fp = fopen($filename, "w"); #open
flock($fp, LOCK_EX); #lock
fwrite($fp, $data); #write
flock($fp, LOCK_UN); #release
fclose($fp); #close
header("Location:table_text.htm"); #redirect
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?PHP
if (isset($_GET['row'])) {
?>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><input type="hidden" name="rownum" value="<?PHP echo $rownum;?>">
<?PHP
foreach ($td_body as $cellnum => $cellval) { #loop through each cell
echo "Column $cellnum: <input type=\"text\" name=\"column_$cellnum\" value=\"$cellval\"><br>";
}
?>
<input type="submit" name="submit_edit" value="Submit"></p>
</form>
<?PHP
}
else {
?>

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<?PHP
for ($i=0; $i<$num_cells; $i++) {
echo "Column $i: <input type=\"text\" name=\"column_$i\"><br>";
}
?>
<input type="submit" name="submit" value="Submit"></p>
</form>
<?PHP
}
$links = "";
foreach ($rows as $index => $row) {
$links .= "<a href=\"".$_SERVER['PHP_SELF']."?row=$index\">row $index</a> | ";
}
$links = substr($links, 0, -2);
echo "<p>Edit Row:<br>".$links."</p>";

?>

</body>
</html>

table_text.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
table {
border-collapse: collapse;
}
td {
border: 1px solid;
}
</style>

</head>

<body>

<table>
<tr>
<td>Column One</td>
<td>Column Two</td>
<td>Column Three</td>
<td>Column Four</td>
</tr>
<tr>
<td>Info One</td>
<td>Info Two</td>
<td>Info Three</td>
<td>Info Four</td>
</tr>
</table>

<p><a href="writetable.php">Write New or Edit</a></p>

</body>
</html>And the live link again, is: http://www.infinitypages.com/temp/table_text.htm

Daria
10-01-2003, 01:57 PM
LOVE IT!
I owe you one.

pyro
10-01-2003, 01:59 PM
Awesome. Glad it's what you were looking for... :)

Daria
10-01-2003, 02:06 PM
That is beautiful work, man!
I have one question, though... (please, don't kill me!)
if I will have a lot of entries... It would be complicated to see which row is which. Can they be numbered automatically? If I will have 160 records and I need to edit 117th row... It's a pain to count them :) Could it be done kinda like in Excel, where there is a column with numbers on the left?

I could always make 5 columns and make a first entiry a number, too, but I had to ask...

pyro
10-01-2003, 02:18 PM
I'll see what I can do, but I've got to finish up some real work first... ;)

Daria
10-01-2003, 02:22 PM
Oh, please, take your time - you've already done more then enough!!!

Back to real work it is :)

pyro
10-01-2003, 03:46 PM
Ok, here ya go. A simple change, really. Take a look at http://www.infinitypages.com/temp/table_text.htm, and see if that helps you out.

Look for this in writetable.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?PHP
if (isset($_GET['row'])) {
?>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><input type="hidden" name="rownum" value="<?PHP echo $rownum;?>">
<?PHP
foreach ($td_body as $cellnum => $cellval) { #loop through each cell
echo "Column $cellnum: <input type=\"text\" name=\"column_$cellnum\" value=\"$cellval\"><br>";
}
?>
<input type="submit" name="submit_edit" value="Submit"></p>
</form>
<?PHP
}
else {
?>

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<?PHP
for ($i=0; $i<$num_cells; $i++) {
echo "Column $i: <input type=\"text\" name=\"column_$i\"><br>";
}
?>
<input type="submit" name="submit" value="Submit"></p>
</form>
<?PHP
}
$links = "";
foreach ($rows as $index => $row) {
$links .= "<a href=\"".$_SERVER['PHP_SELF']."?row=$index\">row $index</a> | ";
}
$links = substr($links, 0, -2);
echo "<p>Edit Row:<br>".$links."</p>";

?>

</body>
</html>And replace it with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
table {
border-collapse: collapse;
}
td {
border: 1px solid #ced6dc;
}
a {
color:#0077ff;
text-decoration:none
}
</style>
</head>

<body>

<?PHP
if (isset($_GET['row'])) {
?>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><input type="hidden" name="rownum" value="<?PHP echo $rownum;?>">
<?PHP
foreach ($td_body as $cellnum => $cellval) { #loop through each cell
echo "Column $cellnum: <input type=\"text\" name=\"column_$cellnum\" value=\"$cellval\"><br>";
}
?>
<input type="submit" name="submit_edit" value="Submit"></p>
</form>
<?PHP
}
else {
?>

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<?PHP
for ($i=0; $i<$num_cells; $i++) {
echo "Column $i: <input type=\"text\" name=\"column_$i\"><br>";
}
?>
<input type="submit" name="submit" value="Submit"></p>
</form>
<?PHP
}
$links = "";
echo "<table>";
foreach ($rows as $index => $row) {
echo $row;
echo "<tr><td colspan=\"4\" style=\"border-bottom: 3px solid;\"><a href=\"".$_SERVER['PHP_SELF']."?row=$index\">edit</a></td></tr>";

}
echo "</table>";

?>

</body>
</html>

Daria
10-01-2003, 03:55 PM
Ahhhhh. better then sex :)

Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

pyro
10-01-2003, 04:18 PM
Lol... You are very welcome... :)

Daria
10-05-2003, 11:59 AM
pyro, I'm doing something wrong...
take a look:
http://www.siriustechnology.com/temp/table_text.htm

pyro
10-05-2003, 05:57 PM
Hmm... it adds rows, just doesn't let you modify them, eh? You sure you have the right version uploaded? Or, perhaps something happened when I posted to the forums (they've been know to mess up code in posts before).

Anyway, here's a .zip of the latest versions that you can give a try:

Daria
10-05-2003, 07:35 PM
ufff... works. thankz!!

pyro
10-05-2003, 07:46 PM
hehe... No problem at all... :)

Daria
10-05-2003, 08:40 PM
"no problem" - easy for you to say :) I'm just getting started :)

Hint what should I do to change it into 7 columns?

pyro
10-05-2003, 09:04 PM
Ah, sorry. Just a little bug -- it was supposed to be portable and automatically take care of more/fewer cells. Anyway, got it fixed in this version:

Daria
10-05-2003, 09:10 PM
OK, I'm leaving you alone now :) thanks, man.

grateful,
D.

pyro
10-05-2003, 09:16 PM
Sure thing...

butlerps
10-07-2004, 03:25 PM
hi
I've been searching for a script that will do just this and I thought that my prayers had been answered.....until i found out that my host does not support php, does anyone out there know how else i could impliment a table on a friends webpage that can be edited through an 'admin' webpage? the only thing my server supports is cgi!!!!!

Thanks in anticipation of someones help

Philip (new to all this but willing to learn and help others wherever I can

drythirst
10-07-2004, 07:55 PM
Start a new thread...

Paul Jr
10-07-2004, 08:01 PM
Originally posted by butlerps
hi
I've been searching for a script that will do just this and I thought that my prayers had been answered.....until i found out that my host does not support php, does anyone out there know how else i could impliment a table on a friends webpage that can be edited through an 'admin' webpage? the only thing my server supports is cgi!!!!!

Thanks in anticipation of someones help

Philip (new to all this but willing to learn and help others wherever I can
Yes, it’s possible, but you probably won’t get much help in the PHP forum. Try posting a new thread in the Perl forum.

cdbicknell
10-26-2004, 06:17 PM
Pyro, I happened accross this thread where you detailed the daria script and really like the code. My question is can it be modified to get it's columns from a MySql database? This is exactly the type of code I've been looking for to edit the records in my database but I'm not skilled enough in php to script it myself. But if some could get me started I could probably figure it out.
Thanks for any input.

Dondo
11-19-2004, 05:48 AM
Oops... sorry... answered in an earlier post. Thanx Pyro!

_LOBO_
11-19-2004, 08:09 AM
Pyro, I happened accross this thread where you detailed the daria script and really like the code. My question is can it be modified to get it's columns from a MySql database? This is exactly the type of code I've been looking for to edit the records in my database but I'm not skilled enough in php to script it myself. But if some could get me started I could probably figure it out.
Thanks for any input.

This is interesting, how you can do that ?