doujing8435
2014-05-14 14:41
浏览 53
已采纳

使用for循环和sql COUNT循环遍历div

CODE :

$result= mysql_query ("SELECT COUNT(post_id) AS postCounter FROM headlines WHERE user_id= '$_SESSION[user_id]' ");
$numrows = mysql_num_rows ($result);

if ($numrows!=0){
    while ($row = mysql_fetch_array ($result))
        $postCounter = $row['postCounter'];

    for ($i=1; $i<=$postCounter; $i++){
        echo "$i";
    }
}

Now, what I want to know is "How could I use for-loop to echo div's depending on the COUNT result?"

So if $postCounter above was "4" I need to display 4 divs for it .

图片转代码服务由CSDN问答提供 功能建议

CODE:

  $ result = mysql_query(“SELECT COUNT  (post_id)AS postCounter FROM标题WHERE user_id ='$ _SESSION [user_id]'“); 
 $ numrows = mysql_num_rows($ result); 
 
if($ numrows!= 0){
 while($ row =  mysql_fetch_array($ result))
 $ postCounter = $ row ['postCounter']; 
 
 for($ i = 1; $ i&lt; = $ postCounter; $ i ++){
 echo“$ i”; \  n} 
} 
   
 
 

现在,我想知道的是“我如何根据COUNT结果使用for循环来回显div?”

因此,如果上面的 $ postCounter 为“4”,我需要显示4个div。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpu5600 2014-05-14 14:51
    已采纳

    In your for loop, echo a DIV instead of just echoing the number.

    for ($i = 1; $i <= $postCounter; $i++) {
        echo "<div>$i</div>";
    }
    
    打赏 评论
  • donglin317704291 2014-05-14 14:52

    An other way you can do by FOREACH to embed html into php

    <ul>
    <?php foreach($postcounter as $post) { ?>
        <li><?php echo $post; ?></li>
    <?php } ?>
    </ul>
    

    Hope this help

    打赏 评论

相关推荐 更多相似问题