Click to See Complete Forum and Search --> : Need Script to fill Table on-Site
Daria
09-30-2003, 11:21 AM
I have a table with, 4 columns and unlimited amount of rows (currently approx. 80).
Can I make a page where people can make their own entries in the cells and save it right on the website? Say 2-3 people update that table and they all want to have an access to it?
I could use Dreamweaver Contribute or something similar, but I don't want to have different people to work with it. I need something browser based.
Any ideas/suggestions?
Yep, it's a small task with PHP and MySQL. Even if you don't want to delve into databases, PHP could just write to the static HTML file without a problem. You'd just make a form that has four fields, one for each cell, and then append the data to the table, and save the page. Simple! If your server supports PHP and you want help, just post in the PHP forums...
Daria
09-30-2003, 12:50 PM
Thanks, Pyro! Let me search on PHP topics and if I wouldn't find anything, will do.
Lol... I decided to just make it for you after I posted that... Here it is:
writetable.php
<?PHP
$filename = "table_text.htm"; #CHMOD to 0666
if (isset($_POST['submit'])) {
$contents = file($filename);
$file = "";
foreach ($contents as $line) {
$file .= $line;
}
list($start, $end) = split("</table>", $file);
$end = "</table>".$end;
$new = '<tr>
<td>'.$_POST['column_one'].'</td>
<td>'.$_POST['column_two'].'</td>
<td>'.$_POST['column_three'].'</td>
<td>'.$_POST['column_four'].'</td>
</tr>
';
$data = $start.$new.$end;
$fp = fopen($filename, "w");
flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
header("Location: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">
</head>
<body>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>First Column: <input type="text" name="column_one"><br>
Second Column: <input type="text" name="column_two"><br>
Third Column: <input type="text" name="column_three"><br>
Fourth Column: <input type="text" name="column_four"><br>
<input type="submit" name="submit" value="Submit"></p>
</form>
</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</a></p>
</body>
</html>Or, if you want to view it in action: http://www.infinitypages.com/temp/table_text.htm
Daria
09-30-2003, 04:43 PM
you rock! :)
Hate to be a snoot, but not that I think of it... Is it possible make existing fields editable?
Daria
09-30-2003, 04:56 PM
Thanks Again!
I think you've just inspired me to learn PHP.
:)
-----------------------------------------------
Time! Time! My kingdom for more time!!!!
Great! PHP is the only way to fly... :) Just post in the PHP section with any questions you might have.
Also, it is indeed possible to make existing field editable, though if depending on the number, it would probably be best to use a database. If you want me to, I can try to write up a loop that will do it in the flat file, though the soonest I could get to it would be tomorrow.
Daria
10-01-2003, 08:57 AM
That would be great - just show me the ropes for this one, and I'll try not to take more of your time. If I'll get stumbled, I'll move on to PHP forum (or maybe this thread could be moved)
we should make an emoticon for "thanks"!
Ok, if you want to try it, this is how I would go about it:
First, you would want to do a preg_split() (http://us2.php.net/manual/en/function.preg-split.php) to get the entire table into a variable. From there, you'd want to extract each row (probably with another preg_split()) from the table.
Then, you'll know that the $rows (or whatever you name the variable to hold the <tr>...</tr> will be an array of all your rows. What I would do is allow users to choose which row to edit via a list of numbered links (ie 1 for row 1, 2 for row 2, etc). From there, it's mostly just a matter of updating the value of the array for whichever row was modified.
Let me know if you need any more help on this.