GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Webmaster Q & Fuckin' A (https://gfy.com/forumdisplay.php?f=27)
-   -   PHP Question (https://gfy.com/showthread.php?t=919030)

adulttemps 07-30-2009 11:54 PM

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

Ferrishyn 07-31-2009 12:32 AM

This should work for ya:
Quote:

$res = mysql_query('select distinct(category) from content ORDER BY category');

Ferrishyn 07-31-2009 12:35 AM

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.

webboy21 07-31-2009 03:36 AM

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

adulttemps 07-31-2009 07:46 AM

Yea the database design is from an affiliate dump, they dump the categories seperated by commas.

Kaylum 07-31-2009 06:08 PM

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>';
          }
       }
    }    
    



Kaylum 07-31-2009 11:20 PM

update:

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

peace


All times are GMT -7. The time now is 11:18 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123