tjhilder Posted September 22, 2006 Share Posted September 22, 2006 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 Quote Link to comment Share on other sites More sharing options...
Caesar Posted September 22, 2006 Share Posted September 22, 2006 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] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 23, 2006 Share Posted September 23, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.