问题描述:

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

网友答案:

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>";
}
网友答案:

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";
}
?>
相关阅读:
Top