Web Analytics Made Easy -
StatCounter Reassigning Array Keys, Removing Blanks - CodingForum

Announcement

Collapse
No announcement yet.

Reassigning Array Keys, Removing Blanks

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reassigning Array Keys, Removing Blanks

    Some of you will likely remember me trying to build a page tagging system a while back (original thread here) which at the time stumped me, so I gave it a rest.

    I'm back on it and approaching from a slightly different angle which is basically working, but I have a few issues. I'll walk you through the whole process so you can get a better idea of the whole thing and where gremlins are creeping in to the system.

    The tags for each bookmarked page (up to three) are stored in seperate columns in the database.

    PHP Code:
    //Retrieve tags and combine them into a single string
    $rawtags mysql_query ("SELECT tag1, tag2, tag3 FROM bookmarks") or die(mysql_error());
    while (
    $array mysql_fetch_array($rawtags)) {
        
    $taglist .= $array['tag1'].',';
        
    $taglist .= $array['tag2'].',';
        
    $taglist .= $array['tag3'].',';

    This returns a long list with each tag seperated by a comma. Note the blank values in places, this is where a page has only been tagged with one or two tags, often resulting in tag3 being an empty column. This causes problems later on.

    All Tags CSV:
    Design,Tools,,Jobs,Search Engine Optimisation,,Shopping,Product,Health & Fitness,Tools,Design,Writing,Tools,Eye Tracking,Usability,Tools,Link Building,Website Management,Tools,Search Engine Optimisation,,Tools,Usability,Accessibility,
    PHP Code:
    // Next, turn it back into an array and remove duplicates
    $tags array_unique(explode(","$taglist)); 
    This returns the following. It's getting there, but the blanks are now taking up their own slots in the array, and additionally because I stripped many of the values out, the array has many keys which simply don't exist anymore.

    Raw Unique Tags:
    Array ( [0] => Design [1] => Tools [2] => [3] => Jobs [4] => Search Engine Optimisation [6] => Shopping [7] => Product [8] => Health & Fitness [11] => Writiing [13] => Eye Tracking [14] => Usability [16] => Link Building [17] => Website Management [23] => Accessibility )
    PHP Code:
    // Finally echo each tag individually (so each one can be a seperate link)
    $numtags count($tags);
    for (
    $i=0$i $numtags$i++) { 
        echo 
    $tags[$i].', ';

    Because I'm relying on the key value to go through the whole array and comparing it to the total number of unique tags I end up with an incomplete list. In this case there are 14 unique tags, but the keys go up to 23.

    List Of Processed Unique Tags:
    Design, Tools, , Jobs, Search Engine Optimisation, , Shopping, Product, Health & Fitness, , , Writiing, , Eye Tracking,
    Also, although not a fundamental problem, the multiple blank spaces that are getting echoed out are compromising the aesthetics of the final product.

    Has anyone got any suggestions on how I can solve this? Is there some way to reassign the keys of the array so that they run from 1-14? Or perhaps loop through the array without relying on counting the items?

    How about stripping those irritating blank values?

    Thanks for reading.
    Last edited by Pennimus; Sep 24, 2006, 12:21 PM.

  • #2
    Why won't you make a new table for the tags? That way you could have an "infinete" number of tags for each bookmark and you would not have to worry about your current problem.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

    Comment


    • #3
      As to removing the blanks from the array: use the array_filter() function (Filters elements of an array using a callback function) see http://nl2.php.net/manual/en/function.array-filter.php

      To re-index the array: use the array_values() function (returns all the values from the input array and indexes numerically the array) see http://nl2.php.net/manual/en/function.array-values.php

      So when you first use array_filter to get rid of the blanks and the array_values to renumber, you're on your way.

      Ronald
      RTFM is an almost extinct art form, it should be subsidized.

      Comment


      • #4
        Why won't you make a new table for the tags?
        Heh, I did try that at one point, but it caused as many problems. I assume you mean I would use the UNIQUE value on that table, thus eliminating any need to use array_unique later on?

        If you think it's a markedly better way to do it I will have another look at it ( I must admit it seems a lot cleaner).

        The database also has to be searchable in the other direction as well, meaning I will still have to store the tag information (albeit in a simple list rather than in seperate columns) on the main table, or do some sort of complicated combined table operation when searching.

        (On a side note, I actually don't mind limiting it to 3 tags anyway, because I find that anything more than this is essentially uneccessary. Allowing only three forces people to tag more intelligently and you end up with a more cohesive and easy to use system).

        Ronald, thanks for the info, I'll take a look at that now...

        EDIT:

        I've stumbled on a solution literally by accident.

        Thanks for the array_values function Ronald, that works.

        However, I was playing around with sort() after that, and that's when I found this:

        PHP Code:
        $rawtags mysql_query ("SELECT tag1, tag2, tag3 FROM bookmarks") or die(mysql_error());
            while (
        $array mysql_fetch_array($rawtags)) {
                
        $taglist .= $array['tag1'].',';
                
        $taglist .= $array['tag2'].',';
                
        $taglist .= $array['tag3'].',';
            }
            
            
        $uniquetags array_unique(explode(","$taglist));
            
        sort($uniquetags);
            
        $tags array_values($uniquetags);
            
        $numtags count($tags); 
        Now $uniquetags not only returns an ordered list, but also one that has had the keys reassigned...

        Raw Unique Tags:
        Array ( [0] => [1] => Accessibility [2] => Design [3] => Eye Tracking [4] => Health & Fitness [5] => Jobs [6] => Link Building [7] => Product [8] => Search Engine Optimisation [9] => Shopping [10] => Tools [11] => Usability [12] => Website Management [13] => Writiing )
        Then since the blanks are all contained in key [0], I simply started the for loop at 1 instead

        Maybe not the 'best' solution as far as proper coding goes but I'm happy.
        Last edited by Pennimus; Sep 24, 2006, 01:05 PM.

        Comment


        • #5
          If these "bookmarks" (I don't really know what you really want to do) are something that you assign a certain number (more than 1) of these "tags" from a set I would suggest you to use 3 tables: 1 table for the bookmarks, 1 for the tags and one table that would hold the relations between the bookmarks. This way any tag could be assigned to any amount of bookmarks and any bookmark could have any amount of tags.
          I'm not sure if this was any help, but I hope it didn't make you stupider.

          Experience is something you get just after you really need it.
          PHP Installation Guide Feedback welcome.

          Comment


          • #6
            If these "bookmarks" ... are something that you assign a certain number (more than 1) of these "tags" from a set...
            That's more or less it, yes. Think del.icio.us. It's meant to be an intuitive way to store webpages by tagging them instead of just putting them into a folder in your bookmarks. You can assign each page one or more tags from amongst those you've already used, or make up an entirely new tag if you want to.

            I would suggest you to use 3 tables: 1 table for the bookmarks, 1 for the tags and one table that would hold the relations between the bookmarks.
            That sounds great, and is pretty much how I imagined the optimal solution to work. Unfortunately, as you can probably tell, I'm no PHP expert yet, so my thoughts were that this kind of thing might be a little beyond me right now.

            I'm certainly willing to start reading up on it though - where should I begin reading about these kind of "inter-table relationships"?

            Thanks MM

            Comment

            Working...
            X