1

Hi,

I am able to use checkboxes to select specific columns of the table. I am not able to generate the corresponding select statement. I can use if-else statement to generate a large number of select statements to retrieve specific data. However, with large number of checkboxes the number of permutations becomes very large. How can I have one or small number of statements which can take care of all the possible options.

I have a table of cars with id,year,name columns. In order to retrieve different types of information ( such as Id and name only or name and year only or all the three etc...) I can use three checkboxes. But I will need 6 if-else statements with 6 select statements to take into account all possibilities. If I have more checkboxes the possibilities become very large. What should I do to use one or very few select statements.

Thank you very much

flag

1 Answer

0

You can try something like this.

HTML code:

<form method="post" action="">
<input type="checkbox" name="field[]" value="id"> ID<br>
<input type="checkbox" name="field[]" value="name"> Name<br>
<input type="checkbox" name="field[]" value="year"> Year<br>
<input type="submit" value="Submit">
</form>

PHP code:

if (count($_POST['field'])>0)
{
 $sql = "SELECT ";
 foreach($_POST['field'] as $field)
    $sql.=$field.",";
 $sql = substr($sql, 0, strlen($sql)-1);
 $sql.=" from cars";
 echo $sql;
}
link|flag

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.