Menu

Probs with db_mysql

Help
2004-03-11
2004-03-11
  • Frank Becker

    Frank Becker - 2004-03-11

    Hello all,

    for a new project I want to use PHPLIB for access to mySQL. First tests with common jobs are successful. But with a special job I have a big problem.

    I want to create a hierachical tree out of the database using a recursive function. If I use normal mysql_functions to access to the database the functions works fine. But if I use the  PHPLIB-Class it does not.

    The expected output is:
    1 One
    L-2 Two
    L-3 Three
    L--4 Four
    L----5 Five

    With PHPLIB there is only one level shown.

    The used function is listed below

    Many thanks for your help and for the time reading this posting.

    Best regards
    Frank Becker

    ----
    function disp_child($parent, $level) {

    global $db;           // Database class
    global $dblink;

    /* not working correctly
        $db->query("select * from kd_address where upline1=".$parent);
        //$db->next_record();
        while( $db->next_record() ) {
            $id = $db->f("id");
            $nn = $db->f("name");
            $ul = $db->f("parent");
           
            print( str_repeat( ".",$level) . $id . "$nn<br>" );
            disp_child($id, $level+1);
        }
    */

    // the following part is working fine

        $result = mysql_query("select * from kd_address where parent=".$parent.";");
        while( $row = mysql_fetch_array($result)) {
            print( "L".str_repeat("_", $level) . $row['id'] . $row['name']."<br>" );
            disp_child($row['id'], $level+1);
        }
       
    }

     
    • Layne Weathers

      Layne Weathers - 2004-03-11

      Each $db object can only handle the results from one query at a time. You are naming $db global in your function - you would get the same results if you set $row or $result global in your old code.

      My preferred solution is to loop through the results creating an array, and then loop through that (non-global) array to build your output.

      (Disclaimer: I have not tested this code.)
      function disp_child($parent, $level) {
          global $db;
          $db->query("select * from kd_address where upline1 = '$parent'");
          if(!$db->num_rows()) return false;
          $results = array();
          while($db->next_record()) {
              $results[] = $db->Record;
          }
          foreach($results as $row) {
              print("L" . str_repeat("_", $level) . $row["id"] . $row["name"] . "<br>");
              disp_child($row["id"], $level + 1);
          }
      }

       
      • Frank Becker

        Frank Becker - 2004-03-11

        Hello Layne,

        thank you very much for your fast and competent help. Your solution works fine.

        Best regards

        Frank

         

Log in to post a comment.