Jump to content

Recommended Posts

Hello, first time poster here, looking for some Mysql query advice. I want to 'categorize' my results by a given column.

My table is something like

    Menu

title

description

catid

 

The query I tried (several different ways) was essentially SELECT * FROM menu GROUP BY catid

 

The problem is, with this code it only outputs 1 result for each value in the CATID column. What i want it to do is show all of them.

Link to comment
https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/
Share on other sites

Sorry, I don't know the exact terminology for everything which is probably what is causing my problems.

 

I need a query that will grab all the rows in the database and group the rows with identical values in the column named 'catid'.

 

Here is the code i am using.

$query = "SELECT * FROM menu GROUP BY catid";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);


  while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
    echo "<h3>{$row['category']} </h3>"."<p>{$row['title']} -" .
         "{$row['description']} <br />" .
         "Price : $"."{$row['price']} <br /><br /></p>";
}

This does basically what I want but it only shows one result per catid. I want to show everything in the table but still maintain this overal idea. If anyone can help or point me in the right direction I would greatly appreciate it.

I had tried group_concat also and it still returns only one row. Maybe I am going about this the wrong way. Originally I was going to use a different query for each category.

SELECT * FROM menu WHERE catid=1

produces the results I want, but doing this query over and over for each row in the catid column seems wrong to me and there must be a better way?

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.