I would suggest you start learning a templating system such as SMARTY which will make a lot of your problems a lot easier to solve and your code a lot easier to read and understand.
Jon Grant
2009-07-28 09:35:32
I would suggest you start learning a templating system such as SMARTY which will make a lot of your problems a lot easier to solve and your code a lot easier to read and understand.
I would try something like this, it allows you to separate the design from the logic which is fetching stuff from the database.
1.Build an array from your database which is something like below - this should be straightforward:
<?php
$posts = array(
array(
'title' => 'Hello',
'post' => 'This is the post',
'comments' => array(
array(
'date_posted' => '28/07/2009',
'text' => 'this is the first comment'
),
array(
'date_posted' => '28/07/2009',
'text' => 'this is the second comment'
)
)
),
array(
'title' => 'Another post',
'post' => 'Hello',
'comments' => array()
)
);
?>
2.Loop through array and output html (I have simplified but you should be able to adapt).
<?php foreach ($posts as $post): ?>
<!-- begin status post -->
<h1><?php echo $post['title']; ?></h1>
<p><?php echo $post['post']; ?></p>
<?php if ($post['comments']): ?>
<h2>Comments:</h2>
<ul>
<?php foreach ($post['comments'] as $comment): ?>
<!-- begin comment -->
<li>
<?php echo $comment['text']; ?>
etc.
</li>
<!-- end comment -->
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- end status post -->
<?php endforeach; ?>
You can close your php tag to drop into html again then reopen it inside your loop.
e.g.:
<table width="400">
<?PHP
$last_id = 0;
echo '<table width="400">';
while ($row = mysql_fetch_array($res)) {
//start output of new status post and comments
if ($row['0'] != $last_id) {
//echo 'status post stuff'
?>
<tr>
<td width="99" valign="top" style="border-bottom: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;"> <div class="imageSub" style="width: 90px;"> <img class="female" src="http://cache2.mycrib.net/images/image_group66/0/43/t_6871399b0962b5fb4e29ce477541e165950078.jpg" alt="Something" width="90"/> </div></td>
<td width="489" style="border-bottom: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;">so and so said blah blah blah @ wee hours of the moring! <BR>
<?php
}
//start output of new status post and comments
//output comment here
$last_id = $row['0'];
if($row['commentid'] != ''){
echo 'status COMMENT for above status post'
}
//END output comment here
?>
<!-- begin comment -->
<table width="90%" style="border: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;">
<tr>
<td width="14%" rowspan="2" valign="top"><img class="male" src="http://cache2.mycrib.net/images/image_group34/0/39/T_653807517aff2b1f5662d865b40d87d527c8eb.jpg" alt="Something" width="45"/></td>
<td width="86%">Date Posted</td>
</tr>
<tr>
<td>Comment text</td>
</tr>
</table>
<!-- end comment -->
<?php
}
echo '</table>';
?>