-3

我的表被命名为pank 我知道如何连接到数据库但是下面的编程给我带来了问题

我的桌子是:

id| stream  |     title           |  cast | director

1 | stream1 | title of the movie1 | cast1 | director1

2 | stream1 | title of the movie2 | cast2 | director2

3 | stream2 | title of the movie3 | cast3 | director3

我的 PHP 脚本:

$query  = "SELECT * FROM pank";
$result = mysql_query($query); 

while ($row = mysql_fetch_array($result))
{
    echo "<h2>".'Stream : ', $row['stream'], "</h2>",
         "<br />",
         "<h3>", 'Title of the movie  : ', $row['title'], "</h3>",
         "<h3>", 'cast : ', $row['cast'], "</h3>",
         "<h3>", 'director : ', $row['director'], "</h3>"
        ;
}

我想要输出为:

stream1

title of the movie1
cast1
director1

title of the movie2
cast2
director2


stream 2 

title of the movie3
cast3
director3

上面的 php 给出的输出为:

stream1
title of the movie1
cast1
director2

stream1
title of the movie2
cast2
director2


stream2 
title of the movie 3
cast3
director3

我只是不想第二次再次将输出标记为stream1

4

3 回答 3

0

想要的结果?

$query  = "SELECT * FROM pank";
$result = mysql_query($query); 
$arrayKeys = array();
while ($row = mysql_fetch_array($result))
{
    if(!array_key_exists($row['stream'],$arrayKeys)){
        echo "<h2>".'Stream : ', $row['stream'], "</h2>";
    }
    $arrayKeys[$row['stream']] = true;
    echo "<br />",
         "<h3>", 'Title of the movie  : ', $row['title'], "</h3>",
         "<h3>", 'cast : ', $row['cast'], "</h3>",
         "<h3>", 'director : ', $row['director'], "</h3>";
}
于 2013-01-20T06:58:43.117 回答
0
<?php

$query="SELECT * FROM pank order by stream"; // make sure the result groups all streams together
$result=mysql_query($query); 

$currentstream = null; // create a container to record the current stream
while($row = mysql_fetch_array($result))
{
    // If this entry's stream is different from the current, display it, 
    // and reset the current stream variable
    if($row['stream'] != $currentstream){ 
        echo "<h2>".'Stream : '. $row['stream'] . "</h2>";
        $currenstream = $row['stream'];
    }
    echo "<br />";
    echo "<h3>" .'Title of the movie  : '. $row['title'] . "</h3>";
    echo "<h3>" .'cast : '. $row['cast'] . "</h3>";
    echo "<h3>" .'director : '. $row['director'] . "</h3>";
}
?>
于 2013-01-20T06:31:07.857 回答
-3

尝试这个

echo "<h2>Stream : ".$row['stream']. "</h2>";
echo "<h3>".$row['title']."</h3>";
echo "<h3>".$row['cast']."</h3>";
echo "<h3>".$row['director']."</h3>";
于 2013-01-20T06:37:32.857 回答