Page 1 of 1

Using my php pagination script in my html file

Posted: Thu Mar 31, 2005 3:13 pm
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

Posted: Thu Mar 31, 2005 3:18 pm
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..

Posted: Fri Apr 01, 2005 9:48 am
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

Posted: Fri Apr 01, 2005 10:12 am
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>

Posted: Fri Apr 01, 2005 12:15 pm
by soianyc
Cool that seemed to have worked.

Until next post

Thanx alot

Posted: Fri Apr 01, 2005 12:27 pm
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 ?

Posted: Fri Apr 01, 2005 12:32 pm
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

Posted: Fri Apr 01, 2005 1:21 pm
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. :)