0

Currently I am creating a news section for a website.

I have created it so that the news type is separated by a string E.g. category = ,2,4,6,

I don't normally use strings a whole lot, and my initial thought was to do something like this:

$query_listelements = "SELECT * FROM newsitems WHERE released = 1 AND (category = 2  OR category = 4 OR category = 6) ORDER BY date_rel DESC";

Clearly this won't work as I need to isolate / expand the string. so it needs to be something like:

$query_listelements = "SELECT * FROM newsitems WHERE released = 1 AND (category = strval(",2,")  OR category = strval(",4,") OR category = strval(",6,")) ORDER BY date_rel DESC";

I don't think the above is the right way to go out things either.

Any thoughts would be really valued!

4

1 回答 1

1

You may use it like below, sounds better to me.

$category= array ( 2, 4, 6);
$category = implode(',', $category); //Now it is a string like '2,4,6'
$query_listelements = "SELECT * FROM newsitems WHERE released = 1 AND (category IN ($category)) ORDER BY date_rel DESC";
于 2012-07-06T06:51:38.303 回答