Jump to content

Multiple select from database where clause


adamf

Recommended Posts

Hi,

 

I have a website with a database full of category names...I want to be able to chose lets say 5 of these categories and use a select from database clause where I could simply enter the category names I want to use but am not sure how I would go about doing it.

 

I can get the list to display just the Aprilia category using the code below:

 

$data1->q("SELECT * FROM categories WHERE cat_name = 'Aprilia' ORDER BY cat_name asc");

 

However...how could I get my site to display Aprilia, Ducati, Honda, Suzuki, Yamaha?

 

Adam

Use the implode() function on an array to create a list of possible values to use with the IN syntax in your query:

 

<?php
$cats = array('Aprilia', 'Ducati', 'Honda', 'Suzuki', 'Yamaha');//an array of the possible categories. I assume this might depend on form input. 
$data1->q("SELECT * FROM categories WHERE cat_name = IN(."implode(',',$cats)".) ORDER BY cat_name asc");
?>

IF you have an array of strings the implode needs more than just a comma

<?php
$cats = array('Aprilia', 'Ducati', 'Honda', 'Suzuki', 'Yamaha');
$catlist = implode ("','", $cats);

$data1->q("SELECT * FROM categories WHERE cat_name = IN ('$catlist') ORDER BY cat_name asc");


Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.