Forum Moderators: coopster

Message Too Old, No Replies

Edit and Delete options for tabular data

Common Practices

         

coopster

1:17 am on Jan 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As server-side programmers we are often required to display a listing of information from a database in tabular form for routine maintenance. Take for example an administrator's function for maintaining a group of users that are allowed to enter a restricted area of the site. Typically, you have two options, display the data as a list of <a href=""> elements and offer maintenance through a GET request, or put them into a <form> with an *Edit* and *Delete* button for each item using the POST method.

There are a couple of options using the GET method. Typically we see ...

while (getting data from the database) { 
print '<td>' . $row['username'] . '</td>' .
'<td><a href="edit_user.php?id=' . $row['id'] . '>Edit</a></td>' .
'<td><a href="delete_user.php?id=' . $row['id'] . '>Delete</a></td>';
}
// which will give us something along the lines of ...
+------+------+--------+
¦ User ¦ Edit ¦ Delete ¦
+------+------+--------+
¦ John ¦ Edit ¦ Delete ¦
¦ Suzy ¦ Edit ¦ Delete ¦
¦ Bill ¦ Edit ¦ Delete ¦
¦ Joan ¦ Edit ¦ Delete ¦
¦ Eddy ¦ Edit ¦ Delete ¦
+------+------+--------+

... where the Edit and Delete columns are hyperlinks.

It works and it works just fine. Another option of course would be to drop in a URL without the query string and employ something like Apache's mod_rewrite engine to parse the static url and invoke our PHP script ...

while (getting data from the database) { 
print '<td>' . $row['username'] . '</td>' .
'<td><a href="edit_user/' . $row['id'] . '>Edit</a></td>' .
'<td><a href="delete_user/' . $row['id'] . '>Delete</a></td>';
}

I won't go into the RewriteRule, but you get the picture. This option forces a bit more work on the server and really doesn't accomplish anything except get the query string off the url. I'm not a big fan of the query string on the GET method but in some cases it works just fine.

The POST method is pretty self explanatory -- a form and submit buttons. I find myself trying to find new ways around the obviously simple routes every once in awhile and thought I'd ask others what they use, and why.

Anybody care to discuss?

ergophobe

4:03 am on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I'm not a big fan of the query string on the GET method but in some cases it works just fine.

I try to keep them out of public areas, but I think anyone who wears the title "admin" should be able to handle seeing a query string in a url. Since these areas are usually password protected, it also removes worries related to SEO.

As for the rest, I definitely know what you mean. If you remember way back, I was for a while enamored with the idea of displaying first as formatted text and then having it turn into an editable textarea upon clicking (there's a good example on the quirksmode.org site). That way you could have it both ways - nice looking for reading, but still editable if necessary.

The problem is that the data on the site I was thinking of doing it for was not formatted really, so there was no particular advantage to doing it in the long run. I still like the idea, but I've only done it fooling around on my experiments.

I usually try to do a table with checkboxes if possible, so the admin doesn't have to submit a form for each and every change. In your GET example, the admin would have to click on a link, wait for the page to reload, click on a link, wait for the page. I find that trying, so I like to give them a form that's more like


+------+-------------+-----------+
¦ User ¦ User Level ¦ Delete ¦
+------+--------_----+-----------+
¦ John ¦[select box] ¦[check box]¦
¦ Suzy ¦[select box] ¦[check box]¦
¦ Bill ¦[select box] ¦[check box]¦
¦ Joan ¦[select box] ¦[check box]¦
¦ Eddy ¦[select box] ¦[check box]¦
+------+-------------+-----------+
¦ [submit button ¦
---------------------------------+

I would much rather batch process if possible.