Jump to content

Archived

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

tjhilder

multiple categories using $var

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

Share this post


Link to post
Share on other sites
Not sure what you're trying to do but, try something like this:

[code]<?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");

?>[/code]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.