 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
02-02-2008, 09:37 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 107
Thanks: 3
|
Pagination without using database ??
I have content-manager which doesnt use database. The contents are being written in a file, and read from the file. Is there anyway to paginate the content ?
the php file:
PHP Code:
<?
require('config.php');
$filename = "article_summary.html";
#- open article summaries
if(file_exists($filename)){
$fh = fopen($filename, "r");
$old_news = fread($fh, filesize($filename));
fclose($fh);
}
#- get first five article
$articles = explode("<!--ARTICLE-->", $old_news);
$i=0;
foreach ( $articles as $article ){
if(count($articles)>$i){
if($max_latest >= $i++){
print $article;
}
}
}
?>
You can see, it uses explode to <!--ARTICLE-->
In the file the <!--ARTICLE--> is between contents.
Example:
In file,
content123 blabla
<!--ARTICLE-->
content456hahaha
<!--ARTICLE-->
content789hehehe
|
|
|
02-03-2008, 12:32 AM
|
#2 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
|
Of course you can paginate your article content. The whole article is just, put simply, a list of blocks of content... just like you can get a list of blocks of content from a database. The pagination system shouldn't really care where the blocks come from at all.
|
|
|
|
02-03-2008, 06:06 AM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 107
Thanks: 3
|
But how can I do it, can you please explain ? I have no idea how to do that....
|
|
|
02-03-2008, 07:52 AM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Explode the text by the separators, and then if there's a GET['page'] (or something like that) set, then output the appropriate element in the array. Something like so:
PHP Code:
$parts = explode( '<!--ARTICLE-->', $text );
$page = isset( $_GET['page'] ) ? intval( $_GET['page'] ) : 0;
echo $parts[$page];
Of course, you might want to enforce some more checks there (like check if the requested page is greater than 0 or 1, depending on how you want the pages to appear, check if the requested page is not greater than the dimension of the array - 1, and so on).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
02-03-2008, 09:30 AM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Also you can count the $parts array, and see how many articles you get.
Then you decide how many articles per page you want, and you use some calculations, and voilą! Pagination system :)
__________________
Last edited by Tanax : 02-04-2008 at 09:05 AM.
|
|
|
|
02-03-2008, 10:18 AM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 107
Thanks: 3
|
Thanks.
What I want to do is,
count articles, if articles are more than 5, create page number and then echo the other articles in the other page.
But I cant do it in php language :S
|
|
|
02-03-2008, 10:54 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
I've written a pagination class here:
pagination.php
PHP Code:
<?php
/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/
class pagination {
// The total value.
private $totalPages;
private $totalResults;
private $totalPerPage;
// The current value.
private $currentPage;
private $currentSpan;
private $firstResult;
public function setMax($max) {
if(is_numeric($max)) {
$this->totalPerPage = $max;
}
}
public function setPage($page) {
if(is_numeric($page)) {
$this->currentPage = mysql_real_escape_string($page);
$this->firstResult = (($this->currentPage * $this->totalPerPage) - $this->totalPerPage);
return $this->firstResult;
}
}
public function getPages($results) {
$this->totalResults = count($results);
$totalPages = $this->totalResults / $this->totalPerPage;
$this->totalPages = ceil($totalPages);
$x = 1;
$array = array();
while($x <= $this->totalPages) {
$array[] = $x;
$x++;
}
return $array;
}
public function checkLink($pagenr) {
if($pagenr <= $this->totalPages && $pagenr >= 1) {
return true;
}
return false;
}
public function getCurrentPage() {
$array = array();
$array[] = $this->currentPage;
$array[] = $this->totalPages;
return $array;
}
}
?>
example.php
PHP Code:
<?php
/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/
include('pagination.php');
$p = $_GET['p'];
(isset($p)) ? $p : 1;
$max = 10;
// Create the object and set some basic values.
$pagination = new pagination();
$pagination->setMax($max);
// Get the total results, and then calculate how many pages that becomes based on how many results per page.
$totSql = "SELECT * FROM `table`";
$totQuery = mysql_query($totSql) or die(mysql_error());
$totResults = mysql_fetch_array($totQuery);
$totPages = $pagination->getPages($totResults);
// Set the current page, and get the first result on it.
$first = $pagination->setPage($p);
// Get the results of the current page.
$exSql = "SELECT * FROM `table` LIMIT $first, $max";
$exQuery = mysql_query($exSql) or die(mysql_error());
$exResults = mysql_fetch_array($exQuery);
// Echo out the current page, and the total amount of pages.
$page = $pagination->getCurrentPage();
echo 'Page: '.$page[0].' of '.$page[1];
// Get previous page link, and check if it's valid.
$prevLink = $p - 1;
if($pagination->checkLink($prevLink)) {
echo 'Previous Page';
}
// Print all the pages
foreach($totPages as $pageNumber) {
if($pageNumber == $p) {
echo '<strong>'.$pageNumber.'</strong>';
}
else {
echo $pageNumber;
}
}
// Get the next page link, and check if it's valid.
$nextLink = $p + 1;
if($pagination->checkLink($nextLink)) {
echo 'Next Page';
}
// Echo the results...
?>
Ofcourse you have to edit the "Next page" for example, to contain a link to the next page..
The good thing with this pagination class is that it's only the core, and it only generates RESULTS, and NO html tagging.
So you can modify the result page easially by just changing the html tagging in that file. Instead of having to go through the class file and change the html tagging in there..
I haven't written a tutorial or a "how-to-use" for this, but I'm sure you'll understand it, otherwise you can just ask if you hit a problem
Hope it helped.
__________________
Last edited by Tanax : 02-04-2008 at 09:05 AM.
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
02-03-2008, 11:33 AM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 107
Thanks: 3
|
Thanks a lot Tanax. I am gonna test it... But is this using mysql ? If it is, where should I write database settings ? I am not very good in php, just in middle ;) And can you explain how can I paginate without using mysql ? (Get data from file, not from mysql table ?)
|
|
|
02-03-2008, 01:22 PM
|
#9 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
No it's not using mysql.
My example is using mysql, but the core can work with files too.
You would have to count your results, and set how many results per page you want to show.
PHP Code:
$totPages = $pagination->getPages($totResults);
That's how many pages in total there are based on how many results, and how many results per page you want to show.
$totResults would be count($articles) in your case..
I'm not sure how you would solve the "exQuery" in your case(without mysql), but you would have to come up with some other way to generate the results on the CURRENT page. As you see, I'm not using the class in any of the $ex~ variables..
But other than that you can use my example.php exactly like it is... xD
__________________
Last edited by Tanax : 02-04-2008 at 09:05 AM.
|
|
|
|
02-03-2008, 07:40 PM
|
#10 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 107
Thanks: 3
|
Thanks. I am gonna learn how to
echo pages if the contents are 5+ and show the others in the next page.
|
|
|
02-03-2008, 11:42 PM
|
#11 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
Very interesting. Btw I just learned a new term, pagination. It even sounds cool!
|
|
|
|
02-04-2008, 09:04 AM
|
#12 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by ETbyrne
Very interesting. Btw I just learned a new term, pagination. It even sounds cool!
|
hahahahh  It actually does sound cool 
__________________
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|