I am trying to create a "depth" based threaded comment type of thing for a job I am working on. The code (below) works but is prob. cumbersome and also I would like to go to $x depths which the way I have done it would mean "lots" of loops, any suggestions about how to create a simpler/better way?
$sql = $azdb->get_row("SELECT * FROM ".$table_prefix."_content WHERE ID='".$_GET['ID']."'");
if($sql):
echo '<h2>'.$sql->content_title.'</h2>';
echo date('d m Y',strtotime($sql->content_modified));
echo '<br />';
echo $sql->content;
endif;
$sql = $azdb->get_results("SELECT * FROM ".$table_prefix."_content WHERE content_parent='".$_GET['ID']."'");
if($sql): foreach($sql as $sql):
echo '<div class="comments">';
echo '<h2>Main '.$sql->content_title.'</h2>';
echo date('d m Y',strtotime($sql->content_modified));
echo '<br />';
echo $sql->content;
echo '<br />';
$sql1 = $azdb->get_results("SELECT * FROM ".$table_prefix."_content WHERE content_parent='".$sql->ID."'");
if($sql1): foreach($sql1 as $sql1):
echo '<div class="comments">';
echo '<h2>'.$sql1->ID.' - '.$sql1->content_title.'</h2>';
echo date('d m Y',strtotime($sql1->content_modified));
echo '<br />';
echo $sql1->content;
$sql2 = $azdb->get_results("SELECT * FROM ".$table_prefix."_content WHERE content_parent='".$sql1->ID."' ");
if($sql2): foreach($sql2 as $sql2):
echo '<div class="comments">';
echo '<h2>'.$sql2->content_title.'</h2>';
echo date('d m Y',strtotime($sql2->content_modified));
echo '<br />';
echo $sql2->content;
echo '</div>';
endforeach; endif;
ec开发者_如何学运维ho '</div>';
endforeach; endif;
echo '</div>';
endforeach; endif;
help appreciated. Thanks
Use a recursive function like so
$id = isset($_GET['id']) ? $_GET['id'] : 0;
$root_sql = $azdb->get_results("SELECT * FROM categories WHERE cat_parent = " . $id);
recursive_categories($root_sql);
function recursive_categories($results)
{
if(count($results))
{
echo "<ul>";
foreach($results as $res)
{
echo "<li>" . $res->category;
//Rest of what ever you want to do with each row
//Check this category for children
$rows = $azdb->get_results("SELECT * FROM categories WHERE cat_parent = " . $res->id);
recursive_categories($rows);
//has to be after the inner loops
echo "</li>";
}
echo "</ul>";
}
}
So for each iteration of the root it then finds another category that is a parent of the id, and then runs the same function at that point so creating another inner loop.
Modify accordingly but you get the idea.
What I would do in this situation is first and foremost review HOW you will insert your comments into the DB. The Schema you define is the most critical component to this whole piece.
I referenced a similar question on SO here: Fast Relational method of storing tree data (for instance threaded comments on articles)
The user is stumped as to how to store this comment data, and the Drupal format is really something efficient. And by looking at it this way you can create a clean approach, which will eliminate your multiple x
SQL queries.
Simply create your comment ID's like so (string):
1 - main
1.1 - comment 1
1.2 - comment 2
1.2.1 - nested comment for comment 2
1.2.1.1 - nested comment for comment 2, nested comment 1
1.2.2
1.2.3
1.2.2.1
1.1.1 - (this comment you could order by your sql call to be 3rd from the top, etc)
That allows you to make a single SELECT * FORM
call (get all ids that are x.%
etc), and then order all your comments properly, with the ability to use the ID as a conditional check to nest or not.
I think you need to re-think your schema first, then tackle the PHP code which becomes just a bunch of <li>
entries it looks like.
@RobertPitt 's answer is 'ok' but it does nothing for you, as it just cleans up your code from sequential if conditions to a proper recursive function.
精彩评论