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
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
Link to comment
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.