Jump to content

multiple categories using $var


tjhilder

Recommended Posts

Hi,

how would I go about using a [code]$categories = '1,3,4';[/code]

then splitting them up so they come out with a mysql query like this:

[code]SELECT * FROM news WHERE cat_id='1' AND cat_id='3' AND cat_id='4' ORDER BY date LIMIT 5[/code]

I tried using functions like [code]explode()
list()
foreach()[/code] etc, but I haven't found anything that works yet.

can anyone give me some advice on how I would need to do this?

--
TJ
Link to comment
https://forums.phpfreaks.com/topic/21730-multiple-categories-using-var/
Share on other sites

You can do that with implode() and explode():
[code]<?php
$qtmp = array();
$categories = '1,3,4';
$tmp = explode(',',$catagories);
foreach($tmp as $v)
  $qtmp[] = "cat_id = '" . $cats[$v] . "'";
$q = "select * from news where " . implode(' or ',$qtmp) . " order by date limit 5";
?>[/code]

Ken

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.