SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: Beginner's PHP question

  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,466
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    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.
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  3. #3
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sir i have provided the details i just cant figure out the php programming

  5. #5
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,466
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    What code do you currently have?
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    3,845
    Mentioned
    11 Post(s)
    Tagged
    3 Thread(s)
    How will that information be derived – database, web service, static array, etc?
    The only code I hate more than my own is everyone else's.

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put that in an array and do a loop that will print it. Can't give you more help with that info.
    http://yourshoutbox.com <- FREE for your website
    http://www.medicineinanswers.com <- interesting medical questions
    with answers

  8. #8
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  10. #10
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,466
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    Good morning,

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

    This code:

    PHP 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:

    Code:
    --
    -- 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
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  11. #11
    Resident OCD goofball! bronze trophy
    Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    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.

    Quote Originally Posted by pankajbadatia View Post
    i just dont want output labellled as stream1 again second time

  12. #12
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,466
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    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:

    PHP Code:
    $query "SELECT stream, MIN(price) FROM pank GROUP BY stream"
    Or did I miss something?
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  13. #13
    Resident OCD goofball! bronze trophy
    Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    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.

    Quote Originally Posted by pankajbadatia View Post
    stream1
    title of the movie1
    cast1
    director2

    stream1
    title of the movie2
    cast2
    director2


    stream2
    title of the movie 3
    cast3
    director3

  14. #14
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  15. #15
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,466
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    I think you might need to use ORDER BY.

    Try this:

    PHP 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 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);
    ?>
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  16. #16
    Resident OCD goofball! bronze trophy
    Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    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

  17. #17
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you everyone specially Serenarules Pullo

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •