Jump to content


Photo

Category Sorting


  • Please log in to reply
2 replies to this topic

#1 samsbc12

samsbc12
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 May 2006 - 05:16 PM

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.

<? 
                  $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

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?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 27 May 2006 - 08:28 PM

An easier way is
$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>";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 samsbc12

samsbc12
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 May 2006 - 08:44 PM

[!--quoteo(post=377655:date=May 27 2006, 04:28 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 27 2006, 04:28 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
An easier way is
$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>";
}
[/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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users