循环php只打印第一轮div

时间:2015-11-26 04:34:34

标签: php mysql loops

你好我想把这个结构变成一个循环。通过对数据库的查询形成该结构。我已经采取了很多转向如何执行循环,无法找到解决方案。

结构如下:

示例db:

id | NombrePelicula | Director | ano | 


1 | Taxi Driver | Martin Scorsesse | 1976

2 | The color of money | Martin Scorsesse | 1986

3 | Spartacus | Stanley Kubrick| 1960

4 | After Hours | Martin Scorsesse | 1985

5 | My name is nobody | Sergio Leone | 1973

查询:

$lista="SELECT * FROM peliculas ORDER BY  id desc";

$listado=mysqli_query($Conex, $lista);



bucle:

while($x=mysqli_fetch_array($listado)){

$id=$x["id"];
$director=$x["director"];
$ano=$x["ano"];
$pelicula=$x["NombrePelicula"];



<div class="director"><span class="director-texto"><?php echo $director?></span></div></div>

echo $pelicula;


}

我希望以这种方式出现

Martin Scorsse-----------

Taxi Driver

Raging Bull

The Color of Money

Stanley Kubrick------------

Spartacus

Dr. Strangelove

The Shining

Sergio Leone---------

Once Upon a Time in America

A Fistful of Dollars

我这样做只会对数据库进行查询。无需为每位导演制作循环。

我的意思是,对于导演名字周围的每个循环,然后在他的电影下只打印一次。我试图用条件给出很多圈,但我设法找到解决方案

2 个答案:

答案 0 :(得分:2)

您可以在查询中执行ORDER BY director,然后在循环时检查当前director是否与上一个导演相同,并仅打印director名称何时发生变化 -

$lista="SELECT * FROM peliculas ORDER BY director";

$listado=mysqli_query($Conex, $lista);

$currentDirector = ""; // simple var to hold current director

while($x=mysqli_fetch_array($listado)){

    $id=$x["id"];
    $director=$x["director"];
    $ano=$x["ano"];
    $pelicula=$x["NombrePelicula"];

    if($director ! = $currentDirector) { // if director not the same as the current director, echo header
    { ?>
        <div class="director"><span class="director-texto"><?php echo $director?></span></div>
    <?php
        $currentDirector = $director; // set this director to current director
    }

    echo $pelicula."<br />";     
}

如果你想用<div>包装导演和电影,你会做这样的事情

$lista="SELECT * FROM peliculas ORDER BY director";

$listado=mysqli_query($Conex, $lista);

$currentDirector = ""; // simple var to hold current director

while($x=mysqli_fetch_array($listado)){

    $id=$x["id"];
    $director=$x["director"];
    $ano=$x["ano"];
    $pelicula=$x["NombrePelicula"];

    if($director ! = $currentDirector) { // if director not the same as the current director, echo header
    { 
        if($currentDirector != "") { // if not the 1st director, close the last director div before we open a new director div
            echo "</div>"; // closes last div
        }
    ?>
        <div class="director"><span class="director-texto"><?php echo $director?></span><br />
    <?php
        $currentDirector = $director; // set this director to current director
    }

    echo $pelicula."<br />";     
}
echo "</div>"; // close the last director div

答案 1 :(得分:2)

使用group bygroup_concat获取每位导演连续拍摄的所有电影。

select director, group_concat(movie) 
from movies
group by director

然后你可以在,上爆炸电影行或者将字符串替换为换行符。

SQL小提琴示例:http://sqlfiddle.com/#!9/a0d32/2

Str_replace示例:

$string = 'Taxi Driver,The color of money,After Hours';
echo str_replace(',', '<br>', $string);

输出:

Taxi Driver<br>The color of money<br>After Hours

PHP演示:https://eval.in/476090

或作为您的代码的完整示例(未经测试)......

<?php
$lista="SELECT director, group_concat(NombrePelicula) as movie FROM peliculas group by director ORDER BY director";
$listado=mysqli_query($Conex, $lista);
$currentDirector = ""; // simple var to hold current director
while($x=mysqli_fetch_array($listado)){
    $director=$x["director"];
    $pelicula=$x["movie"];
?>
    <div class="director"><span class="director-texto"><?php echo $director?></span></div>
<?php
    echo str_replace(',', '<br>', $pelicula);     
}
?>