Jump to content

Modify Paging Method


xcsresources

Recommended Posts

I currently have a really cheap a*s paging system and Im looking to replace it with a better one. My current method is shown in the code below and looks like this: (Go to page: 123456789101112131415)..etc.. eeeeewww:mad: Sorry if the code is extensive, Im unsure what does what, I did my best. Here is the myphotos.php file

 

//Liet ke danh sach
$pagesize = $site_config->page_size;
$page = isset ($_GET["p"])?(int)$_GET["p"]:0;

if ($re = $file->GetAllinPage($_SESSION["member_sid"], $page*$pagesize, $pagesize,1))
{
	$col = 0;
	while ($dr = mysql_fetch_array($re))
	{
		$col = $col>3?1:$col+1;

		if ($col == 1)
		{
			$template->assign_block_vars("file_row",array(

			));
		}

		$filelink = "";
		if (ereg("http://", $dr["fileurl"]))
		{
			$filelink = $dr["fileurl"];
		}
		else
		{
			$filelink = YOURDOMAIN."/".$dr["fileurl"];
		} 

		$template->assign_block_vars("file_row.file_col",array(
			"ID" => $dr["id"],
			"FILEURL" => $filelink,
			"TBFILEURL" => $dr["filethmb"],
			"FILENAME" => $dr["filename"],
			"FILESIZE" => number_format($dr["filesize"])
		));

	}


$pagelist = "";
	$n = $file->TotalFiles($_SESSION["member_sid"],1);
	for ($i=0; $i<ceil($n/$pagesize); $i++)
	{
		$j = $i+1;
		$pagelist .= ($i==$page)?"$j  ":"<a href='myphotos.php?p=".$i."'>$j</a> ";
	}			
	$template->assign_vars(array(
		'PAGELIST' => $pagelist!= ""?"<span class='tdtitle'>Go to page: </span>".$pagelist:""

	));    
}

$template->pparse("myphotos");

    	include_once ("footer.php");    
                                                                      
?>

 

 

Here is the code on my class.file.php

//GetAllinPage : get all result in Limitation

function GetAll($start, $pagesize)

{

	//Retrive rows $start+1 -> $pagesize+$start

	$sql = "SELECT b.*, m.*

			FROM `file` as b, `member` as m

			WHERE b.`id` = m.`id`				

                ORDER BY b.id DESC                                

			LIMIT ".$start.",".$pagesize;

	return $this->runQuery($sql);				

}



//GetAllinPage : get all result in Limitation

function GetAllinPage($usersid, $start, $pagesize, $type)

{

	//Retrive rows $start+1 -> $pagesize+$start

	$sql = "SELECT *

			FROM `file`

			WHERE `usersid` = '".$usersid."' and `filetype`='".$type."' 				

                ORDER BY id DESC                                

			LIMIT ".$start.",".$pagesize;



	return $this->runQuery($sql);				

}



//GetTotalinPage : get all result in Limitation

function TotalFiles($usersid, $type)

{

	$sql = "SELECT count(*)

			FROM `file`

			WHERE `usersid` = '".$usersid."' and `filetype`=".$type;



	$r = mysql_fetch_array($this->runQuery($sql));

	return $r[0];

}



//GetAllinPage : get all result in Limitation

function GetFileinPage($start, $pagesize, $where)

{

	//Retrive rows $start+1 -> $pagesize+$start

	$sql = "SELECT *

			FROM `file` ".$where."

                ORDER BY id DESC                                

			LIMIT ".$start.",".$pagesize;



	return $this->runQuery($sql);				

}



//GetTotalinPage : get all result in Limitation

function GetTotalFiles($where)

{

	$sql = "SELECT count(*)

			FROM `file` ".$where;



	$r = mysql_fetch_array($this->runQuery($sql));

	return $r[0];

}

}

?>

 

 

 

Im looking to replace the above with this instance but Im not sure how to integrate it properly :( If anyone can help me do this or let me know if I fall short of information needed to do this?

like this:

 

[1] 2 3 4 5 [Next] [Last]

 

[First] [Prev] 1 [2] 3  4  5[Next] [Last]

 

[First] [Prev] 1  2 [3] 4  5[Next] [Last]

 

[First] [Prev] 2  3 [4] 5  6[Next] [Last]

 

[First] [Prev] 2  3  4 [5] 6[Next] [Last]

 

[First] [Prev] 2  3  4  5  [6]

 

 <?php
include 'library/config.php';
include 'library/opendb.php';

// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query = " SELECT val FROM randoms " .
         " LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while($row = mysql_fetch_array($result))
{
   echo $row['val'] . '<br>';
}
// how many rows we have in database
$query   = "SELECT COUNT(val) AS numrows FROM randoms";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];

//only display page numbers: n limit numbers above, and n limit numbers below the current page

//(not going before 0 or past the max number)

$pagetotal = 7; // maximum page numbers to display at once, must be an odd number

$pagelimit = ($pagetotal-1)/2;

$pagemax = $pagetotal>$maxpage?$maxpage:$pagetotal;

if ($pagenum - $pagelimit < 1) {

$pagemin = 1;

}

if ($pagenum - $pagelimit >=1 && $pagenum + $pagelimit <= $maxpage) {

$pagemin = $pagenum - $pagelimit;

$pagemax = $pagenum + $pagelimit;

}

if ($pagenum - $pagelimit >=1 && $pagenum + $pagelimit > $maxpage) {

$pagemin = ($maxpage-$pagetotal+1)<1?1:($maxpage-$pagetotal+1);

$pagemax = $maxpage;

}

if ($pagenum + $pagelimit > $maxpage) {

$pagemax = $maxpage;

} 

for($page = $pagemin; $page <= $pagemax; $page++)

{

  if ($page == $pageNum)

  {

     $nav .= " $page "; // no need to create a link to current page

  }

  else

  {

     $nav .= " <a href=\"$self?page=$page\">".floor($page)."</a> ";

  }

}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
   $page  = $pageNum - 1;
   $prev  = " <a href=\"$self?page=$page\">[Prev]</a> ";

   $first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
   $prev  = ' '; // we're on page one, don't print previous link
   $first = ' '; // nor the first page link
}

if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"$self?page=$page\">[Next]</a> ";

   $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
   $next = ' '; // we're on the last page, don't print next link
   $last = ' '; // nor the last page link
}

// print the navigation link
echo $first . $prev .
" Showing page $pageNum of $maxPage pages " . $next . $last;

// and close the database connection
include '../library/closedb.php';

// ... and we're done!
?>

Link to comment
https://forums.phpfreaks.com/topic/191535-modify-paging-method/
Share on other sites

Fatal error: Call to a member function resource() on a non-object in /home/nwhost/public_html/pixelpocket/myphotos.php on line 127

 

uuuuugh I SUCK!! lol

 

include_once("header.php");
    include_once("m_menu.php");  
    include_once("class/file.class.php");           
    include_once("class/paginate.class.php");     //Include

$template->set_filenames(array("myphotos" => "myphotos.html"));  
    
$file = new Files();

$inform = "";

if (isset($_POST["delete"]) && isset($_GET["id"]) && $_GET["id"] >0)
{	//Xoa file
$file -> DeleteFile($_GET["id"]);
}else
if (isset($_POST["edit"]) && isset($_GET["id"]) && $_GET["id"] >0)
{	

if ($dr = mysql_fetch_array($file -> GetById($_GET["id"])))
{
	$template->assign_vars(array(
		"ID" => $dr["id"],
		"FILEURL" => $dr["fileurl"],
		"FILENAME" => $dr["filename"], 
		"FILESIZE" => $dr["filesize"],
		"THUMBNAIL" => "<img src=\"".$dr["filethmb"]."\" style=\"\" border=\"0\" height=\"101\" width=\"135\">"
	));
}
}   

//insert and upload photo
if ($r=mysql_fetch_array($member->GetBySId($_SESSION["member_sid"])))
{
$quote_use = $r["quot_use"];
$limit_size = ($r["is_pro_member"]==1)?$site_config->pro_max_filesize:$site_config->free_max_filesize;
$limit_quot = ($r["is_pro_member"]==1)?$site_config->pro_quote:$site_config->free_quote;
$limit_size = $limit_size*1024*1024;
$limit_quot = $limit_quot * 1024*1024;
}
if (isset($_POST["submit"]))
{
if ($_POST["photoname"] == "")
{
	$inform = "Missing photoname";
}else
{
	if (is_uploaded_file($_FILES["photofile"]["tmp_name"]))
	{
		if ($_FILES["photofile"]["size"]>=$limit_size)
		{
			$inform = "File is too big! You should upload a file that has a size less than ".$limit_size;
		}else
		if ($quote_use+$_FILES["photofile"]["size"]>=$limit_quot)
		{
			$inform = "Quota is overload!";
		}else
			if (strrpos($_FILES["photofile"]["type"],"image/") === false)
			{
				$inform = "Warning: Invalid Image file was uploaded! Data was saved successful without updating image url.";
			}else
			{
				$newpath = "upload/image/".time(). substr($_FILES["photofile"]["name"],strrpos($_FILES["photofile"]["name"],"."));
				if (move_uploaded_file($_FILES["photofile"]["tmp_name"], $newpath))
				{
					$fileurl = $newpath;
					$tmbpath = "upload/image/tb_".time(). substr($_FILES["photofile"]["name"],strrpos($_FILES["photofile"]["name"],"."));
                        createthumb($newpath, $tmbpath, 135,101);

					if ($_POST["editid"]>0)
					{//($id, $filename, $fileurl, $filesize, $filetype,$filedate)
						$file->Update($_POST["editid"],$_POST["photoname"],$fileurl, $tmbpath, $_FILES["photofile"]["size"], 1, time());
					}
					else
					{
						// AddNew ($userid, $filename, $fileurl, $filesize, $filetype, $filedate)
						$file->AddNew($_SESSION["member_sid"],$_POST["photoname"],$fileurl, $tmbpath, $_FILES["photofile"]["size"], 1, time());
					}
					$inform = "Image upload successfull!!!";
				}else
				{
					$inform = "Image cannot be moved for some reason, no action taken!!!";
				}
			}
            }  
		else
		{
			if ($_POST["editid"]>0)
			{
				$file->UpdateField($_POST["editid"], "filename", $_POST["photoname"]);
			}
			else
				$inform .= "Missing Image";
		}
	}
    }    

if ($r=mysql_fetch_array($member->GetBySId($_SESSION["member_sid"])))
{
	$quote_use = $r["quot_use"];
	$remain = $limit_quot - $quote_use;
}

$template->assign_vars(array(
	"CURRENTQUTA" => number_format($quote_use),
        "FREEQUTA" => $remain,
        "LIMITQUTA" => number_format($limit_quot/1024),
        'INFORM' => $inform
	)); 


//Liet ke danh sach
$pagesize = $site_config->page_size;
$page = isset ($_GET["p"])?(int)$_GET["p"]:0;

if ($re = $file->GetAllinPage($_SESSION["member_sid"], $page*$pagesize, $pagesize,1))
{
	$col = 0;
	while ($dr = mysql_fetch_array($paginator->resource()))       //source of error
	{
		$col = $col>3?1:$col+1;

		if ($col == 1)
		{
			$template->assign_block_vars("file_row",array(

			));
		}

		$filelink = "";
		if (ereg("http://", $dr["fileurl"]))
		{
			$filelink = $dr["fileurl"];
		}
		else
		{
			$filelink = YOURDOMAIN."/".$dr["fileurl"];
		} 

		$template->assign_block_vars("file_row.file_col",array(
			"ID" => $dr["id"],
			"FILEURL" => $filelink,
			"TBFILEURL" => $dr["filethmb"],
			"FILENAME" => $dr["filename"],
			"FILESIZE" => number_format($dr["filesize"])
		));

	}


$pagelist = "";
	$n = $file->TotalFiles($_SESSION["member_sid"],1);
	for ($i=0; $i<ceil($n/$pagesize); $i++)
	{
		$j = $i+1;
		$pagelist .= ($i==$page)?"$j  ":"<a href='myphotos.php?p=".$i."'>$j</a> ";
	}

	$template->assign_vars(array(
		'PAGELIST' => $pagelist!= ""?"<span class='tdtitle'>Go to page: </span>".$pagelist:""

	));    
}
//added
$paginator = new pagination($page_num_variable, 'SELECT * FROM `file`');

$paginator->results_per_page = 5;      
$paginator->total_results = 12;            
$paginator->total_pages = 99;              
$paginator->link_prefix = '/?p=';        
$paginator->link_suffix = '';             
$paginator->page_nums_separator = ' | '; 

$page_links = $paginator->paginate();

echo $page_links;

$template->pparse("myphotos");

    	include_once ("footer.php");    
                                                                      

untested but try this

<?php

include_once ("header.php");
include_once ("m_menu.php");
include_once ("class/file.class.php");
include_once ("class/paginate.class.php"); //Include
$template->set_filenames(array("myphotos" => "myphotos.html"));
$file = new Files();
$inform = "";
if (isset($_POST["delete"]) && isset($_GET["id"]) && $_GET["id"] > 0)
{
    //Xoa file
    $file->DeleteFile($_GET["id"]);
} else
    if (isset($_POST["edit"]) && isset($_GET["id"]) && $_GET["id"] > 0)
    {
        if ($dr = mysql_fetch_array($file->GetById($_GET["id"])))
        {
            $template->assign_vars(array("ID" => $dr["id"], "FILEURL" => $dr["fileurl"],
                "FILENAME" => $dr["filename"], "FILESIZE" => $dr["filesize"], "THUMBNAIL" =>
                "<img src=\"" . $dr["filethmb"] . "\" style=\"\" border=\"0\" height=\"101\" width=\"135\">"));
        }
    }
//insert and upload photo
if ($r = mysql_fetch_array($member->GetBySId($_SESSION["member_sid"])))
{
    $quote_use = $r["quot_use"];
    $limit_size = ($r["is_pro_member"] == 1) ? $site_config->pro_max_filesize : $site_config->
        free_max_filesize;
    $limit_quot = ($r["is_pro_member"] == 1) ? $site_config->pro_quote : $site_config->
        free_quote;
    $limit_size = $limit_size * 1024 * 1024;
    $limit_quot = $limit_quot * 1024 * 1024;
}
if (isset($_POST["submit"]))
{
    if ($_POST["photoname"] == "")
    {
        $inform = "Missing photoname";
    } else
    {
        if (is_uploaded_file($_FILES["photofile"]["tmp_name"]))
        {
            if ($_FILES["photofile"]["size"] >= $limit_size)
            {
                $inform = "File is too big! You should upload a file that has a size less than " .
                    $limit_size;
            } else
                if ($quote_use + $_FILES["photofile"]["size"] >= $limit_quot)
                {
                    $inform = "Quota is overload!";
                } else
                    if (strrpos($_FILES["photofile"]["type"], "image/") === false)
                    {
                        $inform = "Warning: Invalid Image file was uploaded! Data was saved successful without updating image url.";
                    } else
                    {
                        $newpath = "upload/image/" . time() . substr($_FILES["photofile"]["name"],
                            strrpos($_FILES["photofile"]["name"], "."));
                        if (move_uploaded_file($_FILES["photofile"]["tmp_name"], $newpath))
                        {
                            $fileurl = $newpath;
                            $tmbpath = "upload/image/tb_" . time() . substr($_FILES["photofile"]["name"],
                                strrpos($_FILES["photofile"]["name"], "."));
                            createthumb($newpath, $tmbpath, 135, 101);
                            if ($_POST["editid"] > 0)
                            { //($id, $filename, $fileurl, $filesize, $filetype,$filedate)
                                $file->Update($_POST["editid"], $_POST["photoname"], $fileurl, $tmbpath, $_FILES["photofile"]["size"],
                                    1, time());
                            } else
                            {
                                // AddNew ($userid, $filename, $fileurl, $filesize, $filetype, $filedate)
                                $file->AddNew($_SESSION["member_sid"], $_POST["photoname"], $fileurl, $tmbpath,
                                    $_FILES["photofile"]["size"], 1, time());
                            }
                            $inform = "Image upload successfull!!!";
                        } else
                        {
                            $inform = "Image cannot be moved for some reason, no action taken!!!";
                        }
                    }
        } else
        {
            if ($_POST["editid"] > 0)
            {
                $file->UpdateField($_POST["editid"], "filename", $_POST["photoname"]);
            } else
                $inform .= "Missing Image";
        }
    }
}
if ($r = mysql_fetch_array($member->GetBySId($_SESSION["member_sid"])))
{
    $quote_use = $r["quot_use"];
    $remain = $limit_quot - $quote_use;
}
$template->assign_vars(array("CURRENTQUTA" => number_format($quote_use),
    "FREEQUTA" => $remain, "LIMITQUTA" => number_format($limit_quot / 1024),
    'INFORM' => $inform));
