next previous sql
Moderator: General Moderators
-
FireElement
- Forum Commoner
- Posts: 86
- Joined: Wed Oct 17, 2007 6:03 pm
next previous sql
Hello coders!
I am currently doing a news feed... and I would like to be able to add a script that cut up a chuck of sql records up in to several pages.
For exaple lets say i had 39 records and I wanted 3 records on each page. Instead of a massive long list of records I want next button and previous button with a list of pages from 1 to 5... then if i click 4, then it shows pages 2 to 6 with extra link of top and bottom... for example:
----------------------------------------------------------------
record 10.... date set out in div
record 11.... date set out in div
record 12.... date set out in div
-----------------------------------------------------------------
:: Top :: Previous :: 2 3 4 5 6 :: Next :: Bottom
-----------------------------------------------------------------
I know how to do this with hard coding and messing around I have done it in past but, every time I go back to the code I can't help but think surely there is function that does this better then every time I build website I spend a day trying to adapt my code to fit the new site and messing around... surely there is function out there that does this for you... If there isnt I guess I will be spending a week writing one that I can use on any site... but if there is any can you please tell me where so I don't waste my time.
Thanks in advance!
I am currently doing a news feed... and I would like to be able to add a script that cut up a chuck of sql records up in to several pages.
For exaple lets say i had 39 records and I wanted 3 records on each page. Instead of a massive long list of records I want next button and previous button with a list of pages from 1 to 5... then if i click 4, then it shows pages 2 to 6 with extra link of top and bottom... for example:
----------------------------------------------------------------
record 10.... date set out in div
record 11.... date set out in div
record 12.... date set out in div
-----------------------------------------------------------------
:: Top :: Previous :: 2 3 4 5 6 :: Next :: Bottom
-----------------------------------------------------------------
I know how to do this with hard coding and messing around I have done it in past but, every time I go back to the code I can't help but think surely there is function that does this better then every time I build website I spend a day trying to adapt my code to fit the new site and messing around... surely there is function out there that does this for you... If there isnt I guess I will be spending a week writing one that I can use on any site... but if there is any can you please tell me where so I don't waste my time.
Thanks in advance!
Re: next previous sql
Have a look at this Class, this Class gives you the similar pageing string that you have specified.
You can implement it with some of the changes to fulfill your requirement.
Cheers
You can implement it with some of the changes to fulfill your requirement.
Code: Select all
class SmartyPageing{
public $page,$lastPage,$numPages;
private $invalidPage = false;
private $getString = array();
/**
* @Constructor
*/
public function __construct(&$smarty,$page=1,$display=20,$offSet=10,$display=true)
{
global $page;
$this->lastPage = false;
$this->display = $display;
$this->page = $page;
$this->smarty = &$smarty;
$this->offSet = $offSet;
}
/**
* @Returns offset value
*/
public function getOffset()
{
return $this->offSet;
}
/**
* @Set offset value
*/
public function setOffset($offSet = NULL)
{
if(!preg_match('!^\d+$!', $offSet)){
$this->Error = new Error('12','Offset value must be an integer');
return false;
}
$this->offSet = $offSet;
}
/**
* @Returns total value
*/
public function getTotal()
{
return $this->numRecords;
}
/**
* @Set total value
*/
public function setTotal($total)
{
if(!preg_match('!^\d+$!', $total)){
$this->Error = new Error('12','Total value must be an integer');
return false;
}
$this->numRecords = $total;
}
/**
* @Returns the result set array
*/
public function getResultSet()
{
return $this->result;
}
/**
* @Set result set
*/
public function setResultSet($result = NULL)
{
if(!is_array($result)){
$this->Error = new Error('13','Invalid Result Set');
}
$this->result = $result;
}
/**
* @Generate pageing with the defined parameters
*/
public function generatePageing()
{
$numPages = ceil($this->numRecords/$this->offSet);
if($this->lastPage){
$this->page = $numPages;
}
// if page is invalid then show page 1 results
if($this->page > $numPages){
$this->invalidPage = true;
$this->page = 1;
}
if($this->numRecords == 0){
$this->empty = 1;
$this->page = 1;
$numPages = 1;
}
$this->smarty->assign('list_array', $this->result);
// CREATE NAVBAR
$thisPage = $_SERVER['PHP_SELF'];
#echo 'THIS PAGE'.$thisPage;
//Recreate Get String
foreach($_GET as $key => $value){
if($key != "page"){
$getString[] = $key."=".$value;
}
}
if(sizeof($getString) !== 0){
$QUERY_STRING = "&".implode("&",$getString);
}else if($this->page > $numPages){
$QUERY_STRING = NULL;
}
// produce page... string
if($this->page > 4){
$doEllipse = 1;
$pageString .= " <a href=\"".$thisPage."?page=1".$QUERY_STRING."\">First</a> ";
}
if($this->page > 1){
$pagemo = $this->page - 1;
$pageString .= " <a href=\"".$thisPage."?page=".$pagemo.$QUERY_STRING."\">Previous</a> ";
}
if($doEllipse == 1){
$pageString .= " ... ";
}
if($this->page > 3){
for ($i = $this->page - 3; $i < $this->page; $i++) {
$pageString .= " <a href=\"".$thisPage."?page=".$i.$QUERY_STRING."\">".$i."</a>";
}
}else{
for ($i = 1; $i < $this->page; $i++) {
$pageString .= " <a href=\"".$thisPage."?page=".$i.$QUERY_STRING."\">".$i."</a>";
}
}
$pageString .= " <span style=\"color:red\">".$this->page."</span>";
if($numPages > $this->page + 3){
for ($i = $this->page + 1; $i <= $this->page + 3; $i++) {
$pageString .= " <a href=\"".$thisPage."?page=".$i.$QUERY_STRING."\">".$i."</a>";
}
}elseif ($this->page != $numPages){
for ($i = $this->page + 1; $i <= $numPages; $i++) {
$pageString .= " <a href=\"".$thisPage."?page=".$i.$QUERY_STRING."\">".$i."</a>";
}
}
$doEllipse = 0;
if($this->page < $numPages){
$pagepo = $this->page + 1;
$pageString2 .= " <a href=\"".$thisPage."?page=".$pagepo.$QUERY_STRING."\">Next</a>";
}
if($this->page < $numPages - 3){
$doEllipse = 1;
$pageString2 .= " <a href=\"".$thisPage."?page=".$numPages.$QUERY_STRING."\">Last</a>";
}
if($doEllipse == 1){
$pageString .= " ... ";
}
$pageString .= $pageString2;
if($this->numRecords < ($this->page*$this->offSet))
$toRecords = $this->numRecords;
else
$toRecords = ((int)$this->page*$this->offSet);
$recordString=" Displaying ".((int)$this->page*$this->offSet-$this->offSet+1)." - ".$toRecords." of ".$this->numRecords;
$pageString.=$recordString;
if($this->display){
if($numPages != 1){
$pageString = "Page " . $this->page . " of " . $numPages . "<br />" . $pageString;
}else{
$pageString = "Page " . $this->page . " of " . $numPages;
}
}
$this->smarty->assign('page_string', $pageString);
}
}
-
FireElement
- Forum Commoner
- Posts: 86
- Joined: Wed Oct 17, 2007 6:03 pm
Re: next previous sql
This code looks a little above my level, classes and functions... I only just started using functions yet alone classes with is it injections....
Its like a whole new level to me... can you explain this a little and how I would use it... How would I call the functions... and when...
Glad you showed me this helped me realise how much I still have to learn!
Thanks!
Its like a whole new level to me... can you explain this a little and how I would use it... How would I call the functions... and when...
Glad you showed me this helped me realise how much I still have to learn!
Thanks!
-
FireElement
- Forum Commoner
- Posts: 86
- Joined: Wed Oct 17, 2007 6:03 pm
Re: next previous sql
I mean I know how to call a function... Include this file then call them... I just mean when and where....
say i have my while statment calling my sql...
while .... {
records pulled here
}
this has just given me a wake up call on structuring my code... am still at an intermidate level. Any explaination of the above code would be very helpful for me thanks!!!
say i have my while statment calling my sql...
while .... {
records pulled here
}
this has just given me a wake up call on structuring my code... am still at an intermidate level. Any explaination of the above code would be very helpful for me thanks!!!
-
FireElement
- Forum Commoner
- Posts: 86
- Joined: Wed Oct 17, 2007 6:03 pm
Re: next previous sql
GREAT SITE: http://www.killerphp.com/tutorials/object-oriented-php/
If you read this code and want to know more about OOP... (Object Orintated Programming)
Then thats the site to go!!
I have done a fair bit of OOP in the past but I never really used classes I just stuck loads of functions in a php file and called it with an include, but I think learning how to structure and better design of code is assential these days! I mean if you ever want to work in a team then you kind of need to know it if your after working alone it would be good just for professionalism in work... I mean then if a big company decided to hire you pay you to do a bundle of work for them for whatever reason your work would be understandable by the next person who came along or if you get hit by a car, then at least people wont have to spend the next course of there life unraveling the mistory of your work!!!
If you read this code and want to know more about OOP... (Object Orintated Programming)
Then thats the site to go!!
I have done a fair bit of OOP in the past but I never really used classes I just stuck loads of functions in a php file and called it with an include, but I think learning how to structure and better design of code is assential these days! I mean if you ever want to work in a team then you kind of need to know it if your after working alone it would be good just for professionalism in work... I mean then if a big company decided to hire you pay you to do a bundle of work for them for whatever reason your work would be understandable by the next person who came along or if you get hit by a car, then at least people wont have to spend the next course of there life unraveling the mistory of your work!!!
-
FireElement
- Forum Commoner
- Posts: 86
- Joined: Wed Oct 17, 2007 6:03 pm
Re: next previous sql
He explains it better: http://www.killerphp.com/videos/oop_why ... n_oop.html
Re: next previous sql
Try this may this will help you. Simple pagination code
It will be like this
<< | Previous | Next | >>
function pagingPN($sql, $page, $limit, $getvars, $class)
{
if ($page == "")
$page = 1;
if ($limit == 0)
$limit = $this->limit;
$tsql = $sql;
$result = mysql_query($tsql) or die("Error: ".mysql_errno().":- ".mysql_error());
$total = mysql_num_rows($result);
$totnumpages = ceil($total/$limit);
if ($offset < 0)
$offset = 0;
else
$offset = ($page - 1) * $limit;
$sql = $sql. " limit $offset, $limit";
$res = $this->select_row($sql);
$serial_no = ($page - 1) * $limit;
if ($total > 0)
{
$link = "";
if ($page > 1)
{
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=1$getvars class='".$class."' title='Jump to First Page'><<</a> | ";
$prev = $page - 1;
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prev."$getvars class='".$class."' title='Goto Previous Page'>Previous</a><span class='".$class."'> | </span>";
}
else
{
$link .= "<span class='".$class."' title='Jump to First Page'><<</span> | <span class='".$class."' title='Goto Previous Page'>Previous | </span>";
}
if ($page < $totnumpages)
{
$next = $page + 1;
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$next."$getvars class='".$class."' title='Goto Next Page'>Next</a> | ";
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$totnumpages."$getvars class='".$class."' title='Jump to Last Page'>>></a>";
}
else
{
$link .= "<span class='".$class."' title='Goto Next Page'>Next</span> | <span class='".$class."' title='Jump to Last Page'>>></span>";
}
}
$retarr["sql"] = $sql;
$retarr["records"] = $res;
$retarr["serial_no"] = $_no;
$retarr["link"] = $link;
return $retarr;
}
It will be like this
<< | Previous | Next | >>
function pagingPN($sql, $page, $limit, $getvars, $class)
{
if ($page == "")
$page = 1;
if ($limit == 0)
$limit = $this->limit;
$tsql = $sql;
$result = mysql_query($tsql) or die("Error: ".mysql_errno().":- ".mysql_error());
$total = mysql_num_rows($result);
$totnumpages = ceil($total/$limit);
if ($offset < 0)
$offset = 0;
else
$offset = ($page - 1) * $limit;
$sql = $sql. " limit $offset, $limit";
$res = $this->select_row($sql);
$serial_no = ($page - 1) * $limit;
if ($total > 0)
{
$link = "";
if ($page > 1)
{
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=1$getvars class='".$class."' title='Jump to First Page'><<</a> | ";
$prev = $page - 1;
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prev."$getvars class='".$class."' title='Goto Previous Page'>Previous</a><span class='".$class."'> | </span>";
}
else
{
$link .= "<span class='".$class."' title='Jump to First Page'><<</span> | <span class='".$class."' title='Goto Previous Page'>Previous | </span>";
}
if ($page < $totnumpages)
{
$next = $page + 1;
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$next."$getvars class='".$class."' title='Goto Next Page'>Next</a> | ";
$link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$totnumpages."$getvars class='".$class."' title='Jump to Last Page'>>></a>";
}
else
{
$link .= "<span class='".$class."' title='Goto Next Page'>Next</span> | <span class='".$class."' title='Jump to Last Page'>>></span>";
}
}
$retarr["sql"] = $sql;
$retarr["records"] = $res;
$retarr["serial_no"] = $_no;
$retarr["link"] = $link;
return $retarr;
}
Re: next previous sql
Sorry for the delay,
This code gives you an idea, you can't implement it directly. I think you have to change it little to get your result.
And if you go through this code, i think it's not so hard to understand it.
Cheers
This code gives you an idea, you can't implement it directly. I think you have to change it little to get your result.
And if you go through this code, i think it's not so hard to understand it.
Cheers
-
FireElement
- Forum Commoner
- Posts: 86
- Joined: Wed Oct 17, 2007 6:03 pm
Re: next previous sql
It's hard for me to understand it, but like everything at first....
I have always struggled with highly structured code though... I will keep at it thought...
Am great to a intermidiate level... but once it gets more OOP and function calls, constructors, pointers... so on...
I start getting a little lost... but I will stick at it when I understand that code properly and can implement changes then I will know am on the right track!!!
Thanks for your help!
I have always struggled with highly structured code though... I will keep at it thought...
Am great to a intermidiate level... but once it gets more OOP and function calls, constructors, pointers... so on...
I start getting a little lost... but I will stick at it when I understand that code properly and can implement changes then I will know am on the right track!!!
Thanks for your help!