用户个人资料中的分页

时间:2012-05-21 18:21:39

标签: php sql

我在用户个人资料中加了一个分页。

当我在测试配置文件中时,URL是profile.php?uid = 1

但是现在我想使用分页来从SQL数据库中获取信息。 当我在另一个页面上尝试分页时,它可以正常工作,但正如您所知,分页更改后,URL取决于您的分支。

所以我的问题是分页改变了我的profile.php?uid = 1到profile.php?page = 2

您对如何解决该错误有任何提示吗?

这是我的分页代码。

/*pagination */ 
    $per_page = 5;
    //$pages_query = mysql_query("SELECT COUNT('user_id') FROM users");

    $pages_query = mysql_query("SELECT COUNT('file_name') FROM users WHERE  `user_name` = '{$_SESSION['username']}' ") or die(mysql_error()); 
    $pages = ceil(mysql_result($pages_query, 0) /$per_page);

    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;

    <?php  
 $data = mysql_query("SELECT * FROM users LIMIT $start, $per_page")
 or die(mysql_error()); 
 echo "<table border cellpadding=1>"; 
 while($info = mysql_fetch_array( $data )) 
    { ?><?php
        echo "<tr>"; 
//      echo "<td>".$info['user_id'] . "</td> "; 
        echo "<td>".$info['user_name'] . "</td> ";
    //  echo "<td>".$info['file_name'] . "</td> ";
        ?>
        <td> <a href="download.php?user_id=<?php echo $info['user_id'];?>"> <?php echo $info['file_name']; ?></a></td>
        <?php
        echo "<td>".date('d/m/Y') . "</td>" ;


    }
        echo "</tr>";
        echo "</table>"; 


 if($pages >= 1 && $page <=$pages){

 for ($x = 1; $x<=$pages; $x++){ 

 echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';

 }

 }

 ?> 

/ *分页* /

和个人资料

function fetch_user_info($ uid){     $ uid =(int)$ uid;

    $sql = "SELECT
    `user_name` AS `username`,
    `user_email` AS `email`
FROM `users`
WHERE `user_id` = {$uid} ";

$result = mysql_query($sql);

return mysql_fetch_assoc($result);

}

 $data = mysql_query("SELECT * FROM users") or die(mysql_error()); 

$files = mysql_query("SELECT `file_name` FROM users WHERE  `user_name` = '{$_SESSION['username']}' ") or die(mysql_error()); 
$user_info = fetch_user_info($_GET['uid']);  

我真的不知道如何解决这个问题= /

3 个答案:

答案 0 :(得分:1)

看起来您没有在寻呼机链接中传递用户ID,请尝试更改此行...

echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';

echo ($x == $page) ? '<strong><a href="?uid='.$_GET['uid'].'&page='.$x.'">' .$x. '</a> </strong>' : '<a href="?uid='.$_GET['uid'].'&page=' .$x. '">' .$x. ' </a> ';

答案 1 :(得分:0)

会话中有uid吗?

echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?user_id=' . $_SESSION['uid'] . '&page=' .$x. '">' .$x. ' </a> ';

但是,如果您为uid添加会话,实际上并不需要它。您可以更改fetch_user_info

function fetch_user_info(){ 

    $sql = "SELECT
    `user_name` AS `username`,
    `user_email` AS `email`
FROM `users`
WHERE `user_id` = " . $_SESSION['uid'];

$result = mysql_query($sql);

return mysql_fetch_assoc($result);

}

答案 2 :(得分:0)

<强>错字:

    $page = (isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];

已更改为

    $page = (!isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];

这是一个非常简单的解决方案。您基本上“读取”分页代码中的URL /查询字符串并自动“将其附加到您的URL。我为您编写了一些简单的即插即用代码 - 这将很容易实现(所有您需要的只是1个额外的行代码和一个包含文件 - 'inc_pagination.php', - 到任何需要分页的页面。不修改现有代码):

/*pagination */  
$per_page = 5; 
include 'inc_pagination.php'; /* FIRST LINE ADDED TO YOUR CODE*/

$pages_query = mysql_query("SELECT COUNT('file_name') FROM users WHERE  `user_name` = '{$_SESSION['username']}' ") or die(mysql_error());  
$pages = ceil(mysql_result($pages_query, 0) /$per_page); 

/* REMOVING THESE TWO LINES */
/*  $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
    $start = ($page - 1) * $per_page; */

