简体   繁体   English

While语句中的PHP For循环计数

[英]PHP For Loop Count in While Statement

I'm trying to figure out the best solution on how to output my html with the php while counting up on a div class. 我试图找出最好的解决方案,该如何在div类上计数的同时用php输出我的html。

This is my function that prints news: 这是我打印新闻的功能:

function post_news()
{
    $post_news = $this->mysql->Query("SELECT * FROM news ORDER BY id DESC LIMIT 4");
    while ($news_row = mysqli_fetch_array($post_news))
    {
        echo "<div class='column1'>
            <div class='box'>
            <h3>".$news_row['title']."</h3>
            <p>".$news_row['content']."</p>
            <a href='index.php?p=news&id=".$news_row['id']."' class='button button-small'>Read More</a> </div>
            </div>";
    }
}

What I am attempting is, where it shows <div class='column1'> I need it to count upward to class='column4' . 我正在尝试的是显示<div class='column1'>我需要它向上计数到class='column4' When I attempt For loops or a second While loop I can sometimes get it to count but it always does a new line where I want these blocks to display side by side like normal. 当我尝试For循环或第二个While循环时,有时可以让它计数,但是它总是在新行中让这些块像正常一样并排显示。

So what is the best way to count up on my div class without it doing a line break after every block? 那么,在每个块之后不进行换行的情况下,加进div类的最佳方法是什么?

I assume that when you say 1 to 4 you want it to start over at 1 after 4. Just increment a counter and reset it when it reaches 4: 我假设当您说1到4时,您希望它在4之后从1重新开始。只要增加一个计数器并在计数器达到4时将其重置即可:

$num = 0;
while ($news_row = mysqli_fetch_array($post_news))
    {
        $num++;
        echo "<div class='column{$num}'>
            <div class='box'>
            <h3>".$news_row['title']."</h3>
            <p>".$news_row['content']."</p>
            <a href='index.php?p=news&id=".$news_row['id']."' class='button button-small'>Read More</a> </div>
            </div>";
        if($num == 4) { $num = 0; }
    }

Or actually, you use LIMIT 4 in the query so you don't really need the if statement that resets it to 0 , as the loop will only iterate 4 times. 或实际上,您在查询中使用LIMIT 4 ,因此您实际上并不需要if语句将其重置为0 ,因为循环仅迭代4次。

A pure html workaround to this would be to have the code like so: 一个纯HTML解决方法是使用如下代码:

<div class='column1'>
...
</div><div class='column2'>
..
</div>

But because you're echoing it that's not going to work. 但是因为您在回声,所以行不通。 Another thing you can do is specify an inline-block css property for those columns. 您可以做的另一件事是为这些列指定一个inline-block css属性。 If you're using bootstrap you can just use col-xs-3 so that you can have 4 even columns. 如果您使用的是引导程序,则可以只使用col-xs-3这样您就可以拥有4个偶数列。

If not, instead you're going to have to specify each of those classes to be width:25% 如果没有,那么您将不得不将每个类指定为width:25%

So to wrap up, in css: 总结一下,在css中:

.col{
    width:25%;/*probably going to have make responsive for screen width*/
    display:inline-block;/*or float:left, whichever*/
}

And in php/html: 并在php / html中:

...
echo "<div class='column1 col'>
            <div class='box'>
            <h3>".$news_row['title']."</h3>
            <p>".$news_row['content']."</p>
            <a href='index.php?p=news&id=".$news_row['id']."' class='button button-small'>Read More</a> </div>
            </div>";
...

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM