Forum Moderators: coopster

Message Too Old, No Replies

Editing an Array/Table of Data

Looking for a reference to example code for editing a table of data

         

John_P

7:32 am on Jul 20, 2011 (gmt 0)

10+ Year Member



Hi all,

I am creating a simple php/mysql application to keep track of correspondence (letters etc) that need to be written and sent by my wife's psychology practice.

I am ok with the mysql and getting records into and out of the database, creating new records and displaying information (data fields are basic things like names, dates, type of letter, etc).

What I need to be able to do is;
1. Take the data from an array in php and display the list of letters (there might be a dozen or so) in a table;
2. Allow the user to edit the data (e.g., change status from 'drafted' to 'approved', add comments in a comment field);
3. Then capture the changed data fields back in another array in php.

I understand that this is a form action of some sort, and while I can do simple forms, one that is handling arrays is beyond me.

I assume that this is something that has been addressed many times in the past, so if anyone could direct me to a prior message or code template it would be greatly appreciated.

Thanks

rocknbil

4:04 pm on Jul 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard John_P, the first question is why you'd need to store the letters in an array. Variables (arrays included) take up memory, and eventually can slow down your app - you can do all the sorting you need directly in mysql.

I'd approach it something like this:



$letters = null; // squech concatenation warnings
$query = "select id,title,date_format(posted_date,"%d/%m/%Y") as dt from letters order by posted_date desc";
$result = mysql_query($query) or die("Cannot get letters: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
// Built the table content with the concatenation operator,.=
$letters .= "<tr><td><a href="edit-script.php?rec_id=" . $row['id'] .
">Edit " . $row['title'] . "</a></td>
<td>" . $row['dt'] . </td></tr>
";
}
//
if ($letters) {
echo "<table>
<tr><th>Letter Title</th><th>Posted Date</th></tr>
$letters
</table>
";
}
else { echo "<p>There are no letters to display</p>"; }


Then "edit-script.php" would do something like this:


$id=$title=$content=null;
// verify a valid id query
if (isset($_GET['rec_id']) and is_numeric($_GET['rec_id']) and ($_GET['rec_id']) > 0)) {
$query = "select id,title,content from letters where id=" $_GET['rec_id'];
$result = mysql_query($query) or die("Cannot get letter: " . mysql_error());
if ($row = mysql_fetch_array($result)) {
$id = $row['id']; // redundant, you could use the $_GET var
$title = $row['title'];
$content = $row['content'];
echo '
<form method="post" action="update-letter.php">
<input type="hidden" name="rec_id" id="' . $id . '">
<p><label for="title">Title:</label>
<input type="text" name="title" id="title" value="' . $title . '"></p>
<p><label for="content">Content:</label>
<textarea name="content" id="content" rows="20" cols="55">' . $content . '</textarea></p>
<p><input type="submit" value="Update Letter"></p>
</form>
';
}
}
else { echo "<p>Invalid record query.</p>"; }


Then "update-letter.php" would take the data from $_POST, cleanse it, and update the record.

This is not "working code" (typed on the fly) and for example only, there are many things you'll need to add to cleanse input and make the data database safe . . . . but there's really no need to store database data in an array if you leverage the power of mysql.

Once working, you could go back to the original setup and add checkboxes for "delete" and wrap it in a form that posts to a delete-letters.php in the same way.