Jump to content

Sorting by category...


geudrik

Recommended Posts

Well, heres my issue.  I have a database with multiple rows.  One of the columns is 'category'.  What I need to do is pull all info out of the database and organize it by category.  Where should I begin looking on how to get this done?

 

I got as far as a sorty by argument in my sql, or maybe a for each l($row[category]) 

 

Thoughts?  And it's not just sorting it by category, I need to physically pull chunks of the database out but only where they fall under each category.

Link to comment
Share on other sites

So you need something like this?

 

$myquery = mysql_query("SELECT * FROM tablenamehere WHERE category='cat2'") or DIE(mysql_error());

while($r=mysql_fetch_array($myquery))
{
$id=$r["id"];
$value=$r["value"];
$value=$r["value"];

echo "$value";
}

 

Quicky to get the idea to you, basically grab all information with a certain category. Then you just set variables for whatever information you want from each from. Hope this is what you were looking for.

Link to comment
Share on other sites

<?php
$query = "SELECT name, description, category, upc FROM products ORDER BY category, name";
$result = mysql_query($query);
$all = array();
while ($row = mysql_fetch_assoc($result)){
  $all[$row['category']][$row['upc']] = $row;
}
print_r($all)
?>

Link to comment
Share on other sites

wow :) thank you all for the volume of responses.  Greatly appreciated.

 

A little more info...

The table is set up like...

 

id | title | content | category

 

given that, I thought about using ORDER BY in my sql, but that really doesnt work, as I need to be able to pull everything from the table, sort everything from each row by category, and then be able to generate a link on each one <a href="view.php?id=$id">$title</a>

 

so, based on what I've seen from you guys thus far, I'm thinking something along the lines of a combination of F1Fan's and aebstract's suggestions.  Yes?

 

Link to comment
Share on other sites

addition: each 'group' of rows that fall into the same category need to be placed in different places within the page, hence why I'm thinking they should be their own entity. 

 

Now, I realize that I COULD simply make numerous queries, one for each category, but it seems like I should be able to have something a little more modular...

Link to comment
Share on other sites

My suggestion should make this fairly simple.

<?php
$query = "SELECT * FROM products ORDER BY category, title";
$result = mysql_query($query);
$all = array();
while ($row = mysql_fetch_assoc($result)){
  $all[$row['category']][$row['id']] = $row;
}
/* If you want to select one category... */
$cat = "somecat";
foreach ($all[$cat] as $item){
  echo "<a href=\"view.php?id={$item['id']}\">{$item['title']}</a>";
}

/* Or, loop through all categories... */
foreach ($all as $cat=>$vals){
  echo "Category $cat...<br>";
  foreach ($vals as $item){
    echo "<a href=\"view.php?id={$item['id']}\">{$item['title']}</a>";
  }
}
?>

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.