Jump to content

Archived

This topic is now archived and is closed to further replies.

samsbc12

Category Sorting

Recommended Posts

So let me see if i can explain this what i want to do is go through my table and pull all categories that are not doubles and then sort them alphabetically.

[code]<?
                  $username="******";
                  $password="******";
                  $database="******";
                  
                  mysql_connect(localhost,$username,$password);
                  @mysql_select_db($database) or die( "Unable to select database");
                  $query="SELECT * FROM links";
                  $result=mysql_query($query);
                
                  //Count Number of rows in table
                  $num=mysql_numrows($result);
                      
                  //Close Connection
                    mysql_close();
                  //loop through
                  //Echo categories
                $i=0;
                
                  while ($i < $num) {
                      $c = $i +1;
                    $category=mysql_result($result,$i,"category");
                   while ($c < $num) {
                    $category2=mysql_result($result,$c,"category");
                         if($category == $category2){
                            $double = "true";        
                        }
                        $c++;
                        }
                     if($double != "true"){    
                       echo "<href =\"resouces.php?cat=$category\">$category</a>";
                    }
                      $double ="";
                    
                 $i++;
                }
                  
                //End Category Echo
[/code]

that will echo that categories, I realize I will probably have to store them in some kind of list then sort any suggest as to where to go from here?

Share this post


Link to post
Share on other sites
An easier way is
[code]
$res = mysql_query("SELECT category, COUNT(*) as total
        FROM links
        GROUP BY category
        HAVING total = 1");

while (list($category, $total) = mysql_fetch_row(res)) {
    echo "<a href =\"resouces.php?cat=$category\">$category</a><br>";
}
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=377655:date=May 27 2006, 04:28 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 27 2006, 04:28 PM) [snapback]377655[/snapback][/div][div class=\'quotemain\'][!--quotec--]
An easier way is
[code]
$res = mysql_query("SELECT category, COUNT(*) as total
        FROM links
        GROUP BY category
        HAVING total = 1");

while (list($category, $total) = mysql_fetch_row(res)) {
    echo "<a href =\"resouces.php?cat=$category\">$category</a><br>";
}
[/code]
[/quote]


what would that replce in my code, i am having a little trouble

I got it working excpet it only displays categories who only have one item in it as opposed to displaying only one category once


I took out HAVING total = 1");
and it works great now thanks a bunch

Share this post


Link to post
Share on other sites

×

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.