Take the 2-minute tour ×

I have http://communitychessclub.com/games-nu.php with a game scores table.

And I want the user to be able to scroll from game to game. The games were sequentially numbered, but many had to be removed.

So instead of "game1001.php" and "game1002.php", I have "game1203.php" and "game1007.php" The problem is that each game has L-R icons for scrolling forward and backward to other games.

<?php $file = $_SERVER["SCRIPT_NAME"]; $game = substr($file,11,4);

$filename = '../games.csv'; if (file_exists($filename)) 
{$last_mod = filemtime($filename);
if (!($games_date == $last_mod)) 
{$games_date = $last_mod; $lines = count(file($filename));}} 

if ($game==1001){$game_prev=1000+$lines;} else {$game_prev = $game-1;}
if ($game==1000+$lines){$game_next=1001;} else {$game_next = $game+1;} 

echo "<div style=\"margin:0; padding:0\">"; 
echo "<a href =\"games/game$game_prev"; echo ".php\"> 
<img src=\"images/go-back.png\" alt=\"next\" class=\"atc-L\"></a>";
echo "<a href =\"games/game$game_next"; echo ".php\"> 
<img src=\"images/go-next.png\" alt=\"next\" class=\"atc-R\"></a>";
echo "</div>";

and games.csv is:


I would like to add and access elements in $list

$list = array (1231, 1227, 1223, 1222, 1185, 1166, 1163, 1013, 1007, 1002);

I would like to place these numbers in array and sort them and them access them with the php script above. I don't mind if all the numbers are on one line, just as long has php can array them. How can I create such an array and have the php code above scroll forward and backward, say from game1223.php to game1227.php.

share|improve this question
please tell me game 1223.php and game 1227.php are using the same file and you're using htaccess or something to handle the url request. Or at least have one awesome explanation for having each game an individual php file. Your problem is very easy if it is a common file, database driven or something dynamic. –  Kai Qing Dec 1 '11 at 21:34
communitychessclub.com/games/game1223.php and communitychessclub.com/games/game1227.php are separate files. But the overhead for each file is minimal. Maybe I should look into having the moves ("pgn") portable game notation as a php include from a mysql file. (But I don't know how to do that.) –  verlager Dec 1 '11 at 21:56
add comment

1 Answer

up vote 0 down vote accepted

Assuming you have the array $list defined then change next/prev lines to:

$index = array_search($game, $list);
$max = count($list)-1;
if ($index == 0) {$index_prev = $max;} else {$index_prev = $index-1;}
if ($index == $max) {$index_next = 0;} else {$index_next = $index+1;}
$game_prev = $list[$index_prev];
$game_next = $list[$index_next];
share|improve this answer
Excellent! Thank you very much! –  verlager Dec 1 '11 at 23:22
add comment

Your Answer


