Jump to content

[SOLVED] little paging problem with a menu from database


irkevin

Recommended Posts

Ok, i read all the post about paging but still it doesn't solve my problem. That's why i'm here now

 

Here's a code i got on phpfreaks for paging

 

<?php 
  //connect to database
$conn = mysql_connect("localhost","blah","****");
mysql_select_db("kevin",$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 = 6;

// 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 />";
	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"),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> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $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>";
?>

 

So, i have a menu on my site.. like this

 

Bleach

DeathNote

Dragon Ball

etc etc

etc etc

 

when i click on bleach, it goes on a page name multimedia.php, there it shows the data which i inserted in mysql database..

 

when i click on death note, it shows the data from mysql about death note on the same page..thats ok

 

For bleach, i have the pagination set and it works fine because i have set it to 6 information per page and there are moe than 6 info for bleach.. it shows 5 pages in total for bleach. i like that, but for death note, i have only three information and it still show 5 pages ! this is not normal, isn't it?

 

there is no data for dragon ball and it still show the 5 links.. how come  ??? Same for the other one :S

 

if there is no data, it shouldn't show the link, right?

 

the query for my menu go like this

 

<?php
//connect to database
$conn = mysql_connect("localhost","blah","****");
mysql_select_db("kevin", $conn);
$sql = "Select * from multimedia";
$result = mysql_query($sql, $conn) or die(mysql_error());
while($myarray = mysql_fetch_array($result)) {
?>
<a href=multimedia.php?go_to=<?php echo $myarray['mult_id']; ?> > <?php echo $myarray['mult_name']; ?></a> <br />
<?php
};
?>

 

i've used $_GET['go_to'] to show the data on the multimedia.php page and $_GET['page'] for the pagination

 

can someone tell me whats wrong? for bleach everything is ok.. but for dragon ball there's no data on the page yet, why i'm i seeing the next link?? :S

 

my website is http://www.mu-anime.com

 

have a look :S

 

Link to comment
Share on other sites

change

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM multi_file"),0);

to

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM multi_file WHERE mult_id = $temp_go_to"),0);

Link to comment
Share on other sites

try to change

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"multimedia.php?go_to={$temp_go_to}&page=$i\">$i</a> ";
    }
}

with

$how_much_before = 2;
$how_much_after = 2;
$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[] = '...';
$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> ";
    }
}

Link to comment
Share on other sites

generate $pages array that have all value in navigation part

 

$how_much_before = 2;<-- define how much link want to display before current page

$how_much_after = 2;        (for this forum value is 5)

$pages = array(1); <-- always set page no 1

if ($page > $how_much_before + 2) $pages[] = '...'; <-- check if I need ...

for ($i = max($page - $how_much_before, 2); $i < min($page + $how_much_after + 1,$total_pages); $i++) $pages[] = $i;<-- add middle part but not 1st and last page

if ($page < $total_pages - $how_much_after - 1) $pages[] = '...'; <-- check if I need ...

if ($total_pages > 1) $pages[] = $total_pages; <-- add last page if number of pages is > 1

                                                                    (I tested after and i see if i have just 1 page in pagination it shows 2 times)

foreach ($pages as $i){ <--do what you do before but use just $i in pages array

if($page == $I or $i == '...'){ <-- no link for current page and for ...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.