alxne Posted May 29, 2020 Share Posted May 29, 2020 (edited) I have quite a long .php page but I've attached the end of it. I was wondering if I can change the order it is displayed. Basically $j is a year. And currently it goes from 1894 until 2019. I don't think anything is currently telling it to sort either way, it just by default shows that way. But I want it to show 2019 first and go down to 1894. I had this written for me by someone who I can no longer get hold of. $total_record=count($session_player); $i=0; $palyerslink=""; foreach($session_player as $var=>$val) { $i++; if($i==$total_record) $palyerslink.="<a href='/history/?pid=$var' title='view this in more detail'>$val</a>"; else $palyerslink.="<a href='/history/?pid=$var' title='view this in more detail'>$val</a>, "; } if(!empty($session_player)) { $tr++; $records.="<tr class=row> <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; ?> This is the end of the page where the records are then displayed using the echo. I'm not sure if more code is needed though, I could post it in full if needed Edited May 29, 2020 by alxne Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/ Share on other sites More sharing options...
requinix Posted May 29, 2020 Share Posted May 29, 2020 If you want to change the values that $j gets then you need to change the code that gives values to $j. That will have been somewhere earlier in the script. Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578478 Share on other sites More sharing options...
alxne Posted May 29, 2020 Author Share Posted May 29, 2020 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578481 Share on other sites More sharing options...
requinix Posted May 29, 2020 Share Posted May 29, 2020 for($j=$mindate;$j<='2017';$j++) { That's what you need to change. It's currently counting from $mindate to 2017 by adding one to $j (that's what $j++ means) each time. You want that to instead count from 2019 to $mindate by subtracting one from $j (that would be $j--) each time. When you have that working, why 2019 specifically? Is it because that was last year? Since it says 2017 now, does that mean someone forgot to update it last year to say 2018? If you want the year to be dependent on the current year then you can have PHP do that automatically instead of having to edit the script... Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578482 Share on other sites More sharing options...
alxne Posted May 29, 2020 Author Share Posted May 29, 2020 Anytime I change that piece of code the page crashes. Probably as I don't know how it should be 2017 doesn't matter, it still works for years after that. Basically all data up for 2017 is stored in the database, but everything after is generated. I did that mainly to save page times. Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578484 Share on other sites More sharing options...
requinix Posted May 29, 2020 Share Posted May 29, 2020 Just now, alxne said: Anytime I change that piece of code the page crashes. Probably as I don't know how it should be So how about posting what you tried so you can find out what was wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578485 Share on other sites More sharing options...
alxne Posted May 29, 2020 Author Share Posted May 29, 2020 for($j<='2017';$j=$mindate;$j--) for($j<='2017';$j=$mindate;$j++) tried both of those and neither worked, tried the latter to see if it was the -- stopping it working but it wasn't Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578486 Share on other sites More sharing options...
requinix Posted May 29, 2020 Share Posted May 29, 2020 The syntax of a for loop is for (<initialization>; <condition to check>; <action to take on each loop>) { ... } For you, The initialization would be starting the $j variable at 2017 The condition to check is that $j is greater than or equal to (since it will be counting down) $mindate The action is to decrement (--) $j Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578489 Share on other sites More sharing options...
alxne Posted May 29, 2020 Author Share Posted May 29, 2020 (edited) Thanks for the explanation but it's beyond my ability to figure out to make it work Edited May 29, 2020 by alxne Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578491 Share on other sites More sharing options...
Barand Posted May 29, 2020 Share Posted May 29, 2020 This summarises a section of your code ... for($j='2018';$j<=$maxdate;$j++) { $q="select id from seasonstats where date >='$session_value' and date <='$session1_value' order by date"; while($r=mysqli_fetch_object($qr)) { $q="select distinct(_player_id) from seasonteamstats where matchid in ($match_id_list) order by lastname"; while($r=mysqli_fetch_object($qr)) { foreach($players_ids as $var=>$val) { $q="select * from seasonteamstats where _player_id='$val' and matchid in ($match_id_list) order by lastname"; } } } } Do you find it takes a while to run? You need to learn to use JOINs and you can do all this in one efficient query. The golden rule is "Don't run queries inside loops" Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578494 Share on other sites More sharing options...
alxne Posted May 29, 2020 Author Share Posted May 29, 2020 51 minutes ago, Barand said: Do you find it takes a while to run? The page loads within a few seconds. I didn't write it, as mentioned earlier I can't get hold of who wrote it for me. Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578497 Share on other sites More sharing options...
archive Posted May 30, 2020 Share Posted May 30, 2020 The page loads within a few seconds. A few seconds is probably a long time? $j is not used in your script and has no effect on any of the code following it which is why it hasn't been changed and why you haven't noticed, is this causing other problems though? As Barand points out, really you should look change the query which would allow you to arrange the results in descending order as well as joining all the queries together in one. Barand: for($j='2018';$j<=$maxdate;$j++) { Should that have been: for($j='2018';$j<=$maxdate;$j--) { Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578517 Share on other sites More sharing options...
Barand Posted May 30, 2020 Share Posted May 30, 2020 2 hours ago, archive said: Should that have been: I didn't say is was right - it's just what was in the code I was condensing to illustrate the nested loops. Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578519 Share on other sites More sharing options...
archive Posted May 30, 2020 Share Posted May 30, 2020 6 minutes ago, Barand said: I didn't say is was right - it's just what was in the code I was condensing to illustrate the nested loops. Yeah, I wasn't trying to be clever, I wasn't sure if I'd missed something. I had intended to edit the post after clarification but I can't now, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/310861-change-the-order/#findComment-1578521 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.