...

View Full Version : Splitting a query result into seperate lists.



CyberPirate
07-28-2012, 10:41 PM
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

$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($getSerie, MYSQLI_ASSOC);
echo "\t<b>" . $printSerie['navn'] . "</b><br>\n";
echo "\t" . $printSerie['beskrivelse'] . "<br>\n";

mysqli_data_seek($getSerie, 0);

while ($printSerie = mysqli_fetch_array($getSerie, MYSQLI_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 :)

HDRebel88
07-28-2012, 10:56 PM
I'm assuming you have the season id in each row?

You can loop through the results and flag each row.

Such as:


$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

}

CyberPirate
07-28-2012, 11:06 PM
This worked like a charm. Thanks a lot HDRebel88, you are great :)

HDRebel88
07-29-2012, 12:45 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum