954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

do-loop inside another do-loop? help!

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 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><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>
lolimsoasd
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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 Capture.PNG

QUERY ORDER Capture2.PNG


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.

Attachments Capture.PNG 13.96KB Capture2.PNG 13.59KB
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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?

lolimsoasd
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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 :/

lolimsoasd
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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

lolimsoasd
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: