php can't get my head round this simple problem

Joined
12 Feb 2006
Posts
17,590
Location
Surrey
ok so lets say i have a comments system in that user posts comments which consist of title and description.

i have 20 selected at a time order by the timestamp and a link (which is the title) is shown which will link to the full description.

now the trouble i'm having is displaying the date as a header if it changes, so all the comments with today date have header saying todays date and they are listed under it, then ones with yesterday have yesterday date and are listed under, repeat until the 20 are all shown

now i can't seem to get my head round the logic behind how i am trying to do this.

atm i have a while loop going, which first displays todays date, the first comment title, then say for the next do you have same date, if yes display, if not display yesterday date as a header and then display title.

the trouble is then as it will do the same for any not with todays date and i can't think how to say if you are not todays date but have you already been put as a header.

hope i have been clear in what i'm doing. probably done it a very illogical way so if there is a better please suggest, i find best to learn from my mistakes rather then suggestions which is why i have done it first how i tihnk to do it.
 
I think I see what you're trying to do, you want your comments to display like this:

Date Header
Comment
Comment
Comment
Date Header
Comment
Comment
Date Header
Comment

to 20 comments?

So, run the select statement to get your comments, then loop through them thus:

PHP:
foreach ($results as $comment) {
	if (!isset($datehead) || $datehead != $comment['date_posted']) {
		$datehead = $comment['date_posted'];
		echo '<h2>' . $datehead . '</h2>';
	}
	echo 'Commentstuff';
}

that will only print your date header if the date for the current comment is different to the date for the last comment. Is that what you're after?
 

yeah that is what i am after however having problems getting it to work how i want.

have never used a foreach statement before so not fully understanding it however have read up about it in php manual so i think i kind of understand it.

here is what i think it happening

$results is the array of the select statement, so atm i have just selected the commentTitle and commentDate where commentSubject=$messageId, that is stored as $query, then i do $result = mysql_fetch_array( $query ) to have it as an array.

then in the foreach loop i am assinging the value of $result array to $comment, so using $comment['commentTitle'] is the same as $result['commentTitle']?

then in the foreach loop i say if datehead isn't set or datehead doesn't match date for current comment, set datehead to commentDate and then display it, then display the comment title, so $comment['commentTitle']?


the trouble i'm having is instead of putting a date and then title, the first results puts 9 as the date, and then 99 as title, then second puts G as date and GG as title, third is n and nn, and fourth is 1 and 11.

i guess it is doing it for times as i have for different dates but i have no clue why it is giving those results.

wouldn't have thought it would make a difference that the date is stored as a timestamp would it?


sorry for long post and thanks once again for the help
 
yeah, $results holds your array returned from your query. do me a favour and run this:

PHP:
echo '<pre>' . print_r($results,true) . '</pre>';

where $results is your returned query data. Post that up so I can see if there's anything unexpected in your data.

You've got the gist of what the foreach loop is doing (how have you lasted without it!? :p), when using timestamps, you'll need to run it through PHP's date() function to give it any kind of formatting.

Just re-read your post, and you're kinda there on foreach - $comment['commentDate'] would be the same as $results[$n]['commentDate'] where $n is the current iteration in the loop
 
Last edited:
[edit]
thanks a bunch sic got it working finally

it wasn't working because of one simple mistake, when writing code i will use a capital letter for any word past the first word so i changed the datehead to dateHead but forgot to change the ones in the expression so every time it said if !isset($datehead) it would never be set because it would then only set $dateHead

anyway working perfectly now so once again thanks a bunch
 
Last edited:
Back
Top Bottom