Jump to content

Recommended Posts

I'm trying to configure a search-function on my site, so that users can select one or several tables to search from, using checkboxes.

[code]$table=$_GET['table'];

$query = "SELECT * FROM $table WHERE $kolumn LIKE \"%$trimmed%\" AND approved='yes'
  ORDER BY name ASC";[/code]

The value table is currently set by

[code]<select name="table">
  <option value ="tcg_dex_ex_legend_maker">Ex Legend Maker</option>
  <option value ="tcg_dex_ex_delta_species">Ex Delta Species</option>
  <option value ="tcg_dex_ex_unseen_forces">Ex Unseen Forces</option>
  </select>[/code]

But rather then just selecting one table, I'd like to use checkboxes instead.

[code]
<input type="checkbox" name="table" value="tcg_dex_ex_legend_maker">Ex Legend Maker<br>
<input type="checkbox" name="table" value="tcg_dex_ex_delta_species">Ex Delta Species<br>
<input type="checkbox" name="table" value="tcg_dex_ex_unseen_forces">Ex Unseen Forces<br>[/code]

Needless to say, using checkboxes like this, will only result in one of the options being used. How should I configure my query to recieve several values into $table?

Link to comment
https://forums.phpfreaks.com/topic/10108-checkbox-search/
Share on other sites

[code]<input type="checkbox" name="table[]" value="tcg_dex_ex_legend_maker">Ex Legend Maker[/code]

Like that? For some reason, it won't display any results at all. Are there any other changes in the code that I need to do?

for example, $table=$_GET['table'];

still recieves its value from "table", not "table[]", right? (or rather, "table%5B%5D" as it is generated in the url).
Link to comment
https://forums.phpfreaks.com/topic/10108-checkbox-search/#findComment-37631
Share on other sites

$table = $_GET['table'];

if you have 3 checkboxes selected then $table is your array of those three values.

echo $table[0]; // echos the first value
echo $table[1]; // echos the 2nd value
echo $table[2]; // echos the 3rd value

so in your sql query if you did

select * from $table[0] where blah=blah

or whatever, it would get the results from the first table that you checked. Obviously you can use a loop to loop through each one, or use a join
Link to comment
https://forums.phpfreaks.com/topic/10108-checkbox-search/#findComment-37633
Share on other sites

I think I've gotten the array to work now, even though it selects the last option i check instead of the first (and it still won't select more then one table).

Anyway, I'm not that versed in looping queries or using join's. Are there any (newbie-friendly) documentation written about this?
Link to comment
https://forums.phpfreaks.com/topic/10108-checkbox-search/#findComment-37642
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.