SitePoint Sponsor

User Tag List

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

    Problem with Arrays...

    Hello!

    I've been having trouble with my PHP code, arrays to be more specific.

    PHP Code:
    <?

    include 'db.inc.php';

    function 
    get_tag_data() {

    $arr = array();

    $query "SELECT count, tag FROM tags";
    $result mysql_query($query) or die(mysql_error());

    $arr mysql_fetch_array($result); 

    return 
    $arr;

    }

    ?>
    Basically, I need my code to connect to the database (which it does fine), then select 'count' and 'tag' from the 'tags' table, then I need the code to display the arrays like the following:

    PHP Code:
    $arr = array('tag' => count); 
    and for multiple results:

    PHP Code:
    $arr = array('tag' => count'tag' => count'tag' => count); 
    but I'm baffled on how I'd accomplish this...

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:

    PHP Code:
    <?

    include 'db.inc.php';

    function 
    get_tag_data() {

    $arr = array();

    $query "SELECT count, tag FROM tags";
    $result mysql_query($query) or die(mysql_error());
    $arr = array();

    while( 
    $row mysql_fetch_array$result ) )
    {
      
    $arr[$row['tag']] = $row['count'];
    }

    return 
    $arr;
    }
    Though note if you have more than one 'tag' with the same name it'll write over itself in the array.

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, great. That works. Thank you.


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
  •