Jump to content


Photo

multiple categories using $var


  • Please log in to reply
2 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 22 September 2006 - 11:18 PM

Hi,

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

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

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

I tried using functions like
explode()
list()
foreach()
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

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 22 September 2006 - 11:42 PM

Not sure what you're trying to do but, try something like this:

<?php

$string = "1,2,3";
$cats = explode(",", $string);

$query = $db->query("SELECT * FROM news WHERE cat_id='$cats[0]' OR cat_id='$cats[1]' OR cat_id='$cats[2]' ORDER BY date LIMIT 5");

?>

PHP Ninja

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 September 2006 - 12:03 AM

You can do that with implode() and explode():
<?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";
?>

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users