SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy how to get the result sorted in array format after count?

    Hi,

    I'm trying to create a tag cloud for my site. Now I have problem to sort the result in the array format as below:

    tags=array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27, 'apple2' => 37);
    What should I do after selecting the data in the query?


    $get_tags= mysql_query("SELECT keywords, COUNT(id) from mytable where keywords!='' group by keywords")
    or die(mysql_error());

    Thanks!

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $tags = array();
    $get_tagsmysql_query("SELECT keywords, COUNT(id) AS myCount from mytable where keywords!='' group by keywords") or die(mysql_error()); 
    while (
    $tag mysql_fetch_assoc($get_tags))
    {
       
    // example of $tag: array('keywords' => 'weddings', 'myCount' => 32);
       // so now you would want to get an array to use 'keywords' as key, and 'myCount' as value:
       
    $tags[$tag['keywords']] = $tag['myCount'];


    PS. it's better to use [ code ] and [/ code ] or [ php ] and [/ php ] for PHP code, instead of [ quote ] and [/ quote ]

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    PHP Code:
    $tags = array();
    $get_tagsmysql_query("SELECT keywords, COUNT(id) AS myCount from mytable where keywords!='' group by keywords") or die(mysql_error()); 
    while (
    $tag mysql_fetch_assoc($get_tags))
    {
       
    // example of $tag: array('keywords' => 'weddings', 'myCount' => 32);
       // so now you would want to get an array to use 'keywords' as key, and 'myCount' as value:
       
    $tags[$tag['keywords']] = $tag['myCount'];


    PS. it's better to use [ code ] and [/ code ] or [ php ] and [/ php ] for PHP code, instead of [ quote ] and [/ quote ]

    Thanks a lot ScallioXTX!

    Learnt something from you now


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
  •