View Full Version : Page Tagging System - Build + Manipulate Array

09-02-2006, 12:50 AM
Hi all,

First a description of what I'm trying to do - basically it's a personal bookmarking system that lets you tag sites with whatever descriptive words you want in the same way as del.icio.us. I currently have a single database table with 3 columns : URL of page; title of the page; and tags applied to page, stored as a single string with the tags seperated by spaces.

When tagging a new page, I want to show a list of individually clickable, previously used tags to make the process quicker.

With the above table, I'm fairly sure that means I have to go through this process:

1. Retrieve the entire contents of the tags column from the database.
2. Put it all into an array.
3. Delete duplicate tags from that array.
4. Explode the array.
5. Echo the individual substrings one by one.

With the help of Brandoe I have the code for steps 4 and 5, but I'm having trouble with the array part. This is what I'm trying to use:

// Get raw tags data
$raw = mysql_query ("SELECT tags FROM bookmarks");

// Build an array
while ($a = mysql_fetch_array($raw)) {
$taglist = array ($a['tags']);

// Remove duplicates from array and explode it
$tags = array_unique(explode(" ", $taglist));

// Count tags for use in loop later on
$numtags = count ($tags);

Clearly not the correct idea since this simply echoes 'Array' when I run the loop Brandoe helped me with:

for ($i=0; $i < $numtags; $i++) {
echo $tags[$i];

Any ideas?


09-02-2006, 07:19 AM
The problem is in your assignment of $taglist. You are assigning the results of mysql_fetch_array() to it, but you are encasing it as an array; I'm not quite sure why. For every row returned from your table, you're making the assignment $taglist = array($a['tags']), so $taglist ends up an array of arrays, looking like this:

$taglist[0] = (0 => tagvalue)
$taglist[1] = (0 => tagvalue)
$taglist[2] = (0 => tagvalue)
$taglist[3] = (0 => tagvalue)
etc. etc. etc.

It seems like you really want this:

$taglist[0] = tagvalue
$taglist[1] = tagvalue
$taglist[2] = tagvalue
$taglist[3] = tagvalue
etc. etc. etc.

You can simply add a variable to keep track of $taglist's index and increment it in your while loop.

// Build an array
$index = 0;
while ($a = mysql_fetch_array($raw)) {
$taglist[$index] = $a['tags'];

And then remove the call to the explode() function-- if all you want is an array of unique values, you'll get it without explode. Explode is for strings anyway; not arrays.

09-02-2006, 08:14 PM
Thanks for your help, but in retrospect I've got a bit mixed up, focussing on arrays when I don't think there is any need to... I should ammend my initial process:

1. Retrieve the entire contents of the tags column from the database.
2. Put it all into a single string.
3. Explode the string.
4. Delete duplicate tags from that string.
5. Echo the individual substrings one by one.

:thumbsup: Given this, I ended up using...

$raw = mysql_query ("SELECT * FROM bookmarks") or die(mysql_error());
while ($a = mysql_fetch_array($raw)) {
$taglist .= $a['tags'];
$tags = array_unique(explode(" ", $taglist));

This is working (despite the use of 'array_unique' on a non-array object?)

I've one final question - when I echo out the list of tags they are not in alphabetical order, is there a function for alphabetizing an exploded string/array?

09-02-2006, 11:59 PM
Hey if your method works, go for it! :)

FYI the explode() function returns an array, that's why array_unique() is working for you.

You can also sort an array using asort(), or arsort() (for descending).