Hello, I'm trying to make a facebook wall-alike php script.

It loads the posts stuff, photo url and user stuff like username and user id from my MySQL database. It also checks if the post is just a comment to another post (parentof). I can present all the original posts with pictures with a simple do-while using <table> tags .

But how could I present the comments under every post? I guess with another do-loop, but how?

Here is my code:

<?php

require_once "config.php";
	$result = mysql_query('SET NAMES utf8');
	$result = mysql_query('SET CHARACTER SET utf8');
	$wallhaku = mysql_query("SELECT `wall`.`post_id`, `wall`.`parentof`, `wall`.`sentby`, `wall`.`text`, `wall`.`image_url`, `users`.`username`, `users`.`photopath`, `users`.`name`, `users`.`member_id` FROM `wall` LEFT JOIN `users` ON `wall`.`sentby` = `users`.`member_id` WHERE parentof=0 ORDER BY post_id DESC") or die (mysql_error());
	$row_wallhaku = mysql_fetch_array($wallhaku);
	
?>
<table width="800" height="120" border="0" cellpadding="10">
<?php $i=0; $numberpage=1;
	do {
	$wallid = $row_wallhaku['post_id'];
	$parenthaku = mysql_query("SELECT post_id, parentof FROM wall WHERE parentof=$wallid") or die (mysql_error());
	$row_parenthaku = mysql_num_rows($parenthaku);
	if($i%$numberpage==0) echo "<tr>";?>
	  <td width="120" align="left">
      <img src=<?php echo $row_wallhaku['photopath']; ?> height=	"100" width="100">
      </td>
	  <td width="600" align="left">
    <a href="member.php?id=<?php echo $row_wallhaku['member_id']; ?>"><?php echo $row_wallhaku['name']?></a><br /><p><?php echo $row_wallhaku['text'];
	if($row_wallhaku['image_url']=="0") {
	?>
    <p align="right"><?php echo $row_parenthaku ?> kommenttia.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>">Lue, kommentoi</a> <?php } ?></td>
<?php $i++; if($i%$numberpage==0) echo "</tr>";
if($row_wallhaku['image_url']!="0")
	{
		?>
	<tr>
    <td width='800' colspan='2' align='center'>
	<a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>"><img src=<?php echo $row_wallhaku['image_url']; ?> height="180"></a>
    <p align="right"><?php echo $row_parenthaku ?> kommenttia.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="post.php?id=<?php echo $row_wallhaku['post_id']; ?>">Lue, kommentoi</a>
    </td>
    </tr>
    
    <?php
	}
?>
<?php } while ($row_wallhaku = mysql_fetch_assoc($wallhaku)); ?>
</table>
Member Avatar for diafol

A common problem with getting data for a running second queries. One solution I've used is to create an additional field, like this:

SELECT *, IF(parentof = 0,post_id,CONCAT(parentof,'.',post_id )) AS neworder FROM wall ORDER BY neworder


Note I've stripped other fields for clarity.

This can produce an order based on the following:

TABLE ORDER

QUERY ORDER


All you need to do is in your loop, if the parentof = 0, set a different title format etc, otherwise, format as replies.

Because you're using the table id to order, the comments should be ordered chronologically.

That sounds like a good idea, but not so well organized i guess.

That's why I separated comments into another table.
So now I got tables:
users, wall, comments.
Is there any way to loop and print comments to all posts? any other way?

Member Avatar for diafol

You need to bone up on joins since it should work even with sep tables

But I heard about some thing called "foreach" -loop. I read about it but don't know how to use it for my code. duh :/

Member Avatar for diafol

the whole point is that the sql I offered should ensure that you don't need to use foreach. Up to you.

Is foreach a bad thing then? Well what would be your mysql query? I didn't understand it, well, could google a bit :)

Member Avatar for diafol

From your eg.s you have 2 tables: users/wall.

SELECT users.*, wall.*, IF(wall.parentof = 0,wall.post_id,CONCAT(wall.parentof,'.',wall.post_id )) AS neworder FROM wall INNER JOIN users ON wall.user_id = users.user_id ORDER BY neworder

I don't know whether I got the related keys right, but that's a way to go with two tables. The fact that you now have 3 tables should make it just as simple.

OK, my last post on this thread. Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.