Sorry @requinix I'm not that good with this, it just looks a mess to me! Here's the whole thing
<?php
$competitions=array();
$competitions[]="Championship";
$competitions[]="Division One";
$competitions[]="Division One (old)";
$competitions[]="Division Two (old)";
$competitions[]="League One";
$competitions[]="Midland League";
$competitions[]="Premier League";
$competitions[]="South Regional League";
$competitions[]="Southern Section";
$competitions[]="Midland Division";
$competitions[]="North";
$competitions[]="South";
$competitions[]="Midland Section";
$competitions[]="FA Cup";
$competitions[]="League Cup";
$competitions[]="League Trophy";
$competitions[]="UEFA Champions League";
$competitions[]="UEFA Cup";
$competitions[]="UEFA Cup Winners Cup";
$competitions[]="Anglo-Italian Cup";
$competitions[]="Members Cup";
$competitions[]="War Cup";
$q="select max(date) as max_date,min(date) as min_date from seasonstats";
$qr=mysqli_query($ccppdbc, $q) or die($q.mysqli_error($ccppdbc));
$r=mysqli_fetch_object($qr);
$max_date=$r->max_date;
$max_dates=explode('-',$max_date);
$maxdate=$max_dates[0]+1;
$min_date=$r->min_date;
$min_dates=explode('-',$min_date);
$mindate=$min_dates[0];
$session_option="<select name=session class=jmp onchange='submit_form();'>";
$tr=0;
$records="<div class='mainBox box_inner--border secondary table-responsive'><h1 class='history_title'>Top goalscorers</h1>
<table class='table' cellspacing='0'>
<tr class='ipsButtonBar ipsType_reset'>
<th width=8%>Season</th>
<th width=8%>Goals</th>
<th width=84%>Players</td></th>";
for($j=$mindate;$j<='2017';$j++)
{
$value=substr($j+1,-2 );
$session_player=array();
$q="select t.playerid,t.goals,p.firstname,p.lastname,p.playerid from topscorers as t,players as p where t.goals is true and season='$j' and t.playerid=p.playerid";
$qr=mysqli_query($ccppdbc,$q) or die($q.mysqli_error($ccppdbc));
while($r=mysqli_fetch_object($qr))
{
$pid=$r->playerid;
$goals=$r->goals;
$firstname=utf8_encode($r->firstname);
$lastname=utf8_encode($r->lastname);
$session_player[$pid]="$firstname $lastname";
}
$total_record=count($session_player);
$i=0;
$palyerslink='';
foreach($session_player as $var=>$val)
{
$i++;
if($i==$total_record)
$palyerslink.="<a href=/history/players?pid=$var title='view this in more detail'>$val</a>";
else
$palyerslink.="<a href=/history/players?pid=$var title='view this in more detail'>$val</a>, ";
}
if(!empty($session_player))
{
$tr++;
$records.="<tr class=ftrow>
<td class=border-right><a href='/history/browse?season=$j'>$j-$value</a></td>
<td class=border-right>$goals</td>
<td>$palyerslink</td></tr>";
}
}
for($j='2018';$j<=$maxdate;$j++)
{
$value=substr($j+1,-2 );
//$val=$i;
if($j==1890) continue;
$session=$j;
$session1=$session+1;
$session_value=$session.'-08-01';
$session1_value=$session1.'-06-31';
$total_goals=0;
$match_ids=array();
$session_player=array();
$q="select id from seasonstats where date >='$session_value' and date <='$session1_value' order by date";
$qr=mysqli_query($ccppdbc, $q) or die($q.mysqli_error($ccppdbc));
while($r=mysqli_fetch_object($qr))
{
$match_ids[]=$r->id;
}
if(!empty($match_ids))
{
$match_id_list=implode(',',$match_ids);
$players_ids=array();
$q="select distinct(_player_id) from seasonteamstats where matchid in ($match_id_list) order by lastname";
$qr=mysqli_query($ccppdbc, $q) or die($mysqli_error($ccppdbc));
while($r=mysqli_fetch_object($qr))
{
//$firstname=$r->firstname;
//$lastname=$r->lastname;
$playerid=$r->_player_id;
$players_ids[$playerid]=$playerid;
}
//$players_names=array_unique($players_names);
$total_players=count($players_ids);
$max_goal_season=0;
foreach($players_ids as $var=>$val)
{
$total_goals=0;
$total_penalties=0;
$q="select * from seasonteamstats where _player_id='$val' and matchid in ($match_id_list) order by lastname";
$qr=mysqli_query($ccppdbc, $q) or die($q.mysqli_error($ccppdbc));
while($r=mysqli_fetch_object($qr))
{
$firstname=utf8_encode($r->firstname);
$lastname=utf8_encode($r->lastname);
$pid=$r->_player_id;
if($session>=1915 and $session<=1918)
{
$goals=$r->wargoals1;
$penalties=$r->warpenalties1;
}
elseif($session>=1939 and $session<=1945)
{
$goals=$r->wargoals;
$penalties=$r->warpenalties; }
else
{
$goals=$r->goals;
$penalties=$r->penalties;
}
$total_goals+=$goals+$penalties;
}
if($max_goal_season==$total_goals)
{
$session_player[$pid]="$firstname $lastname";
}
else if($max_goal_season<$total_goals)
{
$session_player=array();
$max_goal_season=$total_goals;
$session_player[$pid]="$firstname $lastname";
}
}
}
$total_record=count($session_player);
$i=0;
$palyerslink="";
foreach($session_player as $var=>$val)
{
$i++;
if($i==$total_record)
$palyerslink.="<a href='/history/players?pid=$var' title='view this in more detail'>$val</a>";
else
$palyerslink.="<a href='/history/players?pid=$var' title='view this in more detail'>$val</a>, ";
}
if(!empty($session_player))
{
$tr++;
$records.="<tr class=ftrow>
<td class=border-right><a href='/history/browse?season=$j'>$j-$value</a></td>
<td class=border-right>$max_goal_season</td>
<td>$palyerslink</td>
</tr>";
}
}
$records.="</table></div>";
echo $records;
?>