$data = mysql_query("SELECT * FROM users LIMIT $start, $per_page") or die(mysql_error());         
echo "<table border cellpadding=1>";                                    
while($info = mysql_fetch_array( $data ))                                    
{ ?><?php                                   
    echo "<tr>";                                    
    echo "<td>".$info['user_name'] . "</td> ";                                   
    ?>                                   
    <td> <a href="download.php?user_id=<?php echo $info['user_id'];?>"> <?php echo $info['file_name']; ?></a></td>                                   
    <?php                                   
    echo "<td>".date('d/m/Y') . "</td>" ;                                   

}                                   
    echo "</tr>";                                   
    echo "</table>";    

/* 2nd LINE ADDED (Replaces Existing Page Code) - Add this wherever you need page numbers displayed */
echo paginate_me(ceil(mysql_result($pages_query, 0)/$per_page), $per_page);

请注意,您需要明显删除现有的分页代码块:

if($pages >= 1 && $page <=$pages){                      

   for ($x = 1; $x<=$pages; $x++){                       
      echo ($x == $page) ? '<strong><a href="?page='.$x.'">' .$x. '</a> </strong>' : '<a href="?page=' .$x. '">' .$x. ' </a> ';                      
   }                      

} 

最后这里是包含文件'inc_pagination.php'的代码。请注意,这个有所有的花里胡哨 - 包括用于悬停/鼠标悬停的javascript和其他视觉效果 - 如果你愿意,我还可以包含一个非常简单的文本版本!

<?
//The number of page numbers you want displayed accross page in pagination bar
$nav_links_per_row = 35;
$page = (!isset($_REQUEST['page'])) ? 1 : $_REQUEST['page'];
$start = ($page == 1) ? 0 : ($page - 1) * $per_page ;
$per_page = (!$per_page) ? 5 : $per_page;

