Using my php pagination script in my html file

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

Post Reply
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Using my php pagination script in my html file

Post by soianyc »

Newbie here, I found a script to do pagination and got it to work for me. I have an html tempelate that i want to use the pagination script with but im not sure how the two integrate with eachother. Any help is greatly appreciated.

Thanks

-soianyc
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically, you place the pagination code in the content area of the template. This can be done after the pagination has rendered its data, or during, depending on how your template works..
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

How would i know how to do that??? I set up my tempelate with tables and such, and in four cells i want to put the code.

This is the pagination script that i used, found it online and works with my data. Im a little confused in how to embedd all of this???

Code: Select all

<?php

// Database Connection

include 'connect.php';

// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 10;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
    echo $row['title']."<br />";
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>
Any guidelines would be greatly appreciated.

Thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Just change your echos to variable declarations and then put them wherever you want on the page:

Code: Select all

<?
   
$prevs = "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a>";
$pages = "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>";    
$nexts = "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";}echo "</center>";
?>
<body>
<table width="500" align="center">
<tr>
<td colspan="3" align="center">My Pagination</td>
<tr>
<tr>
<td><?=$prevs;?></td>
<td align="center"><?=$pages;?></td>
<td align="right"><?=$nexts;?></td>
<tr>
</table>
</body>
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

Cool that seemed to have worked.

Until next post

Thanx alot
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Burrito :
<?= This is thought was only an ASP thing <%=
Did not know this works for PHP too.
I wanted to check up google giving <?= as keywords. Its giving more-or-less a blank page - with no search results or suggestions or anything.
Are there some negative sides on using this ?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Not to my knowledge (any negatives)...I've always used it...although I'm sure someone out there will have some issues with it they'll let you know about.

for me however, I've never had problems with it (possibly doesn't work in older versions of php??)...dunno.

I don't think it works with <?php= though, only with <?= but I might be wrong and I'm too lazy to test it right now 8O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<?= is call a short tag echo. It can easily be turned off, so it's best to avoid by always using

Code: Select all

<your html here><?php echo 'foo'; ?>
or use templates. :)
Post Reply