1

我有一个正在处理的图库页面,似乎无法通过使用 GET 参数 page=2 正确加载下一组图像。

我正在使用一张带有 6 张图片以及下一个和上一个按钮的表格。但是,当我从第 1 页单击“下一步”时,它会加载图像 8 到 13,而不是商定的 7 到 12。

代码在这里:

<?php 
echo "<table width=\"490\" border=\"1\" align=\"center\">";
if(isset($_GET['page'])){
//Read Page, Set Start image, End image
$cur_page=abs($_GET['page']);
//if the page is page1, then set the defaults
if($cur_page==1){
    $prev_page=0;
    $next_page=2;
    $start_img=0;
    $last_img=6;
    }
    //if the page is not page1, calculate the defaults
    else{
        $prev_page=$cur_page-1;
        $next_page=$cur_page+1;
        $start_img=(($cur_page-1)*6)+1;
        $last_img=$start_img+5;
        }
}else{
//Page=1 start image=1 end image=6
$cur_page=0;
$prev_page=1;
$next_page=2;
$start_img=0;
$last_img=6;
}
//Do the sql query for the images
$sql="SELECT * FROM images LIMIT $start_img, 6";
$result = mysql_query($sql, $conn) or die(mysql_error());
//Set a counter variable to iterate the image display
$count=0;

//Begin the table that displays the images.
while ($newArray = mysql_fetch_array($result)) {
$count++;
//Print the odd-numbered column first
if($count%2==1){
    echo "<tr><td width=\"50%\"><a href=\"$newArray[Pic_Address]\">$newArray[Pic_Address]</a></td>";
//Print the even-numbered column next
}else{
    echo "<td width=\"50%\"><a href=\"$newArray[Pic_Address]\">$newArray[Pic_Address]</a></td></tr>";
}
}
//print the next and previous Links
echo "<tr>";
if($prev_page==0){echo "<td align=\"center\">Prev</td>";}else{echo "<td align=\"center\"><a href=\"index.php?page=$prev_page\">Prev</a></td>";};
echo "<td align=\"center\"><a href=\"index.php?page=$next_page\">Next</a></td>";

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

//Printscreen to test the page-load variables.

echo "  <p align=\"center\">
    current page=$cur_page <br>
    prev_page=$prev_page <br>
    next_page=$next_page <br>
    start_img=$start_img <br>
    last_img=$last_img <br>
    </p>
    ";
?>
4

2 回答 2

3

问题出在以下几行:

$start_img=0;
$last_img=6;

&

$start_img=(($cur_page-1)*6)+1;
$last_img=$start_img+5;

这些行,当 page=1 时,将从ID 0to加载图像ID 6,总共 7 个图像,当 page=2 时,代码将导致$statr_img = ((2-1)*6)+1=7并且ID 7实际上将是第 8 个图像,因为我们从 0 开始计数。同样,$last_img = 7+5=12和再次ID 12是第 13 张图。

更新:

这是更新的完整代码。不需要 if/else 。当然,您需要清理 $_GET。

$cur_page = $_GET["page"];
$prev_page=$cur_page-1;
$next_page=$cur_page+1;
$start_img=(($cur_page-1)*6);
$last_img=$start_img+5;

上述代码中 Page =1,2,3($cur_page 为 1 或 2 或 3)的
结果:Page=1,结果:

$prev_page = 1-0 = 0    
$next _page = 1+1 = 2
$start_img = ((1-1)*6) = 0*6 = 0
$last_img = 0+5 = 5

页=2,结果:

$prev_page = 2-0 = 1
$next _page = 2+1 = 3
$start_img = ((2-1)*6) = 1*6 = 6
$last_img = 6+5 = 11

页=3,结果:

$prev_page = 3-0 = 2
$next _page = 3+1 = 4
$start_img = ((3-1)*6) = 2*6 = 12
$last_img = 12+5 = 17

此外,<table>强烈建议不要使用 for 布局。请查看 CSS 和 DIV。查看此代码以在 CSS 中暗示相同的设计。

//continued from above $var calculating code + your MySql code

//Set a counter variable to iterate the image display
$count=0;

while ($newArray = mysql_fetch_array($result)) {
    $count++;
    echo "<div id=\"table\">\r\n";
    // Print the odd-numbered column first
    // if ID is 0,2,4 then Images are 1,3,5 OR odd
    if($count%2==0){
        echo "<div id=\"row\">\r\n<div class=\"cell\">\r\n<a href=\"$newArray[Pic_Address]\">$newArray[Pic_Address]</a></div><!--//cell-->\r\n";
    } else { // IDs are 1,3,5 OR Images are 2,4,6 OR even
        echo "<div class=\"cell\"\r\n><a href=\"$newArray[Pic_Address]\">$newArray[Pic_Address]</a></div><!--//cell-->\r\n</div><!--//row-->\r\n";
    } //else ends
 // while ends

//print the next and previous Links
echo "<div id=\"links\">\r\n";

if($prev_page != 0)
    echo "<a href=\"index.php?page=$prev_page\">";
echo "Prev"
if($prev_page != 0)
        echo "</a>\r\n";
echo "<a href=\"index.php?page=".$next_page."\">Next</a>\r\n";
echo "</div><!--//links-->\r\n"

echo "</div><!--//table-->\r\n"

所以,现在你的页面结构是这样的:

+----------------table-----------------+
| +---------------row----------------+ |
| | +----cell------+------cell-----+ | |
| | |    img       |     img       | | |
| | +--------------+---------------+ | |
| +---------------row----------------+ |
| | +----cell------+------cell-----+ | |
| | |    img       |     img       | | |
| | +--------------+---------------+ | |
| +---------------row----------------+ |
| | +----cell------+------cell-----+ | |
| | |    img       |     img       | | |
| | +--------------+---------------+ | |
| +----------------------------------+ |
| +--------------links---------------+ |
| |    Prev                Next      | |
| +----------------------------------+ |
+--------------------------------------+

当然,您需要检查Float每个Div. 我会把它留给你(Google Css Float)

于 2012-07-09T14:33:18.833 回答
1

第一个图像被索引为 0 而不是 1。

if($cur_page==1){ 
   $prev_page=0; 
   $next_page=2; 
   $start_img=0; 
   $last_img=5; 
} 
//if the page is not page1, calculate the defaults 
else{
    $prev_page=$cur_page-1; 
    $next_page=$cur_page+1; 
    $start_img=($cur_page-1)*6; 
    $last_img=$start_img+5; 
} 

start_img 不应加 1。

此外,您不需要 2 个 else 条件,因为只有一个会满足。

于 2012-07-09T14:35:54.773 回答