PHP pagination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

PHP pagination

Post by akimm »

I have a text document that I use simple fopen,fread,frwrite functions for a journal I keep myself. I was wondering, do you think its practical to have the page after a certain amount of characters paginate, or is there anything I might not know to help cut off at a certain point? I just mean after say, 5 entries it adds all new entries to page 2.

I was thinking maybe* to use a counter, everytime I add an entry have the form iterate, until it reaches X number and then paginates. Any ideas you can bounce off me is more than appreciated, I will start some code once I figure out which way is the best way to tackle this prob.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

sortable javascript

Post by yacahuma »

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Thanks!!! I'm going there now.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Would it work better if I made each journal seperate then used count function or another function to read how many journal entries there were, and print some set number per page?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

here is what I got so far..

Code: Select all

<?php
## this program takes form info and writes it to my directory journal/
$write_string = "<b>" . "<font color=#006699>" . "Date posted:  " . "</font>" . "</b>" . $_POST['date']; 
$write_string .= "<font family=arial>"; $write_string .= "<BR>" . "<BR>";
$write_string .= "<b>" . "<font color=#006699>" . "Current mood:  " . "</font>"   . "</b>" . $_POST['mood'];
$write_string .= "<font family=arial>";
$write_string .=  "<img src=" . $_POST['smile'] . ">" . "<br>" . "<br>" . "<br>";
$write_string .= stripslashes($_POST['entry']) . "<font family=arial>" . "<BR>" . "<BR>";
$write_string .= "<HR NOSHADE SIZE=4 COLOR=BLUE>";

$fp = fopen( 'journal/' . $_POST['mood'] . rand(1, 10) . '.txt', 'w');
if($fp) {
fwrite($fp, $write_string);
fclose($fp);
}

?>

<h1>Sucessfully posted</h1> 

<br><br>
<?php
echo  "<b>" . "<font color=#006699>" . "Date posted:  " . "</font>" . "</b>" . $_POST['date'] . "<br>";
echo  "<b>" . "<font color=#006699>" . "Current mood:  " . "</font>" . "Current Mood: " . "</b>" . $_POST['mood'] . "<br>";
echo "<img src=" . $_POST['smile'] . ">". "<br>" . "<br>" . "<br>";
echo stripslashes($_POST['entry']) . "<br>";
?>

<br><br>
<a href="http://www.akimm.com/journal06/journal.php">Go To Journal Sean!</a>

I need it to read from that directory using glob( "*.txt") perhaps, I want it to read a set number of files then print them to the first page, redirect all the others to the next page and continue the process so as to keep each page limited in its journal entries

here is what I have with the glob idea used, any way to limit or direct only a certain amount of entries with glob????

Code: Select all

<?php
error_reporting (E_ALL);
$path = "journal/";
foreach(glob($path . "*.txt") as $file) {
$fh = fopen($file, 'r');
$theData = fread($fh, filesize($file));
echo $theData;
fclose($fh);
}
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Any ideas guys?
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Why not store your journal entries in XML format? I'm sure it would be easier to modify, manage, and sort. I know the Java-DOM functions has the ability to count how many XML entities there are, and loop through them. I'm sure PHP XML parsers have just as much, if not more functionality...
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

how would I go about doing that. I've never used XML, but wouldn't mind trying it. Am I going to need to write the DOM?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

akimm wrote:how would I go about doing that. I've never used XML, but wouldn't mind trying it. Am I going to need to write the DOM?
You could create an extremely simplistic one.

Code: Select all

<journal>
    <entry>
        <title>
            Journal Entry #1
        </title>
        <content>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse....
        </content>
    </entry>
    <entry>
        <title>
            Journal Entry #2
        </title>
        <content>
            ........
        </content>
    </entry>
</journal>
I'm sure that you can see how easy it is to manipulate.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

That seems simple enough

Post by akimm »

Will google tell me how to actually take all that xml data and loop it like I want to. Because as of now I have all my entries being written directly to the directory. Can I possibly have the XML formatted through the fwrite process? Like for instance. Here is my code to post my entries now.

Code: Select all

<?php
$write_string = "<b>" . "<font color=#006699>" . "Date posted:  " . "</font>" . "</b>" . $_POST['date']; 
$write_string .= "<font family=arial>"; $write_string .= "<BR>" . "<BR>";
$write_string .= "<b>" . "<font color=#006699>" . "Current mood:  " . "</font>"   . "</b>" . $_POST['mood'];
$write_string .= "<font family=arial>";
$write_string .=  "<img src=" . $_POST['smile'] . ">" . "<br>" . "<br>" . "<br>";
$write_string .= stripslashes($_POST['entry']) . "<font family=arial>" . "<BR>" . "<BR>";
$write_string .= "<HR NOSHADE SIZE=4 COLOR=BLUE>";

$fp = fopen( 'journal/' . $_POST['mood'] . rand(1, 1000) . '.txt', 'w');
if($fp) {
fwrite($fp, $write_string);
fclose($fp);
}

?>

<h1>Sucessfully posted</h1> 

<br><br>
<?php
echo  "<b>" . "<font color=#006699>" . "Date posted:  " . "</font>" . "</b>" . $_POST['date'] . "<br>";
echo  "<b>" . "<font color=#006699>" . "Current mood:  " . "</font>" . "Current Mood: " . "</b>" . $_POST['mood'] . "<br>";
echo "<img src=" . $_POST['smile'] . ">". "<br>" . "<br>" . "<br>";
echo stripslashes($_POST['entry']) . "<br>";
?>

<br><br>
<a href="http://www.akimm.com/journal06/journal.php">Go To Journal Sean!</a>
Is it possible to $_POST it in XML format then use XML to print each entry, then do I need an external DOM or do I invoke a parser, what do I do to tell the page to act like XML?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

A better question might be.

Code: Select all

<journal>
    <entry>
        <title>
            Journal Entry #1
        </title>
        <content>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse....
        </content>
    </entry>
    <entry>
        <title>
            Journal Entry #2
        </title>
        <content>
            ........
        </content>
    </entry>
</journal>
Can I take this code, and plug in the php right into it? Then use XML to sort the amount of entries on each page?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

hey, I found this code on w3s

Obviously I would adjust it a bit, but do you think this could be suitable for my purpose?

Code: Select all

<html>
<head>
<script type="text/javascript">
function movenext()
{
x=xmldso.recordset
if (x.absoluteposition < x.recordcount)
	{
	x.movenext()
	}
}
function moveprevious()
{
x=xmldso.recordset
if (x.absoluteposition > 1)
	{
	x.moveprevious()
	}
}
</script>
</head>

<body>
<xml src="cd_catalog.xml" id="xmldso" async="false"></xml>

<p>
Title:
<span datasrc="#xmldso" datafld="TITLE"></span>
<br />Artist:
<span datasrc="#xmldso" datafld="ARTIST"></span>
<br />Year:
<span datasrc="#xmldso" datafld="YEAR"></span>
</p>
<p>
<input type="button" value="Previous CD"
onclick="moveprevious()" />
<input type="button" value="Next CD"
onclick="movenext()" />
</p>
</body>
</html>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

akimm wrote:hey, I found this code on w3s

Obviously I would adjust it a bit, but do you think this could be suitable for my purpose?
Could you please stop asking questions and just write the code, already?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Sorry. I didn't realize I was being a bother. I will try and write it now.
Post Reply