-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

2回目にstream1というラベルの付いた出力は必要ありません

4

3 に答える 3

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 に答える
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 に答える
-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 に答える