tags:

views:

43

answers:

2

Hi, I have the below code and I am trying to get each "users post" into a seprate div, currently it shows all the posts in one div. Am sure its something simple I have just messed up on, this isn't finished yet so some parts in the code are still abit dodgy.

<?php

if (loggedin())
{

$ID = getID();
$query = "SELECT * FROM `posts`";
$result=mysql_query($query);
$count=mysql_num_rows($result);


while ($row = mysql_fetch_array($result)) 
{

echo '<div id="posts">';
echo "<br />".$row['2']."<br />";
echo "</div>";
}

}
else
{
    echo "Not Logged In";
}
?>

Thanks :)

A: 

You are doing fine except for you are not using the clear and margin-top so that they appear below each other and a little at distance:

while ($row = mysql_fetch_array($result)) 
{
  echo '<div id="posts" style="clear:both; margin-top:20px;">';
  echo "<br />".$row['2']."<br />";
  echo "</div>";
}
Sarfraz
Thanks that works fine, didn't know about clear in css.:D
Elliott
@Elliott: You are welcome.............
Sarfraz
@Sarfraz: why are you clearing, and why do you maintain OP's `br` s?
ANeves
@sr pt: Using `<br />` is his purpose, basically he was already having separate divs but he could not find the **visual difference**, so giving `clear` was not that important but `margin-top` was so that he **feels** that divs are separate and not the one. :)
Sarfraz
A: 

First and foremost, id's in HTML must be unique and you loop is assigning same id for all your div-tags.

Else it's all correct, you are putting the data into separate divs already, just like Sarfranz says. Perhaps you just need to add some CSS to make it noticeable.

Bellow should be better.

<?php

if (loggedin())
{

$ID = getID();
$query = "SELECT * FROM `posts`";
$result=mysql_query($query);
$count=mysql_num_rows($result);

$i= 0;

while ($row = mysql_fetch_array($result)) 
{

echo '<div id="post_'.$i.'" style="border-bottom:1px #333333 dashed; margin-top:5px;">';
echo $row['2'];
echo '</div>';
$i++;
}

}
else
{
    echo "Not Logged In";
}
?>
jamietelin