Jump to content

php whiles and loops


spinsfil

Recommended Posts

hi

 

i have a db of tv shows, episodes and descriptions. they are in the db like:

 

name    | series | episode | description

Friends  | 1      |  1          | example

Friends  | 1      |  2          | example

Scrubs    | 1      |  1          | example

 

I want to display it this way:

 

Friends

Series 1 Episode 1

Series 1 Episode 2

 

Scrubs

Series 1 Episode 1

 

cant seem to get it to fully work - it will display it for the first show but not do more than one

 

 

thanks for any help :)

Link to comment
https://forums.phpfreaks.com/topic/221645-php-whiles-and-loops/
Share on other sites

$getshows = mysql_query("SELECT * FROM showsdb GROUP BY series ASC, ep ASC ORDER BY id ASC") or die (mysql_error());

echo "<table>";

while ($row = mysql_fetch_array($getshows))
{
echo "<tr><td><b>$row[name]</b></td></tr>";



$row = mysql_fetch_array($getshows);
$count=1;
do {
if ($count>2) { $i="</tr><tr>"; $count=0;}
else { $i="";}
echo "<tr><td>Series $row[series] Episode $row[ep]</td></tr>";
echo $i;
$count++;
}


while ($row = mysql_fetch_array($getshows));
}

echo "</table>";

 

So far this code is doing it correctly for just the first show...

 

Untested example (the query is changed, don't use group by):

 

$getshows = mysql_query("SELECT * FROM showsdb ORDER BY name ASC") or die (mysql_error());
$name = '';

echo "<table>";

while($row = mysql_fetch_array($getshows)) {
    if($row['name'] != $name) {
        echo "<tr><td><b>".$row['name']."</b></td></tr>";
        $name = $row['name'];
    }
    echo "<tr><td>Series ".$row['series']." Episode ".$row['ep']."</td></tr>";
}
echo "</table>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.