function paginate_me($no_of_pages, $per_page, $nav_links_per_row = 35) {

    $page = 1;
    $page = $_REQUEST['page'];
    $x_mid_start = 1;
    $x_mid_end = $no_of_pages;

     /* START: Here is the main code that detects all your existing QUERY STRING and AUTOMATICALLY includes them in the Page Links */
    $x_url = urldecode($_SERVER['HTTP_REFERER']);
    parse_str($x_url, $output);
    $qs = explode('?',urldecode($x_url));
     /* END: QUERY STRING DETECT CODE */ 

    $p_me .= "<br><table align = 'Center' width = '90%' cellpadding = '0' border = '0'><tr>";
    if ($no_of_pages > 35)
    {
        // Go Back 5
        if ( $page > 5 ){
            $bg_color = "White";
            $ObjTopBar = '"jTopBar_start"';
            $q_str = $_SERVER['QUERY_STRING'];
            $jump = (int)$page - 5;
            $title = "User Files: Jump Back 5 Pages";
            $this_page = str_replace("page=$page","page=$jump","?".$q_str);
            $p_me .=  "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: #333333; vertical-align:middle;padding-top: 2px; overflow:auto'>-</div></a></td>";
        }

        //1 to 10
        for ($i = 1; $i <= 8; $i++) 
        {
            if ($i == $page) $bg_color = "orange";
                else $bg_color = "#333333";
            if (!$page) $bg_color = "orange";

            $ObjTopBar = '"jTopBar'.$i.'"';
            $q_str = $_SERVER['QUERY_STRING'];
            $this_page = str_replace("page=$page","page=$i","?".$q_str);
            $title = "User Files: Jump to Page $i";

            $p_me .=  "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
        }

        //Set Starting Page Numbers for Mid Block If Page Count is > 35
        $p_me .=  "<td> <font color=white>... </td>";
        $x_mid_start = floor($no_of_pages / 2) - 5 ;
        $x_mid_end = floor($no_of_pages / 2) + 5 ;

        //if selected page is between half - 5  to half + 5 SHOW start: selected page, end: selected page + 9
        if (($page > 8) && ($page < $no_of_pages - 8)) {
            $x_mid_start = $page - 5;
            $x_mid_end = $page + 5;
        }
    }

    //Empty Blocks...First Few Spaces...if Page Count < 35
    if ($no_of_pages < 35) {    
        for ($k = 1; $k <= floor((35 - $no_of_pages) / 2); $k++)    
            {
                $ObjTopBarLinks = '"divBEnd_'.$k.'"'; 
                $ObjTopBarEmpty = '"divBStart'.$k.'"';
                $p_me .=  "<td><div id=".$ObjTopBarEmpty." onMouseOver='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' onMouseOut='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' style='cursor: pointer; cursor: hand; border: 1px solid silver;  border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: grey; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'> </div></td>";
            }
    }

    // Print Page Numbers in the Center
    for ($i = $x_mid_start; $i <= $x_mid_end; $i++) 
        {
            if ($i == $page) $bg_color = "orange";
                else $bg_color = "#333333";
            if (!$page) $bg_color = "orange";

            $ObjTopBar = '"jTopBar'.$i.'"';
            $q_str = $_SERVER['QUERY_STRING'];
            $this_page = str_replace("page=$page","page=$i","?".$q_str);
            $title = "User Files: Jump to Page $i";

            $p_me .=  "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
        }

    //Empty Blocks...Last Few Spaces...If Page Count < 35
    if ($no_of_pages < 35) {    
        for ($k = 1; $k <= ceil(($nav_links_per_row - $x_mid_end)/2); $k++) 
            {
                $ObjTopBarLinks = '"divBStart'.$k.'"';
                $ObjTopBarEmpty = '"divBEnd_'.$k.'"';
                $p_me .=  "<td><div id=".$ObjTopBarEmpty." onMouseOver='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' onMouseOut='javascript: onMouseOverOutE(".$ObjTopBarLinks.")' style='cursor: pointer; cursor: hand; border: 1px solid silver;  border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: grey; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'> </div></td>";
            }
    }
    else
    {   
        //Page Count is > 35
        $p_me .=  "<td> <font color=white>... </td>";
        //no_of_pages-10 to end
        for ($i = $no_of_pages - 8; $i <= $no_of_pages; $i++) 
        {

            if ($i == $page) $bg_color = "orange";
                else $bg_color = "#333333";
            if (!$page) $bg_color = "orange";

            $ObjTopBar = '"jTopBar'.$i.'"';
            $q_str = $_SERVER['QUERY_STRING'];
            $this_page = str_replace("page=$page","page=$i","?".$q_str);
            $title = "User Files: Jump to Page $i";
            $p_me .=  "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: white; vertical-align:middle;padding-top: 2px; overflow:auto'>".$i."</div> </a></td>";
        }

        // Go Forward 5
        if ( $page < $no_of_pages - 5 ){
            $bg_color = "White";
            $ObjTopBar = '"jTopBar_end"';
            $q_str = $_SERVER['QUERY_STRING'];
            $jump = (int)$page + 5;
            $title = "User Files: Jump Forward 5 Pages";
            $this_page = str_replace("page=$page","page=$jump","?".$q_str);
            $p_me .=  "<td><a title='$title' href='".$this_page."'> <div id=".$ObjTopBar." onMouseOver='javascript: onMouseOverOut(".$ObjTopBar.")' onMouseOut='javascript: onMouseOverOut(".$ObjTopBar.")' style='border: 1px solid silver; border-radius: 4px; -moz-border-radius: 3px; width: 20px; height: 15px; text-align: center; background: ".$bg_color."; color: #333333; vertical-align:middle;padding-top: 2px; overflow:auto'>+</div></a></td>";           }
    }
            $p_me .=  "</tr></table><br>";
    return $p_me;
}   
?>


<script type="text/javascript">

function onMouseOverOut(Obj) {
    setTimeout(function() { onMouseOverOut1(Obj) }, 300);
}

function onMouseOverOut1(x) {

    var ODiv = document.getElementById(x);
    var currBg = ODiv.style.background;

    if (currBg == "orange") return false;
    if (x == "jTopBar_start") return false;
    if (x == "jTopBar_end") return false;

    if (ODiv.style.background.indexOf("red") < 0)
        ODiv.style.background = "red";
    else
    if (ODiv.style.background.indexOf("#333333") < 0)
            ODiv.style.background = "#333333";
}

function onMouseOverOutE(Obj) {
    setTimeout(function() { onMouseOverOutE1(Obj) }, 300);
}       

function onMouseOverOutE1(Obj) {
    var ODiv = document.getElementById(Obj);
    if (ODiv.style.border.indexOf("grey") < 0)
            ODiv.style.border = "1px solid grey";
    else
    if (ODiv.style.border.indexOf("silver") < 0)
            ODiv.style.border = "1px solid silver";

    if (ODiv.style.background.indexOf("white") < 0)
            ODiv.style.background = "white";
    else
    if (ODiv.style.background.indexOf("grey") < 0)
            ODiv.style.background = "grey";
}   
</script>