Not sure exactly how to ask the question of what Im trying to do but will try explain it and insert the code.
But the basics of the question is Im running a sports website with a database with several tables.

PersonProfile
Coach_Career
League
Club
all linked via fields PersonProfile.pid to Coach_career.pid, Coach_career.league_id to League.league_id and coach_career.club_id to club.club.id.

What I was doing previously is doing a query in the coach_career table for any records that match the $pid via a url link to display a table showing the results with field from the coach_career table if the coach_career.league_id matched what was set in the query so to display the seperate league tables for that person I would have to duplicate the query a number of times but as the league_id is from another table wich is likely to be added to over time I would have to cintinually update the page adding extra code and queries every time. Is there a way I can code this to display them otherwise.

example is to group into a table for each league.

images attached.

<code>

$query = "Select coach_career.coach_career_id, coach_career.pid, coach_career.league_id, coach_career.club_id, coach_career.season, coach_career.pos, coach_career.games, coach_career.wins, coach_career.losses, coach_career.draws, coach_career.fingames, coach_career.finwins, coach_career.finlosses, coach_career.findraws, coach_career.gfgames, coach_career.gfwins, coach_career.gflosses, coach_career.gfdraws, coach_career.totgames, coach_career.totwins, coach_career.totlosses, coach_career.totdraws, coach_career.win_ratio, club.club, league.league From coach_career Inner Join club ON coach_career.club_id = club.club_id Inner Join league ON coach_career.league_id = league.league_id Where coach_career.pid = '$pid' Order By coach_career.season Asc, coach_career.league_id Asc";
$statement = $databaseConnection->prepare($query);
$statement->execute();
$statement->store_result();
$statement->bind_result($coach_career_id, $pid, $league_id, $club_id, $season, $pos, $games, $wins, $losses, $draws, $fingames, $finwins, $finlosses, $findraws, $gfgames, $gfwins, $gflosses, $gfdraws, $totgames, $totwins, $totlosses, $totdraws, $win_ratio, $club, $league);
if ($statement->error) {
die('Database query failed: ' . $statement->error);
}
$Exists = $statement->num_rows > 0;
if ($Exists) {
echo"<br /><table width=\"98%\">";
echo"<tr><th width=\"31%\" colspan=\"4\">Coaching Career</th><th width=\"16%\" colspan=\"4\"><center>Home & Away</center></th><th width=\"16%\" colspan=\"4\"><center>Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Grand Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Total Games</center></th><th colspan=\"2\"></th></tr>";
echo"<th class=\"category\" width=\"4%\"><center>Year</center></th><th class=\"category\" width=\"10%\">League</th><th class=\"category\" width=\"10%\">Club</th><th class=\"category\" width=\"3%\"><center>Pos</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\"width=\"5%\"><center>Edit</center></th><th class=\"category\"width=\"5%\"><center>Del</center></th>";
$alternate = "1"; 
while ($statement->fetch()) {
    if ($alternate == "1") { 
        $tbrow = "tbrow2"; 
        $alternate = "2"; 
    } else { 
        $tbrow = "tbrow1"; 
        $alternate = "1"; 
    }
    echo"<tr><td width=\"4%\" class=\"$tbrow\"><center>$season</center></td><td width=\"10%\" class=\"$tbrow\">$league</td><td width=\"10%\" class=\"$tbrow\">$club</td><td width=\"3%\" class=\"$tbrow\"><center>$pos</center></td><td width=\"3%\" class=\"$tbrow\"><center>$games</center></td><td width=\"3%\" class=\"$tbrow\"><center>$wins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$losses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$draws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$fingames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$findraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gflosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfdraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totdraws</center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='editcoachseason.php?ccid=$coach_career_id&amp;lid=$lid&amp;cid=$cid&amp;pid=$pid'><img border=0 src=./Images/db_edit.png title=Edit></a></center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='deletecoachseason.php?ccid=$coach_career_id&amp;pid=$pid&amp;alpha=$alpha&amp;pn=$pn' onClick=\"return confirm('Are you sure?')\"><img border=0 src=./Images/db_delete.png title=Delete></a></center></td></tr>"; 
} 
echo"</table>";
} else {
}

</code>

Recommended Answers

All 16 Replies

I dont think when you add records then you need to create separate code for new records.

League_id is from another table wich is likely to be added to over time I would have to cintinually update the page adding extra code and queries every time. Is there a way I can code this to display them otherwise.

