I have a session variable array called ‘section_remember’ with 2 values in it.
I am expecting the code below to result in a single “print_r” and give me two div’s (one for each value in the array).
Instead it executes 2 print_r’s and gives me 4 div’s, if that makes any sense.
If the array has 3 values in it it runs the loop 3 times and results in 3 print_r’s and 9 div’s. And so on depending on the number of values in the array.
I don’t understand why it’s doing this, but I’m new to PHP so I’m hoping it’s something obvious.
<?php
print_r($_SESSION['section_remember']);
foreach($_SESSION['section_remember'] as $key =>$value)
{
$sql = "SELECT `section`.`start`,`section`.`stop`,`section`.`title`,`daily_show`.`audio_file`
FROM `section`
INNER JOIN `daily_show`
ON `section`.`daily_show_id` = `daily_show`.`id`
WHERE `section`.`id` = $value";
$result = mysql_query($sql);
while ($query = mysql_fetch_array($result))
{
$title = $query['title'];
$seconds = $query['seconds'];
$duration = gmdate ('i:s', $seconds); //'H:i:s' for hours
echo "<div><span class='duration'>".$duration."</span>".$title."</div>";
}
}
?>
Seeing that the print_r() call is outside the loop and you get it to output three times, that means it’s inside a greater loop (or, say, a function being called three times). The code you’ve sent is not to blame here.