Jeg har nu i flere timer siddet og forsøgt at få mit paging til at virke, men må nu have lidt hjælp.
Min kode ser således ud:
<?php
$db = mysql_connect("localhost","brugernavn", "password");
mysql_select_db("database",$db);
$pager = new pager("SELECT * FROM test order by id DESC,10");
while($row = mysql_fetch_array($pager->result))
{
print "<table>
<tr>
<td>
<img src='$row[bigpic]'>
</td>
<td>
<table>
<tr>
<td><img src='$row[smallpic1]'><td>
</tr>
</table>
<table>
<tr>
<td><img src='$row[smallpic2]'><td>
</tr>
</table>
<table>
<tr>
<td><img src='$row[smallpic3]'><td>
</tr>
</table>
</td>
<td>
<b>Videooplysninger</b><br><br>
Spilletid:<br><b>$row[time]</b><br><br>
Størrelse:<br><b>$row[storrelse] MB</b><br><br><br>
<a href='$row[trymovie]'>Se prøvefilm</a><br><br>
<a href='$row[link]'>Bliv medlem nu!</a>
</td>
</tr>
</table><br>";
}
echo $pager->show();
class pager
{
var $sql;
var $getvar;
var $rows;
var $start;
var $getvar_val;
var $pager;
var $result;
function __construct($sql,$getvar,$length)
{
$this->result=null;
$this->sql = $sql;
$this->getvar = $getvar;
$this->rows = 0;
$this->start = 0;
$this->getvar_val = 1;
$this->pager = null;
$this->SetLength($length);
$this->GetStart();
$this->doQuery();
}
//Sets $length
function SetLength($length)
{
$this->length = (int)$length;
if($this->length<0)
{
$this->length = 0;
}
}
function doQuery()
{
$sql = trim($this->sql);
$sql = substr($sql,6);
$sql = 'SELECT SQL_CALC_FOUND_ROWS '.$sql.' limit '.$this->start.', '.$this->length;
$this->result = mysql_query($sql);
$sql = "SELECT FOUND_ROWS()";
$result = mysql_query($sql);
$this->rows = mysql_result($result,0);
}
//getvar_val() gets the
//$getvar_val is a positive integer - > 0
function Set_Getvar_val()
{
$this->getvar_val = (int)$_GET[$this->getvar];
if($this->getvar_val<1)
{
$this->getvar_val = 1;
}
}
//Gets the start of the data
function GetStart()
{
$this->Set_Getvar_val();
$this->start = (($this->getvar_val-1)*$this->length);
}
function show($next="Next",$last="Last",$separator=" ")
{
$array = $this->pager();
$str = array();
foreach($array as $k => $v)
{
if($k!='next'&&$k!='last')
{
if($k==$this->getvar_val)
{
$str[] = $k;
}
else
{
$str[] = '<a href="'.$v.'">'.$k.'</a>';
}
}
}
$str = implode($separator, $str);
$rt = $array['last']===null?"":'<a href="'.$array['last'].'">'.$last.'</a>'.$separator;
$rt .= $str.$separator;
$rt .= $array['next']===null?"":'<a href="'.$array['next'].'">'.$next.'</a>';
return $rt;
}
//Returns an array of the links arround the given point
//['next'] => next page
//['last'] => last page
function pager($margin=10)
{
$path = $_GET;
$newpath = $PHP_SELF."?";
foreach($path as $key => $value)
{
if($key!=$this->getvar)
{
$newpath .= $key."=".$value;
$newpath .="&";
}
}
$newpath .= $this->getvar;
$linkpaths = array();
$current = $this->start / $this->length + 1;
$pages = ceil(($this->rows/$this->length));
$pagerstart = $current-$margin;
$pagerstart = ($pagerstart<1)?1:$pagerstart;
$pagerend = $current+$margin;
$pagerend = ( $pagerend > $pages ) ? ( ceil( ( $this->rows / $this->length ) ) ): $pagerend;
for($i=$pagerstart;$i < ($pagerend+1);$i++)
{
$linkpaths[$i] = $newpath."=".($i);
}
if($linkpaths[($current+1)]!=null)
{
$linkpaths['next']=$linkpaths[($current+1)];
}
if($linkpaths[($current-1)]!=null)
{
$linkpaths['last']=$linkpaths[($current-1)];
}
return $linkpaths;
}
}
?>
Jeg får følgende fejl:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mounted-storage/home28a/sub002/sc25494-GAJW/www/sexsites/sexunion.dk/www/test/index2.php on line 8
Scriptet skulle gerne vise 10 på hver side.