Beginner's PHP question

can anyone help me with this php program

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

plz help me as i am new to php programmingt

Hi there,

I’m afraid you’ll have to give us more to go on than this.
If you provide the code which produces the output you want to style, we’ll be in a much better position to help you.

well sir i want that stream1 should echo only once while the title of the movies should be under its group … i actually want to group them under the label of stream1

but the program i was trying was repeating the label of stream1 again and again ie

stream1
title of the movie1
cast1
director2

stream1
title of the movie2
cast2
director2

stream2
title of the movie 3
cast3
director3

i just dont want output labellled as stream1 again second time

sir i have provided the details i just cant figure out the php programming

What code do you currently have?

How will that information be derived – database, web service, static array, etc?

Put that in an array and do a loop that will print it. Can’t give you more help with that info.

i am trying to make a database driven website i know how to connect to the database but following programming is giving me problem

my table is named as pank

it contains columns named
id stream title cast director

<?php

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

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

?>

i just dont want output labellled as stream1 again second time

my table
id stream title cast director
1 stream1 title of the movie1 cast1 director1
2 stream2 title of the movie2 cast2 director2
3 stream3 title of the movie3 cast3 director3

Good morning,

I would suggest that you have entered something incorrectly to your database, as the code you posted is ok.

This code:

<?php
$con = mysql_connect("localhost","user","pass");
if (!$con){ die('Could not connect: ' . mysql_error()); }

$db_selected = mysql_select_db("test",$con);
$sql = "SELECT * from pank";
$result = mysql_query($sql,$con);

while($row = mysql_fetch_array($result)){
  echo "<p><strong>Row:" . $row['id'] . "</strong><br />";
  echo $row['stream'] . "<br />";
  echo $row['title'] . "<br />";
  echo $row['cast'] . "<br />";
  echo $row['director'] . "</p>";
}

mysql_close($con);
?>

When run against this database:

--
-- Table structure for table `pank`
--

CREATE TABLE IF NOT EXISTS `pank` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `stream` varchar(250) DEFAULT NULL,
  `title` varchar(250) DEFAULT NULL,
  `cast` varchar(250) DEFAULT NULL,
  `director` varchar(250) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `pank`
--

INSERT INTO `pank` (`id`, `stream`, `title`, `cast`, `director`) VALUES
(1, 'stream1', 'title of the movie1', 'cast1', 'director2'),
(2, 'stream2', 'title of the movie2', 'cast2', 'director2');

Outputs:

Row:1
stream1
title of the movie1
cast1
director2

Row:2
stream2
title of the movie2
cast2
director2

Pullo, the little tid-bit, quoted below, gives me the impression that he wants them grouped by the value in the stream field. Perhaps, altering your sample code to reflect ‘group by’ and ‘order by’ clauses in the sql, and required alterations to the loop code, would make it more clear. Though, I may be wrong on this.

Hey Serenarules,

Surely, GROUP BY would only make sense if each stream can be associated with multiple films and had a column that you could apply an aggregate function to.

For example:

$query = "SELECT stream, MIN(price) FROM pank GROUP BY stream";

Or did I miss something?

Possible. Or I did. Heh. According to his sample, stream 1 is indeed used with many entries, much like a category. I get the impression these may not be entered in order. So grouping them by stream in asc order would help in listing them out.

yes i think sir Serenarules got my point
could u just help me reorgainsing the code
$query = “SELECT stream, MIN(price) FROM pank GROUP BY stream”;

what actually MIN(price) is for

I think you might need to use ORDER BY.

Try this:

<?php
$con = mysql_connect("localhost","user","pass");
if (!$con){ die('Could not connect: ' . mysql_error()); }

$db_selected = mysql_select_db("test",$con);
$sql = "SELECT * from pank ORDER BY stream";
$result = mysql_query($sql,$con);

$current_stream = null;
while($row = mysql_fetch_array($result)){
	
  if ($row["stream"] != $current_stream) {
    $current_stream = $row["stream"];
    echo "<h1>$current_stream</h1>";
  }
  echo "<p>" . $row['title'] . "<br />";
  echo $row['cast'] . "<br />";
  echo $row['director'] . "</p>";
}

mysql_close($con);
?>

Right. And if you want the titles in each group to be in alphabetical order, you could use the sql: SELECT * from pank ORDER BY stream, title asc

Thank you everyone specially Serenarules Pullo