我的表名为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 第二次
答案 0 :(得分: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>";
}
?>
答案 1 :(得分: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>";
}
答案 2 :(得分:-3)
试试这个
echo "<h2>Stream : ".$row['stream']. "</h2>";
echo "<h3>".$row['title']."</h3>";
echo "<h3>".$row['cast']."</h3>";
echo "<h3>".$row['director']."</h3>";