Forum Moderators: coopster

Message Too Old, No Replies

Help with query, breaking up results

         

adammc

2:31 am on Sep 11, 2007 (gmt 0)

10+ Year Member



Hello,

I have a table (Linkspage) which stores my links page data:
link_id
website_name
category
url
description

I have only 3 'categories' and would like to perform a query that echos all the links where:
category=category1
category=category2
category=category3

Is this possible with only 1 query or do I just write three different queries?

Habtom

5:06 am on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can have it in one query:

SELECT link_id, website_name . . . WHERE category=category1 OR
category=category2 OR category=category3

While displaying if you are grouping them by category, you can categorize them while looping for display.

adammc

5:25 am on Sep 11, 2007 (gmt 0)

10+ Year Member



HI Habtom, thanks for the reply.
You kinda lost me with the last bit :(

If I want to diplay like so:

Category1:
link
link

Category2:
link
link

Category3:
link
link

while($r = mysql_fetch_assoc($query))
{

// how do I set the different echo statements for each of the 3 categories?

}

Habtom

5:58 am on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$dis_category .= "<table>";
while($r = mysql_fetch_assoc($query))
{
// Grouping by category
if ($cur_cat <> $r['category']) {
$cur_cat = $r['category'];
$dis_category .= "<tr><td><strong>". $cur_cat ."</strong></td></tr>";
}

// Display Links
$dis_category .= "<tr><td> $r['link'] </td></tr>";
}
$dis_category .= "</table>";
?>

remember, there is always a better way of doing things.

adammc

9:55 am on Sep 11, 2007 (gmt 0)

10+ Year Member



Thanks Habitom,
So I can learn from this... What does this actually mean:

if ($cur_cat <> $r['category']) {

Habtom

9:57 am on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want the category to be displayed once:

if ($cur_cat <> $r['category']) {

so if they are not equal assign, and display.

that is the whole point.

Sry, that was a quick response.

adammc

11:02 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



Thank you sooooo much Habtom, it workss a treat!
I also managed to learn a new trick or two ;)