Can you post another piece of code that you modify and for new league added, so that we can compare your code and give suggestions

when I add new records they only go into the coach_career table but the coach_career.league_id field is inner joined to the league table to show the actual league name by league.league.

etc:
Inner Join league ON coach_career.league_id = league.league_id

I want to display each league in a seperate table like the images attached. the first image is how it displays now and the other two are how I want them displayed.

so on the top of table, you want to show league name, and details will follow right?

yes where it says coach career in the header it will show the league name as well and the rows will display the data that mached the league_id and hopefully replicate the same thing for other league_id's in tables below.
except of course I will not need the league name column.

you should just make the stats into arrays like this array[]; (list things between the braces.

Would if I knew how to do it.
array the league_id ?

Kindly check following works or not
I have added league in order by and bring table heading rows inside loop with conditions

$query = "Select coach_career.coach_career_id, coach_career.pid, coach_career.league_id, coach_career.club_id, coach_career.season, coach_career.pos, coach_career.games, coach_career.wins, coach_career.losses, coach_career.draws, coach_career.fingames, coach_career.finwins, coach_career.finlosses, coach_career.findraws, coach_career.gfgames, coach_career.gfwins, coach_career.gflosses, coach_career.gfdraws, coach_career.totgames, coach_career.totwins, coach_career.totlosses, coach_career.totdraws, coach_career.win_ratio, club.club, league.league From coach_career Inner Join club ON coach_career.club_id = club.club_id Inner Join league ON coach_career.league_id = league.league_id Where coach_career.pid = '$pid' Order By league.league,coach_career.season Asc, coach_career.league_id Asc";
$statement = $databaseConnection->prepare($query);
$statement->execute();
$statement->store_result();
$statement->bind_result($coach_career_id, $pid, $league_id, $club_id, $season, $pos, $games, $wins, $losses, $draws, $fingames, $finwins, $finlosses, $findraws, $gfgames, $gfwins, $gflosses, $gfdraws, $totgames, $totwins, $totlosses, $totdraws, $win_ratio, $club, $league);
if ($statement->error) {
die('Database query failed: ' . $statement->error);
}
$Exists = $statement->num_rows > 0;
if ($Exists) {

$alternate = "1"; 
$prevleague="";
$rownumber=0;
while ($statement->fetch()) {
    if ($alternate == "1") { 
        $tbrow = "tbrow2"; 
        $alternate = "2"; 
    } else { 
        $tbrow = "tbrow1"; 
        $alternate = "1"; 
    }

    if($rownumber>0 && $prevleague!=$league)
    {
        echo "</table>";
    }
    if($prevleague!=$league)
    {
        echo"<br /></br><table width=\"98%\"><caption>$league</caption>";
echo"<tr><th width=\"31%\" colspan=\"4\">Coaching Career</th><th width=\"16%\" colspan=\"4\"><center>Home & Away</center></th><th width=\"16%\" colspan=\"4\"><center>Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Grand Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Total Games</center></th><th colspan=\"2\"></th></tr>";
echo"<th class=\"category\" width=\"4%\"><center>Year</center></th><th class=\"category\" width=\"10%\">League</th><th class=\"category\" width=\"10%\">Club</th><th class=\"category\" width=\"3%\"><center>Pos</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\"width=\"5%\"><center>Edit</center></th><th class=\"category\"width=\"5%\"><center>Del</center></th>";
    }

    echo"<tr><td width=\"4%\" class=\"$tbrow\"><center>$season</center></td><td width=\"10%\" class=\"$tbrow\">$league</td><td width=\"10%\" class=\"$tbrow\">$club</td><td width=\"3%\" class=\"$tbrow\"><center>$pos</center></td><td width=\"3%\" class=\"$tbrow\"><center>$games</center></td><td width=\"3%\" class=\"$tbrow\"><center>$wins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$losses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$draws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$fingames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$findraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gflosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfdraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totdraws</center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='editcoachseason.php?ccid=$coach_career_id&amp;lid=$lid&amp;cid=$cid&amp;pid=$pid'><img border=0 src=./Images/db_edit.png title=Edit></a></center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='deletecoachseason.php?ccid=$coach_career_id&amp;pid=$pid&amp;alpha=$alpha&amp;pn=$pn' onClick=\"return confirm('Are you sure?')\"><img border=0 src=./Images/db_delete.png title=Delete></a></center></td></tr>"; 

    $prevleague=$league;
    $rownumber++;
} 
echo"</table>";
} else {
}

Thanks I will give that a try, been busy reading up on creating an array etc.

YES THANKYOU!!!

It works exactly how I wanted it to work, your a champion thanks mate.

Now to get my head around what you did I will be able to apply it to other queries and tables.

Ive been doing my head in for weeks trying to work out how to do this.

So thank you once again for your help and patience.

It may be a simple task for most here but for newbies its great to have others help and understand that not everybody is an expert.

Another thing Im trying to do now is add a sum total of rows at the bottom of each table but trying to insert the code below only adds to row to the last lot of tabled records.
I must be inserting it in the wrong place.
see code below.

<code>

$query = "Select Sum(coach_career.games) AS sumgames, Sum(coach_career.wins) AS sumwins, Sum(coach_career.losses) AS sumlosses, Sum(coach_career.draws) AS sumdraws, Sum(coach_career.fingames) AS sumfingames, Sum(coach_career.finwins) AS sumfinwins, Sum(coach_career.finlosses) AS sumfinlosses, Sum(coach_career.findraws) AS sumfindraws, Sum(coach_career.gfgames) AS sumgfgames, Sum(coach_career.gfwins) AS sumgfwins, Sum(coach_career.gflosses) AS sumgflosses, Sum(coach_career.gfdraws) AS sumgfdraws, Sum(coach_career.totgames) AS sumtotgames, Sum(coach_career.totwins) AS sumtotwins, Sum(coach_career.totlosses) AS sumtotlosses, Sum(coach_career.totdraws) AS sumtotdraws From coach_career Where coach_career.pid = ? AND coach_career.league_id = ?";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('ii', $pid, $league_id);
$statement->execute();
$statement->store_result();
if ($statement->error) {
    die('Database query failed: ' . $statement->error);
}
$statement->bind_result($sumgames, $sumwins, $sumlosses, $sumdraws, $sumfingames, $sumfinwins, $sumfinlosses, $sumfindraws, $sumgfgames, $sumgfwins, $sumgflosses, $sumgfdraws, $sumtotgames, $sumtotwins, $sumtotlosses, $sumtotdraws);
$statement->fetch();
echo"<tr><td width=\"27%\" class=\"category\" colspan=\"4\">Totals</td><td width=\"3%\" class=\"category\"><center>$sumgames</center></td><td width=\"3%\" class=\"category\"><center>$sumwins</center></td><td width=\"3%\" class=\"category\"><center>$sumlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumdraws</center></td><td width=\"3%\" class=\"category\"><center>$sumfingames</center></td><td width=\"3%\" class=\"category\"><center>$sumfinwins</center></td><td width=\"3%\" class=\"category\"><center>$sumfinlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumfindraws</center></td><td width=\"3%\" class=\"category\"><center>$sumgfgames</center></td><td width=\"3%\" class=\"category\"><center>$sumgfwins</center></td><td width=\"3%\" class=\"category\"><center>$sumgflosses</center></td><td width=\"3%\" class=\"category\"><center>$sumgfdraws</center></td><td width=\"3%\" class=\"category\"><center>$sumtotgames</center></td><td width=\"3%\" class=\"category\"><center>$sumtotwins</center></td><td width=\"3%\" class=\"category\"><center>$sumtotlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumtotdraws</center></td><td width=\"10%\" class=\"category\" colspan=\"2\"> </td></tr>";

</code>

To avoid complexity, You may also try with another approach

1) query list distinct leagus of partiular pid

2) loop through leagues found above

2.1) find deatils of coaching for pid for all leages on by one

    2.1.1) loop through details of coaching

2.2) here at the end you can show sum of coaching detail rows

3) if any more league left then go to step 2 else end

Sorry Im not able to follow what you mean there.

forget it

please check following works or not

I have put your new summation code before closing table tag

$query = "Select coach_career.coach_career_id, coach_career.pid, coach_career.league_id, coach_career.club_id, coach_career.season, coach_career.pos, coach_career.games, coach_career.wins, coach_career.losses, coach_career.draws, coach_career.fingames, coach_career.finwins, coach_career.finlosses, coach_career.findraws, coach_career.gfgames, coach_career.gfwins, coach_career.gflosses, coach_career.gfdraws, coach_career.totgames, coach_career.totwins, coach_career.totlosses, coach_career.totdraws, coach_career.win_ratio, club.club, league.league From coach_career Inner Join club ON coach_career.club_id = club.club_id Inner Join league ON coach_career.league_id = league.league_id Where coach_career.pid = '$pid' Order By league.league,coach_career.season Asc, coach_career.league_id Asc";
$statement = $databaseConnection->prepare($query);
$statement->execute();
$statement->store_result();
$statement->bind_result($coach_career_id, $pid, $league_id, $club_id, $season, $pos, $games, $wins, $losses, $draws, $fingames, $finwins, $finlosses, $findraws, $gfgames, $gfwins, $gflosses, $gfdraws, $totgames, $totwins, $totlosses, $totdraws, $win_ratio, $club, $league);
if ($statement->error) {
die('Database query failed: ' . $statement->error);
}
$Exists = $statement->num_rows > 0;
if ($Exists) {

$alternate = "1"; 
$prevleague="";
$rownumber=0;
while ($statement->fetch()) {
    if ($alternate == "1") { 
        $tbrow = "tbrow2"; 
        $alternate = "2"; 
    } else { 
        $tbrow = "tbrow1"; 
        $alternate = "1"; 
    }

    if($rownumber>0 && $prevleague!=$league)
    {



        $query = "Select Sum(coach_career.games) AS sumgames, Sum(coach_career.wins) AS sumwins, Sum(coach_career.losses) AS sumlosses, Sum(coach_career.draws) AS sumdraws, Sum(coach_career.fingames) AS sumfingames, Sum(coach_career.finwins) AS sumfinwins, Sum(coach_career.finlosses) AS sumfinlosses, Sum(coach_career.findraws) AS sumfindraws, Sum(coach_career.gfgames) AS sumgfgames, Sum(coach_career.gfwins) AS sumgfwins, Sum(coach_career.gflosses) AS sumgflosses, Sum(coach_career.gfdraws) AS sumgfdraws, Sum(coach_career.totgames) AS sumtotgames, Sum(coach_career.totwins) AS sumtotwins, Sum(coach_career.totlosses) AS sumtotlosses, Sum(coach_career.totdraws) AS sumtotdraws From coach_career Where coach_career.pid = ? AND coach_career.league_id = ?";
        $statement2 = $databaseConnection->prepare($query);
        $statement2->bind_param('ii', $pid, $league_id);
        $statement2->execute();
        $statement2->store_result();
        if ($statement2->error) {
            die('Database query failed: ' . $statement2->error);
        }
        $statement2->bind_result($sumgames, $sumwins, $sumlosses, $sumdraws, $sumfingames, $sumfinwins, $sumfinlosses, $sumfindraws, $sumgfgames, $sumgfwins, $sumgflosses, $sumgfdraws, $sumtotgames, $sumtotwins, $sumtotlosses, $sumtotdraws);
        $statement2->fetch();
        echo"<tr><td width=\"27%\" class=\"category\" colspan=\"4\">Totals</td><td width=\"3%\" class=\"category\"><center>$sumgames</center></td><td width=\"3%\" class=\"category\"><center>$sumwins</center></td><td width=\"3%\" class=\"category\"><center>$sumlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumdraws</center></td><td width=\"3%\" class=\"category\"><center>$sumfingames</center></td><td width=\"3%\" class=\"category\"><center>$sumfinwins</center></td><td width=\"3%\" class=\"category\"><center>$sumfinlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumfindraws</center></td><td width=\"3%\" class=\"category\"><center>$sumgfgames</center></td><td width=\"3%\" class=\"category\"><center>$sumgfwins</center></td><td width=\"3%\" class=\"category\"><center>$sumgflosses</center></td><td width=\"3%\" class=\"category\"><center>$sumgfdraws</center></td><td width=\"3%\" class=\"category\"><center>$sumtotgames</center></td><td width=\"3%\" class=\"category\"><center>$sumtotwins</center></td><td width=\"3%\" class=\"category\"><center>$sumtotlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumtotdraws</center></td><td width=\"10%\" class=\"category\" colspan=\"2\"> </td></tr>";





        echo "</table>";
    }



    if($prevleague!=$league)
    {
        echo"<br /></br><table width=\"98%\"><caption>$league</caption>";
echo"<tr><th width=\"31%\" colspan=\"4\">Coaching Career</th><th width=\"16%\" colspan=\"4\"><center>Home & Away</center></th><th width=\"16%\" colspan=\"4\"><center>Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Grand Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Total Games</center></th><th colspan=\"2\"></th></tr>";
echo"<th class=\"category\" width=\"4%\"><center>Year</center></th><th class=\"category\" width=\"10%\">League</th><th class=\"category\" width=\"10%\">Club</th><th class=\"category\" width=\"3%\"><center>Pos</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\"width=\"5%\"><center>Edit</center></th><th class=\"category\"width=\"5%\"><center>Del</center></th>";
    }

    echo"<tr><td width=\"4%\" class=\"$tbrow\"><center>$season</center></td><td width=\"10%\" class=\"$tbrow\">$league</td><td width=\"10%\" class=\"$tbrow\">$club</td><td width=\"3%\" class=\"$tbrow\"><center>$pos</center></td><td width=\"3%\" class=\"$tbrow\"><center>$games</center></td><td width=\"3%\" class=\"$tbrow\"><center>$wins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$losses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$draws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$fingames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$findraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gflosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfdraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totdraws</center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='editcoachseason.php?ccid=$coach_career_id&amp;lid=$lid&amp;cid=$cid&amp;pid=$pid'><img border=0 src=./Images/db_edit.png title=Edit></a></center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='deletecoachseason.php?ccid=$coach_career_id&amp;pid=$pid&amp;alpha=$alpha&amp;pn=$pn' onClick=\"return confirm('Are you sure?')\"><img border=0 src=./Images/db_delete.png title=Delete></a></center></td></tr>"; 

    $prevleague=$league;
    $rownumber++;
} 
echo"</table>";
} else {
}

