SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with arrays

    I am having trouble trying to pull my information out of MySQL in an array. I have tried it many different ways but I cannot seem to get it right. I am trying to make an unordered list and I have found a script from another post that will work for me if I can figure out this array issue. In the script you can see how the original array was created, hard coded

    PHP Code:
    <?php

    $localhost 
    "";
    $sql_username "";
    $sql_password "";
    $database "";

    //connect to database
    mysql_connect($localhost,$sql_username,$sql_password);
    @
    mysql_select_db($database) or die("Unable to select database for some reason!");
        
    /////////////Here is my attempt to create the array////////////
    $sql "select id, parent from test_tbl";
    $result mysql_query($sql);
    $data = array();
    while (
    $row mysql_fetch_row($result)) {    
         
    array_push($data$row[0], $row[1]);
        }

    //////////////////////Here is the original array////////////////////
    /*$data = array( 
    array('id' => 11, 'parent' => 0), 
    array('id' => 22, 'parent' => 11), 
    array('id' => 33, 'parent' => 0), 
    array('id' => 44, 'parent' => 33), 
    array('id' => 14, 'parent' => 44), 
    array('id' => 13, 'parent' => 44), 
    array('id' => 12, 'parent' => 13), 
    array('id' => 10, 'parent' => 77), 
    array('id' => 77, 'parent' => 11), 
    array('id' => 88, 'parent' => 11), 
    array('id' => 99, 'parent' => 88), 
    );*/



    // tree builder 
    // build a tree structure from the list above 

    function makeTree($data) { 
    $tree = array( 
    array(
    'id' => 'root''parent' => -1'children' => array())); 
    $treePtr = array(=> &$tree[0]); 
    foreach(
    $data as $item){ 
    $children = &$treePtr[$item['parent']]['children']; 
    $c count($children); 
    $children[$c] = $item
    $children[$c]['children'] = array(); 
    $treePtr[$item['id']] = &$children[$c]; 

    return 
    $tree


    // node printer 
    // put here whatever you want 

    function printNode($node) { 
    print(
    "<li>{$node['id']} is son of {$node['parent']}<ul>"); 
    foreach(
    $node['children'] as $child
    printNode($child); 
    print(
    "</ul></li>"); 



    // here we go 

    $tree makeTree($data); 

    // to see what happened 
    // var_dump($tree); 

    printNode($tree[0]); 
    ?>

  2. #2
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use mysql_fetch_assoc instead mysql_fetch_row

    and use this : array_push($data, $row]);
    instead of : array_push($data, $row[0], $row[1]);


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
  •