tags:

views:

29

answers:

2

Hello, everyone :)

Let's say I'm writing some forum software. Let's additionally say that that forum software has a personal messaging component. I'd like the user to be able to check a few messages out of that list, and then click a delete button somewhere else to delete all of those messages.

Another similar application is GMail, or pretty much every other kind of webmail application.

Is there a common pattern for implementing this? I can just stick a database row ID in each table row, but that seems like a kludge....

+2  A: 

Actually, sticking a database-friendly identifier in every row is the standard solution to this problem. In your case, the identifier would be in the value attribute of the checkbox.

The extended pattern is to let every single operation accept a list of identifiers instead of only one (so that all your operations can act as batches).

Victor Nicollet
+1 for suggesting the way to generalize this.
Billy ONeal
+2  A: 

The usuabl way of acconplishing this is to let the BROWSER send all selected IDs with an array.. similar to this:

<input name="test[]" value="123" /> foo<br />
<input name="test[]" value="133" /> bar<br />
<input name="test[]" value="3" /> barfoo<br />
<input name="test[]" value="4" /> foobar<br />

<?php
if (!empty($_GET['test']) && is_array($_GET['test'])) {
    foreach ($_GET['test'] as $k => $v) {
        echo '<br />delete id' . (int) $v;
    }
}
?>

Please note that I did not run this code. So there might be a typo in it. :D

Jan.
+1 -- I was not aware of this -- I was defining separate `name` s for each and looping through `$_POST`!
Billy ONeal