我有一个正在处理的库页面,似乎无法使用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>
";
?>
问题出在以下几行:
$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
=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)的结果:
页码=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>
。请查看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 | |
| +----------------------------------+ |
+--------------------------------------+
当然,您需要为每个Div
检查CSS的神奇Float
属性。我会把它留给你(谷歌Css浮动)。
第一个图像的索引为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。
此外,你不需要另外两个条件,因为只有一个条件会得到满足。