Sanjib Sinha Posted April 10, 2013 Share Posted April 10, 2013 I tried a simple pagination. As a web hobbyist, i tried to visualize the whole aspect first, and then tried to implement them. I need experts opinion. Is my approach okay? How i can i modify it (i am sure it is too simple an approach, and can be modified)? 1) i keep an MyArray class at my lib folder and name it commonfunction.php. it goes like this: class myArray implements ArrayAccess { protected $array = array(); function offsetSet($offset, $value) { if(!is_numeric($offset)){ throw new Exception("Invalid key {$offset}") ; } $this->array[$offset] = $value; } function offsetGet($offset) { return $this->array[$offset]; } function offsetUnset($offset) { unset ($this->array[$offset]); } function offsetExists($offset) { return array_key_exists($this->array, $offset); } } 2) Next, i have a published comments table, that i want to paginate. To start with, i create a pagination1.php page. The code is like this: <?php include_once 'lib/commonfunction.php'; if(!isset ($_REQUEST['next'])){ echo ""; } mysql_connect('localhost', 'root', ''); mysql_select_db('appababa'); ////////////////////////////////////////////////////////////////////////////////// //trying pagination/// $myrow = new myArray(); $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'"); $myresults = array(); while ($myrow = mysql_fetch_array($myquery)) { $myresults[]=$myrow[0]; } foreach ($myresults as $key=>$value) { $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`id`='".$value."'"); while ($fullrow = mysql_fetch_array($myquery)) { //echo "Description : ".$fullrow['description']."<br>"; if ($fullrow['id']==1){ echo "Description : ".$fullrow['description']."<br>"; $next=next($fullrow); $next++; echo "<a href='http://localhost/ClassesAndObjects/pagination2.php?next={$next}'>NEXT</a>"; } } } ?> 3) Next i have a pagination2.php. The code is like this: <?php include_once 'lib/commonfunction.php'; if(!isset ($_REQUEST['next'])){ echo ""; } mysql_connect('localhost', 'root', ''); mysql_select_db('appababa'); ////////////////////////////////////////////////////////////////////////////////// //trying pagination/// $myrow = new myArray(); $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'"); $myresults = array(); while ($myrow = mysql_fetch_array($myquery)) { $myresults[]=$myrow[0]; } $present=0; if(isset ($_REQUEST['next'])) $present = $_REQUEST['next']; if ($present==0){ header('Location:http://localhost/ClassesAndObjects/pagination1.php'); } $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`id`='".$present."'"); while ($fullrow = mysql_fetch_array($myquery)) { //echo "Description : ".$fullrow['description']."<br>"; if ($fullrow['id']==$present){ echo "Description : ".$fullrow['description']."<br>"; $next=next($fullrow); $next++; echo "<a href='http://localhost/ClassesAndObjects/pagination2.php?next={$next}'>NEXT</a>"; echo " ------ "; $prev=prev($fullrow); $prev--; echo "<a href='http://localhost/ClassesAndObjects/pagination3.php?prev={$prev}'>PREV</a>"; } } $row = new myArray(); $query1 = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'"); $results = array(); while ($row = mysql_fetch_array($query1)) { $results[]=$row[0]; //echo $row['description']." = "."<a href='http://localhost/ClassesAndObjects/update.php?id={$row['id']}'>UNPUBLISH</a>"."<br>"; } //echo "Total number of published comments : ".count($results); $count=count($results); $count++; if ($present==$count){ header('Location:http://localhost/ClassesAndObjects/pagination1.php'); } ?> 3) Next i have pagination3.php. The code is like this: <?php include_once 'lib/commonfunction.php'; if(!isset ($_REQUEST['prev'])){ echo ""; } mysql_connect('localhost', 'root', ''); mysql_select_db('appababa'); ////////////////////////////////////////////////////////////////////////////////// //trying pagination/// $myrow = new myArray(); $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`is_published`='Y'"); $myresults = array(); while ($myrow = mysql_fetch_array($myquery)) { $myresults[]=$myrow[0]; } $present=0; if(isset ($_REQUEST['prev'])) $present = $_REQUEST['prev']; if ($present==0){ header('Location:http://localhost/ClassesAndObjects/pagination1.php'); } $myquery = mysql_query("SELECT * FROM comments WHERE `comments`.`id`='".$present."'"); while ($fullrow = mysql_fetch_array($myquery)) { //echo "Description : ".$fullrow['description']."<br>"; if ($fullrow['id']==$present){ echo "Description : ".$fullrow['description']."<br>"; $next=next($fullrow); $next++; echo "<a href='http://localhost/ClassesAndObjects/pagination2.php?next={$next}'>NEXT</a>"; echo " ------ "; $prev=prev($fullrow); $prev--; echo "<a href='http://localhost/ClassesAndObjects/pagination3.php?prev={$prev}'>PREV</a>"; } } ?> Finally, it works faultlessly. There is apparently no problem. It is based on mainly three array functions, count(), next(), and prev(). I tried to avoid mathematical calculation as much as possible. I need your opinion. Many thanks. Quote Link to comment Share on other sites More sharing options...
Sanjib Sinha Posted April 11, 2013 Author Share Posted April 11, 2013 Any advice please? usually pagination being done by calculating LIMIT in sql code. i tried to avoid that mathematical calculation depending mainly on three array functions. can this approach be encapsulated in a class? or i should take any other approach to solve this problem? Quote Link to comment Share on other sites More sharing options...
trq Posted April 11, 2013 Share Posted April 11, 2013 usually pagination being done by calculating LIMIT in sql code. i tried to avoid that mathematical calculation depending mainly on three array functions. Why? Your are now querying the database for more data than you use. Quote Link to comment Share on other sites More sharing options...
Sanjib Sinha Posted April 11, 2013 Author Share Posted April 11, 2013 many thanks trk. it was probably my bad English sentence construction, that changed the actual meaning for what i actually wanted to mean. i wanted to say, when i query the database, there is always a LIMIT. when i paginate, that LIMIT either reduces or decreases and to capture that fine balance you need to use some mathematical functions. i just downloaded an available pagination class and found, that array functions are not used at all! may be that is the correct approach. i feel slightly confused in that regard. Quote Link to comment Share on other sites More sharing options...
trq Posted April 11, 2013 Share Posted April 11, 2013 I didn't really look at your code earlier, but there is definitely no limit clauses in your queries. You might also want to take a look at what mysql_fetch_array actually returns, I think you might be confused. It only ever returns a single record. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.