Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Splitting a query result into seperate lists.

    Hello!

    I've been tinkering with PHP again and this new MySQLi, it's cool what happens when you're on hiatus for two or more years

    I'm trying to list all episodes in a TV show into separate seasons, but I can't figure out the correct approach.

    This is my current code:
    PHP Code:
    <?php

        $getSerieID 
    mysqli_real_escape_string($conn$_GET['id']);
        
        
    $getSerie mysqli_query($conn"SELECT * FROM episode, program WHERE episode.program_id = '$getSerieID' AND program.program_id = '$getSerieID' ORDER BY sesong_id, episode_id ASC") or die(mysqli_error($conn));
        
        
    $printSerie mysqli_fetch_array($getSerieMYSQLI_ASSOC);
        echo 
    "\t<b>" $printSerie['navn'] . "</b><br>\n";
        echo 
    "\t" $printSerie['beskrivelse'] . "<br>\n";
        
        
    mysqli_data_seek($getSerie0);
        
        while (
    $printSerie mysqli_fetch_array($getSerieMYSQLI_ASSOC))
        {
            echo 
    "\t" $printSerie['episode_navn'] . "<br>\n";
        }
      

    ?>
    I would like to get a result looking like this:
    Season 1
    - 1
    - 2
    - 3
    Season 2
    - 1
    - 2
    - 3

    Help is much appreciated

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    I'm assuming you have the season id in each row?

    You can loop through the results and flag each row.

    Such as:
    PHP Code:
    $current_season_id false;
            
    while(){
    //Detect if this season is different than the last
    if($current_season_id != $row['season_id']){
                
    //Set the new flag value to suppress output of duplicate season title
    $current_season_id $row['season_id'];
                
    echo 
    "Season Number $current_season_id";
    }

    //Display episode list



  • Users who have thanked HDRebel88 for this post:

    CyberPirate (07-28-2012)

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts
    This worked like a charm. Thanks a lot HDRebel88, you are great

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by CyberPirate View Post
    This worked like a charm. Thanks a lot HDRebel88, you are great
    One more thing...This part of you code:

    $getSerieID = mysqli_real_escape_string($conn, $_GET['id']);

    Will the id gotten from the URL only contain numbers? If so you don't necessarily need to use mysqli_real_escape_string... You can simply do $getSerieID=intval($_GET['id']); if you do go that route though, you should get rid of the quotes around the $getSerieID variable in the query.

    If your ids are mixed alphanumberic, then leave it the way it is.


  •  

    Posting Permissions

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