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
Share on other sites

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)

Link to comment
Share on other sites

i want to arrange all article in list on categories they belong...

in simple word, i need to select all article from DB and for each article i have to explode the categories

and just check if this article belong to the category id that in the url (index.php?cat=5)

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.