Jump to content

Help with queries in a guide section.


Wizpig

Recommended Posts

Hi!

 

I'm working on a simple guide section, for practicing my MySQL/PHP. But now I'm kind of stuck.

I've got two tables:

guide_topics

+----+-------+

| id | name    |

+----+-------+

|  1 | Car      |

+----+-------+

|  2 | Boat    |

+----+-------+

 

guides

+----+----------+---------------------+-----------------------+

| id  | topic_id  | name                      | content                    |

+----+----------+---------------------+-----------------------+

|  1  |     2 | How to drive a boat  | Today we are going...  |

+----+----------+---------------------+-----------------------+

|  2  |     1 | How to drive a car    | Today we are going...  |

+----+----------+---------------------+-----------------------+

Here's my question: How can i list up the the guides in the following waya?:

-- Car --

* How to drive a car

 

-- Boat --

* How to drive a boat

 

I've comed so far:

$res_topics = mysql_query("SELECT * FROM `guide_topics` ORDER BY `id` ASC");

 

while ($arr_topics = mysql_fetch_array($res_topics))

{

$output .= " -- ".$arr_topics["name"]." --<br>";

}

echo $output;

die();

 

I've thought about JOINS, but I'm not good with using those in practice, so I hope maybe you guys got a solution to my problem.

Thanks for all answers!

 

- Wizpig

Link to comment
https://forums.phpfreaks.com/topic/178872-help-with-queries-in-a-guide-section/
Share on other sites

You should be able to do someting along the lines of...

 

SELECT g.*, gt.name AS topic FROM guides g
JOIN guide_topics gt ON g.topic_id=gt.id 
ORDER BY `id` ASC

 

The to display it in the format you described, you'd then use something like...

 

$topic = "";
while($row = mysql_fetch_assoc($result)) {
   if($row['topic'] != $topic) {
      echo $row['topic'];
      $topic = $row['topic'];
   }
   echo $row['name'];
}

 

 

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.