this is a basic script to load content from rows of out of a database with paging system
it is mainly meant to teach and show you how to do such a thing
enjoy the script
PHP Code:
<?php
/*by Godkillah
http://www.phantom-designs.net
http://www.vbdeals.com*/
define("MyHost",'localhost');
define("MyDB",'MyDatabase');
define("Username",'myusername');
define("Password",'mypassword');
define("TABLEOFDOOM",'MyTable');
define("DoomedResults",25);
//No further edits required
$connect = mysql_connect(DBHOST,DBUSER,DBPASS) or die('<script type="text/javascript">ar=["block","none"];i=0;function showdetails(){document.getElementById("errordetails").style.display=ar[i]; if(i>0){ i--; }else{ i++;}}</script>Failed to connect to host,<Br>for more details click <a href="javascript:void(0)" onclick="showdetails()">here</a><div id="errordetails" style="display:none"><i>'.mysql_error().'</i></div>');
if(!$connect){
exit();
}
$connect2=mysql_select_db(DBNAME, $connect) or die('<script type="text/javascript">ar=["block","none"];i=0;function showdetails(){document.getElementById("errordetails").style.display=ar[i]; if(i>0){ i--; }else{ i++;}}</script>Failed to connect to database,<Br>for more details click <a href="javascript:void(0)" onclick="showdetails()">here</a><div id="errordetails" style="display:none"><i>'.mysql_error().'</i></div>');
if(!$connect2){
exit();
}
//from here the paging starts
$resultsperpage=DoomedResults;
$curpage=1;
if(isset($_GET['page'])){
$curpage=$_GET['page'];
}
$offset=($curpage-1)*$resultsperpage;
$result=mysql_query("SELECT * FROM ".TABLEOFDOOM." LIMIT ".$offset.", ".$resultsperpage.";");
$r = mysql_query("SELECT * FROM ".TABLEOFDOOM.";");
$rows=mysql_num_rows($r);
$pages1=$rows/$resultsperpage;
$pages=round($pages1);
if($pages1>$pages){
$pages++;
}
$listo='<b>Pages: </b>';
if($curpage!=1){
$listo.=' [<a href="index.php?action=list&page='.($curpage-1).'">Previous</a>]';
}
for($i=1;$i<=$pages;$i++){
if($curpage==$i){
$listo.=' <b>['.$curpage.']</b>';
}else{
$listo.=' [<a href="index.php?action=list&page='.$i.'">'.$i.'</a>]';
}}
if($curpage!=$pages && $pages!=0){
$listo.=' [<a href="index.php?action=list&page='.($curpage+1).'">Next</a>]';
}
$addstuff='';
if($pages==0){
$listo.=' <b>[1]</b>';
$pages++;
$addstuff='There is currently no list!';
}
$listo.=' ('.$pages.' total)';
$conts=''
while($row=mysql_fetch_array($result)){
$conts.='<div>'.$row['mylistitem'].'</div>';
}
$conts.=$addstuff;
//time to write both the page browsing links and the listitems
echo $listo;
echo $conts;
echo $listo;
//$listo is the page links, $conts is the loaded list items
?>