SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Explode into an array with only unique items

    hi

    im trying to output some data i have in a table in my db, the row "tags" contains words seperated by a comma, so ive used the following code

    Code:
    <? $exploded_tags = explode(', ', $row_rsAllDisplayTags['tags']); ?>
    <? foreach ($exploded_tags as $tagexplodearray) { echo ("<a href='/Tags/$tagexplodearray'>$tagexplodearray</a>&nbsp; &nbsp;");} ?>
    this code explodes the data as it should and create an array with the data output. The problem is if there are 2 of the same tags in the table, then it will output the tag twice, what i want is it to only output unique tags...

    any ideas or help? TIA

    [ZmasteR]

  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $exploded_tags array_unique($exploded_tags); 

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could u explain how to add that to my current code,,,im kinda confused atm lol.
    thx

  4. #4
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It goes straight between the two lines you just posted.
    Edit:

    But it needs to be between <? ?> tags!

  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats what i tryed first, but doesnt work....

    any ideas?

  6. #6
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <? $exploded_tags = explode(', ', $row_rsAllDisplayTags['tags']); ?>
    <? foreach ($exploded_tags as $tagexplodearray) { echo ("<a href='/Tags/$tagexplodearray'>$tagexplodearray</a>&nbsp; &nbsp;");} ?>
    Becomes:
    PHP Code:
    <?php
    $exploded_tags 
    explode(', '$row_rsAllDisplayTags['tags']);
    $exploded_tags array_unique($exploded_tags);
    foreach (
    $exploded_tags as $tagexplodearray) {
        echo (
    "<a href='/Tags/$tagexplodearray'>$tagexplodearray</a>&nbsp; &nbsp;");
    }
    ?>

  7. #7
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well perhaps the problem im having is to do with the SQL??? i dunno, ive been tryin to get it to work for a while now,,,ive been usin php for quiet a while now,,,but this is just so,,,argghhh haha well below is the actual php page, with all the code, hav a look at the sql where i use COUNT maybe there is a problem there???

    Code:
    //db connection
    <?php require_once('../Connections/MYDB.php'); ?>
    //Create Record Set (only Dreamweaver code here)
    <?php
     mysql_select_db($database_MYDB, $MYDB);
     $query_rsAllDisplayTags = "SELECT tags, COUNT(title) FROM tblarticles GROUP BY tags";
     $rsAllDisplayTags = mysql_query($query_rsAllDisplayTags, $MYDB) or die(mysql_error());
     $row_rsAllDisplayTags = mysql_fetch_assoc($rsAllDisplayTags);
     $totalRows_rsAllDisplayTags = mysql_num_rows($rsAllDisplayTags);
     ?>
    //End Record Set
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <title>work darn u</title>
     <link href="../CSS/default.css" rel="stylesheet" type="text/css" />
     </head>
     <body>
     <div id="header">
     </div>
     <div id="content">
     <div id="maincloudall">
    //This determines how $tagstyle should be used, ex: if  the COUNT of title is > 8 the $tagstyle is = "cloudsmall2font" and $tagstyle is used later on.
    <?
     if ($row_rsAllDisplayTags['COUNT(title)'] > 0) $tagstyle = "cloudtinyfont";
     if ($row_rsAllDisplayTags['COUNT(title)'] > 3) $tagstyle = "cloudtiny2smallfont";
     if ($row_rsAllDisplayTags['COUNT(title)'] > 5) $tagstyle = "cloudsmallfont";
     if ($row_rsAllDisplayTags['COUNT(title)'] > 8) $tagstyle = "cloudsmall2font";
     if ($row_rsAllDisplayTags['COUNT(title)'] > 10) $tagstyle = "cloudmediumfont";
     if ($row_rsAllDisplayTags['COUNT(title)'] > 13) $tagstyle = "cloudmedium2font";
     if ($row_rsAllDisplayTags['COUNT(title)'] > 15) $tagstyle = "cloudmedium3font";
     ?>
    // variable $tabcound is equal to the count of "title"
     <? $tagcount = $row_rsAllDisplayTags['COUNT(title)']; ?>
    //Explode the row 'tags' from the db, explode at comma
     <? $exploded_tags = explode(', ', $row_rsAllDisplayTags['tags']); ?>
    //For each of the tags now exploded, create an array and echo....
     <? foreach ($exploded_tags as $tagexplodearray) { echo ("<a href='/Tags/$tagexplodearray' rel='tag' class='$tagstyle'>$tagexplodearray ($tagcount)</a>&nbsp; &nbsp;");} ?>
     <?php } while ($row_rsAllDisplayTags = mysql_fetch_assoc($rsAllDisplayTags)); ?>
     </div>
     </body>
     </html>
     <?php
     mysql_free_result($rsAllDisplayTags);
     ?
    TIA

  8. #8
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see array_unique in the code you posted. Did you make the change?
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  9. #9
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have added the code and it didnt work at all, so i had posted the original code just so u could hav a look


Bookmarks

Posting Permissions

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