Jump to content

[SOLVED] mysql select help


AviNahum

Recommended Posts

hey,

ummm i make an articles system, when i add a new article, i choose which categories i want to put in this article

(i can choose more than one category)...

when i insert the article into DB i just implode all checkboxes and insert it like this (1,4,6,3) each number is an category id...

 

then i have to dispaly it on the index,

the url is "index.php?cat=5" that mean we are looking for articles on category id 5...

when i choose only 1 category for 1 article its simple, usually i using this "SELECT * FROM article WHERE category="$_GET['cat']"....

but at this time i have to show each article in few categories and i have no idea how to select it...

 

i tried this code but it's wont work (i lost my self into this code and i have no idea what i did here)

public function cat($id) {
global $cms, $DB;

$DB->query("SELECT * FROM cms_articles");
$c = $DB->fetch_row();

foreach ($c as $a)
{
$cats = explode(',', $a['cats']);

if (in_array($id, $cats))
{
	$DB->query("SELECT * FROM cms_articles WHERE id={$a['id']}");
	$data = $DB->fetch_row();
}
}

return <<<EOF

{$data['content']}

EOF;
}

 

i need to show all article in this category

 

if someone can explain me in simple words what i should to do i I'll thank him...

 

i hope you understend what i trying to do cuz it's difficult to me to explain it in english

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/165565-solved-mysql-select-help/
Share on other sites

im guessing you mean if u tag an article for 'this' cat and 'that' cat, that u want it to show up in both.

 

you could prolly use something like this:

 


$id=$_GET['id'];

$res=mysql_query("SELECT * FROM table WHERE `cat_id` LIKE '%$id,'");

 

lemme know

You could concatenate the categories onto the url with a delimiter, and then explode them for the query and use IN() for the selection.

 

Example, say these are checkboxes (name(value))

[] food(1)

[] hiking(2)

[] surfing(3)

 

You would then get the values of all selected checkboxes, and concatenate them together.  So if I selected food and surfing, and a delimiter of '|' I would have a string like:

1|3

which I could append to the url such as: yourpage.php?cat=1|3

 

(a better idea might be to do it with binary values so that you just send a single number)

 

Anyway, on your page with the sql, you would explode on |, and make the query:

SELECT * FROM cms_articles WHERE id IN (1,3)

Archived

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

×
×
  • 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.