Help displaying casting list

Hello,

I please to some help.

I would display the casting of TV series.
He is my code:

$cast = []; // Associative array [character => [actor]]
 
while ($row = $castResult->fetch_assoc()) {
    // Grouping actors by characters
    $cast[$row['charName']][] = $row['personName'];
}
 
foreach ($cast as $char => $persons) {
    echo implode(' > ', $persons), " : {$char}<br>";
}

Here is my sql query:

$castQuery = $conn->prepare('SELECT S.seriesId, CP.characterId AS charId, P.personId, seriesTitle, characterName AS charName, personName, appearanceOrder AS appOrder, period FROM CASTING AS C
JOIN SERIES AS S ON S.seriesId = C.seriesId
JOIN PERSON AS P ON P.personId = C.personId
JOIN CHAR_PLAYED AS CP ON CP.characterId = C.characterId 
WHERE S.seriesId = '.$_GET['series'].'
ORDER BY appearanceOrder');
$castQuery->execute();
$castResult = $castQuery->get_result();

This displays the following:
Actor 1 : Character 1
Actor 2 replaced by Actor 2 replaced by … : Character 2 (if an actor is replaced by one or more others

Actor 10 : Character 10

If a series has two or more periods, the cast should display like this.

I can’t figure out how to bring that to work.

You would index/pivot the data by first the ‘period’, then the ‘charName’ -

while($row = $castResult->fetch_assoc())
{
    // Grouping actors by period, then character
    $cast[$row['period']][$row['charName']][] = $row['personName'];
}

To produce the output, you would use two nested loops -

foreach($cast as $period=>$chars)
{
	foreach($chars as $char=>$persons)
	{
		echo implode(' > ', $persons), " : {$char}<br>";
	}
	echo $period . '<br><br>';
}

Hi,
Thank you for your help.

But, it remains one problem.
In the example, The Avengers, Patrick Macnee aka John Steed appears between the first and second period, instead of appearing at very first, above the first period.

You would need to post your data, what result you did get, and what result you expected from that data.

This is what I get :

Actor 1 : Character 1
Actor 2 replaced by Actor 2 replaced by … : Character 2 (if an actor is replaced by one or more others

Actor 10 : Character 10
For series wich doesn’t have periods, it’s ok.

This is what I want to have for series that have periods, like in this example.

Sponsor our Newsletter | Privacy Policy | Terms of Service