Random tags display

You have a suggestion for a future version of phpMyFAQ? Then post it here!

Moderator: Thorsten

Post Reply
mojo_jojo
Posts: 12
Joined: Tue Feb 27, 2007 5:23 pm
Contact:

Random tags display

Post by mojo_jojo »

Hi,

I have noticed that the tags in the current beta are being displayed in alphabetical order, and I have thought that it would be more practical and better if every time random tags are being displayed when a page is loaded.

Here is my getAllTags() function.

Code: Select all

function getAllTags($search = null, $limit = false)
    {
        global $DB;
        $tags = array();

        // Hack: LIKE is case sensitive under PostgreSQL
        switch ($DB['type']) {
            case 'pgsql':
                $like = 'ILIKE';
                break;
            default:
                $like = 'LIKE';
                break;
        }

        $query = sprintf("
            SELECT
                tagging_id, tagging_name
            FROM
                %sfaqtags
                %s
            ORDER BY tagging_name",
            SQLPREFIX,
            (isset($search) && ($search != '') ? "WHERE tagging_name ".$like." '".$search."%'" : '')
            );

        $i = 0;
        $result = $this->db->query($query);
        
//changes start here

        if ($result) {
        	while ($row = $this->db->fetch_object($result)) {
        		$allTags[$row->tagging_id] = $row->tagging_name;
        	}
        }

        $numberOfItems = $limit ? PMF_TAGS_CLOUD_RESULT_SET_SIZE : $this->db->num_rows($result);
        
        if ($numberOfItems < count($allTags)) {
        	for ($n = 0; $n<$numberOfItems; $n++) {
        		$valid = false;
        		while (!$valid) {
        			$rand = rand(1, count($allTags) + 1);
        		    if (!isset($soFar[$rand])) {
        		    	if (isset($allTags[$rand])) {
        		    		$valid = true;
        		    		$soFar[$rand] = '';
        		    		$tags[$rand] = $allTags[$rand];
        		    	}
        		    	
        		    }
        		} 
        	}
        }
        else {
        	$tags = $allTags;
        }
        return $tags;
    }
Best regards,

Georgi
Thorsten
Posts: 15559
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi Georgi,

great idea! I'll add it tonight into CVS. Thanks a lot!

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
Thorsten
Posts: 15559
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

it didn't worked here... did I missed something?

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
mojo_jojo
Posts: 12
Joined: Tue Feb 27, 2007 5:23 pm
Contact:

Post by mojo_jojo »

Hello,

It didn't work at all? I've tried it on the last version from the CVS (copy-pasted the function in Tags.php) and it was working. Have you tested it on a database with less tagwords than PMF_TAGS_CLOUD_RESULT_SET_SIZE? In this case all tagwords are displayed as they are obtained from the db, ordered alphabetically. That's why I changed it a bit:

Code: Select all

else {
        	shuffle($allTags);
        	$tags = $allTags;
        }
In this way it should work for all cases.
Best regards,

Georgi
Thorsten
Posts: 15559
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

I'll take a look the next days.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
mojo_jojo
Posts: 12
Joined: Tue Feb 27, 2007 5:23 pm
Contact:

Post by mojo_jojo »

Ok, it should work.

Btw thanks for the friendly mention in the devblog:)
Best regards,

Georgi
Thorsten
Posts: 15559
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

okay, it's working now. I made a copy&paste mistake. :-)

Your code is in checked into CVS.

Thank you!

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
Post Reply