Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

    Page Tagging System - Build + Manipulate Array

    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:

    PHP Code:
    // 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:

    PHP Code:
    for ($i=0$i $numtags$i++) {
      echo 
    $tags[$i];


    Any ideas?

    --

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.
    PHP Code:
    // Build an array
    $index 0;
    while (
    $a mysql_fetch_array($raw)) {
      
    $taglist[$index] = $a['tags'];
      
    $index++;

    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.

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    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.

    Given this, I ended up using...

    PHP Code:
    $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?
    Last edited by Pennimus; 09-02-2006 at 10:27 PM.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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).

    http://us3.php.net/manual/en/function.asort.php


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •