PHP Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adulttemps
    Confirmed User
    • May 2002
    • 320

    #1

    PHP Question

    I have the following function

    PHP Code:
    function categories () {
        
        
        
        $res = mysql_query('select category from content ORDER BY category');
        while($rows = mysql_fetch_array($res)){
        
            
            $a = array();
            
            foreach ($rows as $row)
            {
            $foo = explode(',', $row);
    
            $a = array_merge($a, $foo);
            
    
            echo '<li><a href="/videos/'.$a[0].'" title="'.$a[0].'">'.$a[0].'</a></li>';
                    
            }
        }    
        
    } 
    
    It selects the categories out fine, but how do i get it to only display unique categories?

    This prints each category everytime its listed in the db I cant use select distinct because some categories contain more than one category : example anal, amateur, blowjob
    Hillipino Money with Cams
  • Ferrishyn
    Registered User
    • Nov 2005
    • 28

    #2
    This should work for ya:
    $res = mysql_query('select distinct(category) from content ORDER BY category');

    Comment

    • Ferrishyn
      Registered User
      • Nov 2005
      • 28

      #3
      That's what I get for posting this early in the morning, I didn't read it ;)

      You need to separate that out so its not in the same field like that. You could also read through all of them explode them by the commas and unique that array.
      Last edited by Ferrishyn; 07-30-2009, 11:36 PM.

      Comment

      • webboy21
        Confirmed User
        • Nov 2004
        • 573

        #4
        well your problem is bad database design ;) basically what ferrishyn said....his solution will work too....but it's better to start at the source and build up your database the right way
        Available for: CSS | XHTML | PHP | MySQL | Webdesign

        Comment

        • adulttemps
          Confirmed User
          • May 2002
          • 320

          #5
          Yea the database design is from an affiliate dump, they dump the categories seperated by commas.
          Hillipino Money with Cams

          Comment

          • Kaylum
            Confirmed User
            • Aug 2005
            • 465

            #6
            yo, a quick hack to get your stuff working...

            note: that select statement gives the impression that the categories are already stored in the database as one category per row, but i guess not.

            PHP Code:
            function categories () {
                
                
                
                $res = mysql_query('select category from content ORDER BY category');
                while($rows = mysql_fetch_array($res)){
                
                    
                    $dirty_array = array();
                    $clean_array = array();
            
                    foreach ($rows as $row)
                    {
                    $foo = explode(',', $row);
            
                    $dirty_array = array_merge($dirty_array, $foo);
                             
                    }
            
                    // loop through the crappy array
                    foreach ($dirty_array as $element)
                   {
                        // quick hack to bypass duplicates
                       if (!in_array($element,$clean_array)
                      {
                                // push the category element into array
                                array_push($clean_array,$element); 
                                
                                // quick hack to print out links without re-looping through clean-array
                                echo '<li><a href="/videos/'.$category.'" title="'.$category.'">'.$category.'</a></li>';
                      }
                   }
                }    
                
            } 
            
            -- "You can't stop ingenuity, trust me.."

            Comment

            • Kaylum
              Confirmed User
              • Aug 2005
              • 465

              #7
              update:

              replace ($category) in the echo statement to ($element) which holds the category.

              peace
              -- "You can't stop ingenuity, trust me.."

              Comment

              Working...