如何在模板PHP页面上加载下一个图像列表。

时间:2012-07-09 14:14:10

标签: php image gallery

我有一个我正在处理的图库页面,似乎无法使用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>
    ";
?>

2 个答案:

答案 0 :(得分:3)

问题在于以下几点:

$start_img=0;
$last_img=6;

&安培;

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

当page = 1时,这些行会将图片从ID 0加载到ID 6,共有7张图片,当page = 2时,代码会生成$statr_img = ((2-1)*6)+1 = {{1实际上将是第8个图像,因为我们从0开始计数。相同,7 = ID 7再次,$last_img = 7+5是第13个图像。

<强>更新

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

12

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

ID 12

Page = 2,结果:

$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 = 3,结果:

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

此外,强烈建议不要使用$prev_page = 2-0 = 1 $next _page = 2+1 = 3 $start_img = ((2-1)*6) = 1*6 = 6 $last_img = 6+5 = 11 进行布局。请查看CSS&amp;资料核实。查看此代码以了解CSS中的相同设计。

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

所以,现在您的页面结构如下:

<table>

当然,您需要查看每个//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" 的CSS的神奇+----------------table-----------------+ | +---------------row----------------+ | | | +----cell------+------cell-----+ | | | | | img | img | | | | | +--------------+---------------+ | | | +---------------row----------------+ | | | +----cell------+------cell-----+ | | | | | img | img | | | | | +--------------+---------------+ | | | +---------------row----------------+ | | | +----cell------+------cell-----+ | | | | | img | img | | | | | +--------------+---------------+ | | | +----------------------------------+ | | +--------------links---------------+ | | | Prev Next | | | +----------------------------------+ | +--------------------------------------+ 属性。我会把它留给你(Google Css Float)

答案 1 :(得分: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个条件,因为唯一的条件将被满足。