Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/10592-category-sorting/
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]
Link to comment
https://forums.phpfreaks.com/topic/10592-category-sorting/#findComment-39535
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
Link to comment
https://forums.phpfreaks.com/topic/10592-category-sorting/#findComment-39538
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.