views:

158

answers:

2

I have a $_POST array that results from selecting (in this case) 3 rows from a displayed HTML table in which each row has a checkbox. The IDs are unique identifiers for each row.

Array
(
    [checkbox] => Array
        (
            [0] => 001403166
            [1] => 001407509
            [2] => 001407541
        )
)

I want to gather the IDs into variables so that I can go back and pull the full rows from mysql for processing (e.g., delete, etc.) -- something like ~

$var1 = 001403166
$var2 = 001407509
$var3 = 001407541

I haven't a clue as to how to do this (not unusual), or even what it is called so that I can find information about it. Thanks for any help!

+5  A: 

The extract function should do almost what you're asking :

int extract  ( array $var_array  [, int $extract_type = EXTR_OVERWRITE  [, string $prefix  ]] )

Import variables from an array into the current symbol table.


Something like this, I suppose, should do the trick :

extract($_POST['checkbox'], EXTR_PREFIX_ALL , 'var');

Only thing is that you'll get variables called $var_0, $var_1, $var_2 :

  • counting from 0, and not from 1, as the keys in $_POST['checkbox'] start from 0.
  • and with a '_' between $var and the number.


Note : before using extract, you should make sure $_POST['checkbox'] only contains "clean" data !

Pascal MARTIN
I have used extract() previously, but I was not familiar with its real capabilities. Your solution works very nicely. Thanks!
dave
You're welcome :-) Have fun ! *(but don't forget that using `extract` can lead to troubles, if the data you're using it on is not clean/safe ! )*
Pascal MARTIN
+1  A: 

You might also consider imploding this array into a single string, and using your databases native IN() function to cut down on running a single request for each individual row.

$uniqueIDs = array(1111,1222,1333);
$idString  = implode(",",$uniqueIDs);

$query = "SELECT id, col1, col2 
          FROM tableName
          WHERE id IN ({$idString})";

This query will come out to be:

SELECT id, col1, col2 
FROM tableName 
WHERE id IN (1111,1222,1333)

Selecting rows having an ID found within that list.

Jonathan Sampson
Now, this I had not thought of! It is made to order for the type of thing that I'm working on. Thank you very much!
dave