Jump to content

[SOLVED] help with pagination


irkevin

Recommended Posts

Hi hope everyone is fine, i have this pagination. i got it on the phpfreaks tutorial

 

<?php 
   include('config.php');
  //connect to database
$conn = mysql_connect($server, $dbusername, $dbpassword);
mysql_select_db($db_name,$conn);

// 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

$temp_go_to = $_GET['go_to'];

$sql = mysql_query("SELECT * FROM multi_file WHERE mult_id = $temp_go_to LIMIT $from, $max_results");

while($myarray = mysql_fetch_array($sql)){
    // Build your formatted results here.
        echo $myarray['synopsis']."<br /><br />";
	if (!empty($myarray['file_picture'])) {
    echo "<img src='" .$myarray['file_picture']. "' align=\"right\" alt=\"\" />";
	}
	echo "<font color=\"#3399FF\">Episode:</font>" . " " .$myarray['file_name']."<br />";
	echo "<font color=\"#3399FF\">Title:</font>" . " " .$myarray['file_description']."<br />";
	echo "<font color=\"#3399FF\">Link:</font>" ." ". "<a href=\"".$myarray['file_link']. "\" target=\"_blank\">Click Here To Download</a>";

}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM multi_file WHERE mult_id = $temp_go_to"),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=\"multimedia.php?go_to={$temp_go_to}&page=$prev\"><<Previous</a> ";
}
$how_much_before = 1;
$how_much_after = 1;
$pages = array(1);
if ($page > $how_much_before + 2) $pages[] = '...';
for ($i = max($page - $how_much_before, 2); $i < min($page + $how_much_after + 1,$total_pages); $i++) $pages[] = $i;
if ($page < $total_pages - $how_much_after - 1) $pages[] = '...';
if ($page < $total_pages) $pages[] = $total_pages;
foreach ($pages as $i){
if($page == $i or $i == '...'){
        echo " $i ";
        } else {
            echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$i\">$i </a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$next\">Next>></a>";
}
echo "</center>";
?>

 

It works just fine but need a little mod to it..For example, When i add something it goes like this

 

Episode: Bleach Episode 1

Title: The day I became a shinigami

Link: Click Here To Download

 

Episode: Bleach Episode 2

Title: A shinigami's work

Link: Click Here To Download

 

Select a Page

1 2 ... 14 Next>>

 

When i go to page 2, i get episode 3 and 4

Is there a way to make the newest post to the top? Like this

 

 

Episode: Bleach Episode 4

Title: whatever

Link: Click Here To Download

 

Episode: Bleach Episode 3

Title: whatever

Link: Click Here To Download

 

and when i go to page 2, i'll get the episode 2 and 1 ! see what i mean people?

 

Can someone help me?

Link to comment
https://forums.phpfreaks.com/topic/75833-solved-help-with-pagination/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.