使用 while 循环回显 PHP 数组

Posted

技术标签:

【中文标题】使用 while 循环回显 PHP 数组【英文标题】:echo PHP array with while loop 【发布时间】:2017-01-30 22:37:57 【问题描述】:

我正在使用 while 循环来回显 php 数组:

$connect = mysqli_connect("localhost", "user", "password", "db");
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT 0,4";
$result = mysqli_query($connect, $sql);

if(mysqli_num_rows($result) > 0)

 echo'<section>
        <div class="container">
           <div class="row">
             <h2 class="bold">title</h2>
               <hr> ';

                while($row = mysqli_fetch_array($result))
                
                 echo'
                <div class="col-sm-3 col-md-3 col-md-push-3">
                    <div class="portfolio-wrapper">   
                        <div class="portfolio-single">
                            <div class="portfolio-thumb">
                                <a href="'.$row['link']." target="_blank">                                                        <img src="'.$row['image'].'" class="img-responsive" ></a>
                            </div>
                        </div>
                        <div class="portfolio-info" dir="rtl">
                            <a href="'.$row['link2']." target="_self"><h2>                     </h2>
                                <h6>'.$row['title'].</h6>
                            </a>
                        </div>
                    </div>
                </div>';

            
                echo'
                </div>
                    </div>
                    </section';
               

            ?>

这段代码对我来说很好,但问题是我需要回显 4 个不同的

例如:

   first will be: <div class="col-sm-3 col-md-3 col-md-push-9">
   second: <div class="col-sm-3 col-md-3 col-md-push-3">
   third: <div class="col-sm-3 col-md-3 col-md-pull-3">
   last: <div class="col-sm-3 col-md-3 col-md-pull-9">

任何想法如何使用 while 循环来完成这个结构?

【问题讨论】:

保留一个计数器变量,使用它来确定使用哪个类,然后为循环的下一次迭代增加计数器变量。 就像罗比说的。像 $array = array( '0' =&gt; 'col-sm-3 col-md-3 col-md-push-9' '1' =&gt; 'col-sm-3 col-md-3 col-md-push-3', '2' =&gt; 'col-sm-3 col-md-3 col-md-pull-3', '3' =&gt; 'col-sm-3 col-md-3 col-md-pull-9'); $count = 0; while() div... class=$array[$count]; ... count ++ 这样的东西在记事本中完成,所以语法不正确,但你明白了 【参考方案1】:
<?php
$connect = mysqli_connect("localhost", "user", "password", "db");
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT 0,4";
$result = mysqli_query($connect, $sql);
$i = 1;
$count = count($result);
if(mysqli_num_rows($result) > 0)

 echo'<section>
        <div class="container">
           <div class="row">
             <h2 class="bold">title</h2>
               <hr> ';

                while($row = mysqli_fetch_array($result))
                
                if($i==1)echo '<div class="col-sm-3 col-md-3 col-md-push-9">';
                elseif($i == $count)echo '<div class="col-sm-3 col-md-3 col-md-pull-9">';
                elseif($i%2 == 0)echo '<div class="col-sm-3 col-md-3 col-md-push-3">';
                elseif($i%3 == 0)echo '<div class="col-sm-3 col-md-3 col-md-pull-9">';

                 echo'
                        <div class="portfolio-wrapper">   
                            <div class="portfolio-single">
                                <div class="portfolio-thumb">
                                    <a href="'.$row['link']." target="_blank">                                                        <img src="'.$row['image'].'" class="img-responsive" ></a>
                                </div>
                            </div>
                            <div class="portfolio-info" dir="rtl">
                                <a href="'.$row['link2']." target="_self"><h2>                     </h2>
                                    <h6>'.$row['title'].</h6>
                                </a>
                            </div>
                        </div>
                    </div>';
                    $i =$i+1;
            
                echo'
                </div>
                    </div>
                    </section';
               

?>

【讨论】:

【参考方案2】:

使用计数器,并将您的类放入这样的数组中:

$classes = [
    "col-sm-3 col-md-3 col-md-push-9",
    "col-sm-3 col-md-3 col-md-push-3",
    "col-sm-3 col-md-3 col-md-pull-3",
    "col-sm-3 col-md-3 col-md-pull-9"
];

$i = 0;
while($row = mysqli_fetch_array($result)) 
    if ($i > 3) 
        $i = 0;
    
    ?>
    <div class="<?php echo $classes[$i]; ?>">
        Your content comes here
    </div>
    <?php
    $i++;

注意

如果数组中没有类似的索引,则重置计数器。您的脚本假定您将始终获得 4 行。

【讨论】:

以上是关于使用 while 循环回显 PHP 数组的主要内容,如果未能解决你的问题,请参考以下文章

在while循环中动态回显字符串[重复]

如何在无限循环中每 3 分钟回显一次?

PHP - 仅当余数为 0 时才执行(和回显)操作 - 进入无限循环

在 foreach 循环中每次迭代后 PHP 回显

带有睡眠循环的PHP直到循环结束才更新数据库

如何使用 PHP 将 sql 属性回显到 HTML 表