Jump to content

Recommended Posts

At least I think it will need a while or several....

 

On my message board I want the posts to be displayed like:

Reply 6 - Username

    Reply 8 - Username

Reply 7 - Username

Reply 1 - Username

    Reply 3 - Username

          Reply 4 - Username

                Reply 5 - Username

    Reply 2 - Username

 

As you can see replies can have replies and replies of replies etc....

 

id = post id

fid = post forum

rid = what post it is in reply to

 

I had something on the idea of whiles within whiles but what I tried didn't work....

 

Can anyone provide a solution on how to get these posts echoes out??

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/
Share on other sites

You can make life a lot easier in the way you set up your array. The way i approach this is with a two dimensional array. For each post, the first dimension is the id of the parent post, and second is the id of the current post. You therefore set a top level id (which in the following example was 0):

 

<?php
$array = array();
$array[0][6] = 'Jim';
$array[6][8] = 'Bob';
$array[0][7] = 'Tom';
$array[0][1] = 'Dick';
$array[1][3] = 'Harry';
$array[3][4] = 'Fred';
$array[4][5] = 'George';
$array[1][2] = 'Run out out names';

function recursive($array,$index=0){
foreach($array[$index] as $k => $v){
	echo "<li>Reply $k - $v</li>";
		if(is_array($array[$k])){
		echo '<ul>';
		recursive($array,$k);
		echo '</ul>';
	}
}
}
echo '<ul>';
recursive($array);
echo '</ul>';
?>

 

Of course, this isn't going to work as-is, but it might give you an idea of how these things work.

 

Here's another example(which is largely identical to the one above, but for a breadcrumb menu):

http://www.phpfreaks.com/forums/index.php/topic,156551.msg680172.html#msg680172

 

Again, might just help you on your way.

 

And one further example that i wrote, which was for generating a listing of the number of files within a directory and all sub directories:

 

http://www.phpfreaks.com/forums/index.php/topic,157389.msg684881.html#msg684881

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350079
Share on other sites

I think I can see a way of doing it, possibly....

 

Depends.

 

What I would do is:

1. somehow while out the top level replies into the $array

2. Then use the $array to fetch_assoc etc....

3. This is where I'm stuck....getting to the next lot of replies...

 

Would I make another internal array using while and then again etc....

 

???

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350082
Share on other sites

ooooh! I ran your script and was very surprised. It showed something completely different to what I thought it would...but exactly what I need.

 

Yet there's things I still need to get my head round....

when creating the array I don't know how to while it out successfully

 

I would still need to do several whiles to get all the data I need as there is no one thing the same for each reply that I can seach

for the first stream of replies the rid is the current post, second stream rid is from one of the first stream rids, third from one of the second stream rid and so on....

 

Is there a way I can get it all into the array???

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350093
Share on other sites

Well, in your given example, id is the id of the post, whilst rid is the parent id. Assuming you set a default on the rid field of 0, you could do something like:

<?php
$sql = "SELECT * FROM `forum` WHERE `fid`='$forumnumber'";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)){
$parent_id = $row['rid'];
$post_id = $row['id'];
$user= $row['user'];
$array[$parent_id][$post_id] = $user;
}
?>

 

That would generate the array i typed out.

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350097
Share on other sites

I think I got it near enough working....

 

only problem is that it echoes:

Reply 1 - $hm[title] - $hm[username]

Reply 6 - $nm[title] - $nm[username]

Reply 10 - $om[title] - $om[username]

Reply 7 - $nm[title] - $nm[username]

Reply 9 - $om[title] - $om[username]

Reply 8 - $om[title] - $om[username]

Reply 5 - $hm[title] - $hm[username]

Reply 4 - $hm[title] - $hm[username]

Reply 3 - $hm[title] - $hm[username]

Reply 2 - $hm[title] - $hm[username]

 

How do I get the variables to work????

 

EDIT just occurred to me you will need to know more info :P

 

The replies are set in an array: $array[0][$hmid] = '$hm[title] - $hm[username]';

 

$hmid is working fine. The rest isn't :(

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350130
Share on other sites

The replies are set in an array: $array[0][$hmid] = '$hm[title] - $hm[username]';

 

Php only parses strings for variables that are contained within double quotes. If you place them inside single quotes, they are treated as litterals - that is you, litterally want to echo $var, rather than it's value.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/69645-solved-while-help/#findComment-350390
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.