exzrael
03-18-2006, 08:46 AM
Im currently working on a way to paginate database-results I want. This code shows results but not quite functionable yet. Any suggestions?
This code shows the first five entrys in the database. Hitting Next gives the link http://localhost/test.php?start=-5 and Prev gives the same but without the -.
<?
if ( $_GET['number'] == NULL ){
$limit = 5;
} else {
$limit = $_GET['number'];
}
if ($_GET['page']==NULL){
$start = 0;
} else {
$start = $_GET['page'];
}
if($_GET['page'] < 0){
header("Location: test.php?page=0&limit=".$limit);
}
@mysql_connect("localhost","root","");
mysql_select_db("linklist");
$query = mysql_query("SELECT id_col, title FROM links ORDER BY id_col LIMIT $start, $limit");
while ($row = mysql_fetch_array ($query) ) {
echo '<p>'.$row['title'].'</p>';
}
$previous = $start + $limit;
$next = $start - $limit;
echo '<a href="test.php?start='.$previous.'">Previous Page</a> - ';
echo '<a href="test.php?start='.$next.'">Next Page</a>';
?>
<?
echo '<a href="test.php?page='.$start.'&limit=10">Display 10 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=20">Display 20 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=30">Display 30 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=40">Display 40 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=40">Display 40 Records</a>';
?>
This code shows the first five entrys in the database. Hitting Next gives the link http://localhost/test.php?start=-5 and Prev gives the same but without the -.
<?
if ( $_GET['number'] == NULL ){
$limit = 5;
} else {
$limit = $_GET['number'];
}
if ($_GET['page']==NULL){
$start = 0;
} else {
$start = $_GET['page'];
}
if($_GET['page'] < 0){
header("Location: test.php?page=0&limit=".$limit);
}
@mysql_connect("localhost","root","");
mysql_select_db("linklist");
$query = mysql_query("SELECT id_col, title FROM links ORDER BY id_col LIMIT $start, $limit");
while ($row = mysql_fetch_array ($query) ) {
echo '<p>'.$row['title'].'</p>';
}
$previous = $start + $limit;
$next = $start - $limit;
echo '<a href="test.php?start='.$previous.'">Previous Page</a> - ';
echo '<a href="test.php?start='.$next.'">Next Page</a>';
?>
<?
echo '<a href="test.php?page='.$start.'&limit=10">Display 10 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=20">Display 20 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=30">Display 30 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=40">Display 40 Records</a>';
echo '<a href="test.php?page='.$start.'&limit=40">Display 40 Records</a>';
?>