Page 1 of 1

checkboxes and array

Posted: Tue May 15, 2007 6:53 am
by Gainax
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I have a slight problem. 

I have a form made up of checkboxes which is then stored in an array.

Code: Select all

?php

if (!isset($_POST['list'])) 
{
        echo "Please choose one of the following lists: <br><br>";
} else {
        echo "You selected the following lists:<br>";
        foreach ($_POST['list'] as $selected_list) 
	{
                echo $selected_list."<br>";
        }
} 

?>

<form name="form1" method="post" action="results.php">
<table width="300"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><input type="checkbox" name="list[]" value="List 1"></td>
    <td>List 1</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="list[]" value="List 2"></td>
    <td>List 2</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="list[]" value="List 3"></td>
    <td>List 3</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="list[]" value="List 4"></td>
    <td>List 4 </td>
  </tr>
</table>
<p>
  <input type="submit" name="Submit" value="Submit">
</p>
</form>
As you can see the array is $selected_list and displays those boxes that have been checked. what i want to know is, how do i then use this array to retrieve data from a database?

The array is made up of database table names, named list 1, list 2 etc.

I need the query to take the result of the array, for example, list 2 and list 3 may be in the array, and the query will select the rows ONLY from the lists in the array.

I'm not sure If i eed to do this dynamically, which is why i'm not sure.

Thanks[/b]


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue May 15, 2007 7:08 am
by volka
The sql query is just a string. To build that string from $selected_list you can use the concatenation operator.

Posted: Tue May 15, 2007 7:09 am
by CoderGoblin
If I understand your question... something like the following should work

Code: Select all

$tablenames=implode(',',$_POST['list']);
$sql="SELECT * FROM $tablenames";
OK, you should potentially check all the values of $_POST first and ensure they are valid and allowable possibly by using a preg_match..

Posted: Tue May 15, 2007 7:14 am
by Gainax
Ok.

I'll give both a try and i'll repost the results.

Cheers