//Liet ke danh sach
$pagesize = $site_config->page_size;
$page = isset($_GET["p"]) ? (int)$_GET["p"] : 0;
$paginator = new pagination($page, 'SELECT * FROM `file`');
$paginator->results_per_page = 5;
$paginator->link_prefix = '?p=';
$paginator->link_suffix = '';
$paginator->page_nums_separator = ' | ';
$page_links = $paginator->paginate();
if ($re = $file->GetAllinPage($_SESSION["member_sid"], $page * $pagesize, $pagesize,
    1))
{
    $col = 0;
    while ($dr = mysql_fetch_array($paginator->resource())) //source of error

    {
        $col = $col > 3 ? 1 : $col + 1;
        if ($col == 1)
        {
            $template->assign_block_vars("file_row", array());
        }
        $filelink = "";
        if (ereg("http://", $dr["fileurl"]))
        {
            $filelink = $dr["fileurl"];
        } else
        {
            $filelink = YOURDOMAIN . "/" . $dr["fileurl"];
        }
        $template->assign_block_vars("file_row.file_col", array("ID" => $dr["id"],
            "FILEURL" => $filelink, "TBFILEURL" => $dr["filethmb"], "FILENAME" => $dr["filename"],
            "FILESIZE" => number_format($dr["filesize"])));
    }
    $pagelist = "";
    $n = $file->TotalFiles($_SESSION["member_sid"], 1);
    for ($i = 0; $i < ceil($n / $pagesize); $i++)
    {
        $j = $i + 1;
        $pagelist .= ($i == $page) ? "$j  " : "<a href='myphotos.php?p=" . $i . "'>$j</a> ";
    }
    $template->assign_vars(array('PAGELIST' => $pagelist != "" ?
        "<span class='tdtitle'>Go to page: </span>" . $pagelist : ""));
}
//added
echo $page_links;
$template->pparse("myphotos");
include_once ("footer.php");

yes that shift in position did the trick thank you :) It did take the images from the file table and replaced the existing ones though....so I would assume that its not calling the user id? Apologies If I have no idea what im talking about. It seems to have replaced my user id images with all images in the table.

Jay your script is great for search functions but I need to call the users images and not all images from the sql table. Is there a way to do this and can anyone be of assistance to help me integrate this? Im really backed into a corner because im PHP illiterate "see class file from first post" Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.