-3

my table is named as pank i know how to connect to the database but following programming is giving me problem

my table is:

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

My PHP script:

$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>"
        ;
}

I want output as:

stream1

title of the movie1
cast1
director1

title of the movie2
cast2
director2


stream 2 

title of the movie3
cast3
director3

the above php is giving output as:

stream1
title of the movie1
cast1
director2

stream1
title of the movie2
cast2
director2


stream2 
title of the movie 3
cast3
director3

I just don't want output labeled as stream1 again second time

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
pankaj
  • 3
  • 1
  • Nope. it will return correct answer only. Please check your DB?. may be records are wrong, – Ravichandran Jothi Jan 20 '13 at 06:25
  • 2
    Side note don't use `mysql_*` There is nothing wrong with your code it must be the data you have saved. – Sir Jan 20 '13 at 06:26
  • Your question is not clear. First of all it's a too localized problem, there is likely no further interest to have it in it's current form here on the website. Second according to your data, the output is not like you tell. – hakre Jan 20 '13 at 06:36
  • **Exact duplicate of http://stackoverflow.com/questions/14417123/label-stream1-duplicated** Do not repost questions – Lightness Races in Orbit Jan 20 '13 at 06:50

3 Answers3

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>";
}
?>
Gareth Cornish
  • 4,357
  • 1
  • 19
  • 22
  • This is overcomplicating the issue when its mainly just an issue with data in the table. You can see from his table example that his data would make your if statement kinda redundant here =/ – Sir Jan 20 '13 at 06:34
  • @Dave: What nonsense. Look again at what the `if` is doing. – Lightness Races in Orbit Jan 20 '13 at 06:46
  • Giving this a +1 because it's right contrary to what the first comment says. It _should_ have an explanation in order to be a good answer, though. – Lightness Races in Orbit Jan 20 '13 at 06:46
  • Based on his table he does not have duplicated stream names compare the output to his data thats why its not required. If he did he could left join and GROUP them in his SQL not use PHP. – Sir Jan 20 '13 at 06:47
  • @Dave: The demonstrated input and output shows otherwise, though you have pointed out correctly that he has made a _typo_ in his table quote. – Lightness Races in Orbit Jan 20 '13 at 06:49
  • The output is most likely because he has written stream1 and stream1 twice in his database. Its unknown if typo or that is his data we cannot just assume. – Sir Jan 20 '13 at 06:49
  • @Dave: Yeah it's shockingly poor – Lightness Races in Orbit Jan 20 '13 at 06:50
  • I'm not sure you're edit to change his row data is right =/ That is bending the question to cater to the answer your assuming he made a typo =/ – Sir Jan 20 '13 at 06:52
  • special thanks to Gareth Cornish he got my point – pankaj Jan 20 '13 at 14:02
0

Desired result?

$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>";
}
Muqito
  • 1,369
  • 3
  • 13
  • 27
  • 1
    thanks MaggiQall you got my point this is what i was expecting as an output – pankaj Jan 20 '13 at 14:03
  • @pankaj you could just have added order by in my query and it would work just as great. thanks for the downvote. – Muqito Jan 21 '13 at 12:20
-3

Try This

echo "<h2>Stream : ".$row['stream']. "</h2>";
echo "<h3>".$row['title']."</h3>";
echo "<h3>".$row['cast']."</h3>";
echo "<h3>".$row['director']."</h3>";
Sunil Kumar
  • 1,389
  • 2
  • 15
  • 32