开发者

Loop through array, and ouput into Div's

开发者 https://www.devze.com 2022-12-27 07:04 出处:网络
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 fin

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";
}
?&开发者_运维技巧gt;

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";
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号