mysql / php array the category (quick help pleas)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

mysql / php array the category (quick help pleas)

Post by spamyboy »

PHP / MySql code:

Code: Select all

$query=$galleryCMS->db->query("
SELECT galleries.gallery_id, categories.category_title, galleries.gallery_title
FROM galleries JOIN categories_relations USING (gallery_id) JOIN categories USING (category_id)
WHERE gallery_id='2'
");
while($row=$galleryCMS->db->fetch_array($query)){
print_r($row);
}
Result:

Code: Select all

Array ( [gallery_id] => 2 [category_title] => apps [gallery_title] => test ) Array ( [gallery_id] => 2 [category_title] => games [gallery_title] => test )
And how I imagine correct result, would be:

Code: Select all

array([gallery_id] => 2 [category_title] => apps,[gallery_id] => 2 [category_title] => games) gallery_title
Thank you in advance.
p.s. people said me to use GROUP_CONCAT, but it's alitle bit to dificul on how to use it since I didnt managed to find a tutorial on it, but anyway, I would like to do it without creating another while session (if that is possible), thank you in advance.
edited: seems that GROUP_CONCAT is alitle bit off-topic stuff here, it might be used if you put all categories in one row (by id or title), but in my situation I'm using relations table (as you can probaly see from query that is given), that is db structure looks like:

Code: Select all

categories:
category_id | category_title
categories_relations:
category_id | gallery_id
galleries:
gallery_id, gallery_title
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

anyhelp?
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

I don't get what you want... you want to have an array with two values on the same key (gallery_id and category_title)?
If that's correct, it is not posible :S
Post Reply