It works but the totals are matched to the wrong tables.
see attached screen grab image.

How about this

$query = "Select coach_career.coach_career_id, coach_career.pid, coach_career.league_id, coach_career.club_id, coach_career.season, coach_career.pos, coach_career.games, coach_career.wins, coach_career.losses, coach_career.draws, coach_career.fingames, coach_career.finwins, coach_career.finlosses, coach_career.findraws, coach_career.gfgames, coach_career.gfwins, coach_career.gflosses, coach_career.gfdraws, coach_career.totgames, coach_career.totwins, coach_career.totlosses, coach_career.totdraws, coach_career.win_ratio, club.club, league.league From coach_career Inner Join club ON coach_career.club_id = club.club_id Inner Join league ON coach_career.league_id = league.league_id Where coach_career.pid = '$pid' Order By league.league,coach_career.season Asc, coach_career.league_id Asc";
$statement = $databaseConnection->prepare($query);
$statement->execute();
$statement->store_result();
$statement->bind_result($coach_career_id, $pid, $league_id, $club_id, $season, $pos, $games, $wins, $losses, $draws, $fingames, $finwins, $finlosses, $findraws, $gfgames, $gfwins, $gflosses, $gfdraws, $totgames, $totwins, $totlosses, $totdraws, $win_ratio, $club, $league);
if ($statement->error) {
die('Database query failed: ' . $statement->error);
}
$Exists = $statement->num_rows > 0;
if ($Exists) {

$alternate = "1"; 
$prevleague="";
$rownumber=0;
while ($statement->fetch()) {
    if ($alternate == "1") { 
        $tbrow = "tbrow2"; 
        $alternate = "2"; 
    } else { 
        $tbrow = "tbrow1"; 
        $alternate = "1"; 
    }

    if($rownumber>0 && $prevleague!=$league)
    {
        echo $sumhtml;
        echo "</table>";
    }



    if($prevleague!=$league)
    {
        echo"<br /></br><table width=\"98%\"><caption>$league</caption>";
echo"<tr><th width=\"31%\" colspan=\"4\">Coaching Career</th><th width=\"16%\" colspan=\"4\"><center>Home & Away</center></th><th width=\"16%\" colspan=\"4\"><center>Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Grand Finals</center></th><th width=\"16%\" colspan=\"4\"><center>Total Games</center></th><th colspan=\"2\"></th></tr>";
echo"<th class=\"category\" width=\"4%\"><center>Year</center></th><th class=\"category\" width=\"10%\">League</th><th class=\"category\" width=\"10%\">Club</th><th class=\"category\" width=\"3%\"><center>Pos</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\" width=\"3%\"><center>Gms</center></th><th class=\"category\" width=\"3%\"><center>W</center></th><th class=\"category\" width=\"3%\"><center>L</center></th><th class=\"category\" width=\"3%\"><center>D</center></th><th class=\"category\"width=\"5%\"><center>Edit</center></th><th class=\"category\"width=\"5%\"><center>Del</center></th>";


  $query = "Select Sum(coach_career.games) AS sumgames, Sum(coach_career.wins) AS sumwins, Sum(coach_career.losses) AS sumlosses, Sum(coach_career.draws) AS sumdraws, Sum(coach_career.fingames) AS sumfingames, Sum(coach_career.finwins) AS sumfinwins, Sum(coach_career.finlosses) AS sumfinlosses, Sum(coach_career.findraws) AS sumfindraws, Sum(coach_career.gfgames) AS sumgfgames, Sum(coach_career.gfwins) AS sumgfwins, Sum(coach_career.gflosses) AS sumgflosses, Sum(coach_career.gfdraws) AS sumgfdraws, Sum(coach_career.totgames) AS sumtotgames, Sum(coach_career.totwins) AS sumtotwins, Sum(coach_career.totlosses) AS sumtotlosses, Sum(coach_career.totdraws) AS sumtotdraws From coach_career Where coach_career.pid = ? AND coach_career.league_id = ?";
        $statement2 = $databaseConnection->prepare($query);
        $statement2->bind_param('ii', $pid, $league_id);
        $statement2->execute();
        $statement2->store_result();
        if ($statement2->error) {
            die('Database query failed: ' . $statement2->error);
        }
        $statement2->bind_result($sumgames, $sumwins, $sumlosses, $sumdraws, $sumfingames, $sumfinwins, $sumfinlosses, $sumfindraws, $sumgfgames, $sumgfwins, $sumgflosses, $sumgfdraws, $sumtotgames, $sumtotwins, $sumtotlosses, $sumtotdraws);
        $statement2->fetch();
        $sumhtml="<tr><td width=\"27%\" class=\"category\" colspan=\"4\">Totals</td><td width=\"3%\" class=\"category\"><center>$sumgames</center></td><td width=\"3%\" class=\"category\"><center>$sumwins</center></td><td width=\"3%\" class=\"category\"><center>$sumlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumdraws</center></td><td width=\"3%\" class=\"category\"><center>$sumfingames</center></td><td width=\"3%\" class=\"category\"><center>$sumfinwins</center></td><td width=\"3%\" class=\"category\"><center>$sumfinlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumfindraws</center></td><td width=\"3%\" class=\"category\"><center>$sumgfgames</center></td><td width=\"3%\" class=\"category\"><center>$sumgfwins</center></td><td width=\"3%\" class=\"category\"><center>$sumgflosses</center></td><td width=\"3%\" class=\"category\"><center>$sumgfdraws</center></td><td width=\"3%\" class=\"category\"><center>$sumtotgames</center></td><td width=\"3%\" class=\"category\"><center>$sumtotwins</center></td><td width=\"3%\" class=\"category\"><center>$sumtotlosses</center></td><td width=\"3%\" class=\"category\"><center>$sumtotdraws</center></td><td width=\"10%\" class=\"category\" colspan=\"2\"> </td></tr>";

    }

    echo"<tr><td width=\"4%\" class=\"$tbrow\"><center>$season</center></td><td width=\"10%\" class=\"$tbrow\">$league</td><td width=\"10%\" class=\"$tbrow\">$club</td><td width=\"3%\" class=\"$tbrow\"><center>$pos</center></td><td width=\"3%\" class=\"$tbrow\"><center>$games</center></td><td width=\"3%\" class=\"$tbrow\"><center>$wins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$losses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$draws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$fingames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$finlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$findraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gflosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$gfdraws</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totgames</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totwins</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totlosses</center></td><td width=\"3%\" class=\"$tbrow\"><center>$totdraws</center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='editcoachseason.php?ccid=$coach_career_id&amp;lid=$lid&amp;cid=$cid&amp;pid=$pid'><img border=0 src=./Images/db_edit.png title=Edit></a></center></td><td width=\"5%\" class=\"$tbrow\"><center><a href='deletecoachseason.php?ccid=$coach_career_id&amp;pid=$pid&amp;alpha=$alpha&amp;pn=$pn' onClick=\"return confirm('Are you sure?')\"><img border=0 src=./Images/db_delete.png title=Delete></a></center></td></tr>"; 

    $prevleague=$league;
    $rownumber++;
} 
echo $sumhtml;
echo"</table>";
} else {
}

Yes very well done.
Tested it with nine different league tables at once.

Thanks again for all your wonderful help and work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.