PHP (again)

Status
Not open for further replies.

foetusinc

Ars Scholae Palatinae
1,409
Subscriptor++
This one might be a little tougher than my last q. I want to do recursive MySQL queries in php - i.e. move through a result array, issuing a query on each result, issuing the same query on those results, and so on until there are no more results to be had. This is for message threading, so there's no telling how deep a set might go. I think I know how to do this in Perl with some anonymous reference funnybusiness, but what about php? Can I store db handles (or a least result sets) anonymously in a list while I recurse through them, or am I approaching this from entirely the wrong direction?
 

Telematx

Ars Tribunus Militum
2,841
There's no need to to do recursive queries. Just have fields for ThreadID, ParentID, and ThreadLevel. Then you can just use one query (select everything with a certain ThreadID), store everything in multi-dimensional arrays, and use a recursive function to sort the arrays.

I wrote something along these lines a couple years ago, for both ASP and PHP, but I can't find it at the moment.
 

foetusinc

Ars Scholae Palatinae
1,409
Subscriptor++
I definitely see what you're saying, looks like it comes down to a question of where I want the work done... Got this put together last night as a test, so I'll have to play with your idea now and see what I like better.

Code:
<?php
  function print_children($parent_id) {
    global $dbh;
    $query = "select * from messages where parent_message = $parent_id";
    $results = mysql_query($query,$dbh);
    echo "<orderedlist type=\"disc\">\n";
    while ($current_row = mysql_fetch_array($results)) {
      printf("<listitem>%s - %s</listitem>\n",$current_row["subject"],$current_row["ID"]);
      print_children($current_row["ID"];
    }
    echo "</orderedlist>\n";
  }
?>

Opentopic does some funny stuff if I put ol and li tags in that code...
 
Status
Not open for further replies.