我有一个图像页面,其中填充了从MySQL数据库中使用PHP查询的信息。我正在尝试将查询结果拆分为多个页面,并有一个选择框,允许用户决定每页有多少结果。
问题是,我被困住了!我正在使用javascript发布选择框onChange,但不知道如何将该结果导入查询($limit
)。我也不知道如何获取页码(在查询中用$curPage
表示)。
希望你能解决这个问题并填补空白:
<form name=imgNum action="new_arrivals_img.php" method=POST>
<p>
<select name=ComboName size=1 onChange="imgNum.submit();">
<option value="12" SELECTED>12</option>
<option>16</option>
<option>20</option>
</select>
</p>
</form>
<ul class="new_arrivals_gallery">
<?php
$mysql_link = mysql_connect("localhost", "root", "root");
mysql_select_db("new_arrivals_imgs") or die("Could not select database");
$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
}
?>
</ul>
此外,表单操作是否指向当前页面?或者我必须有两页?
答案 0 :(得分:1)
它位于$_POST["ComboName"]
变量:
$limit = (isset($_POST["ComboName"]) ? $_POST["ComboName"] : 16); // 16 is some default value
答案 1 :(得分:1)
将方法从post更改为get,传递变量...并检查页面:
<?php
$curPage = 0;
if(isset($_GET['page'])){
$curPage = (int) $_GET['page'];
}
?>
<form name=imgNum action="new_arrivals_img.php" method="get">
<p>
<select name=ComboName size=1 onChange="imgNum.submit();">
<option value="12" SELECTED>12</option>
<option>16</option>
<option>20</option>
</select>
<input type="hidden" name="page" value="<?php echo $curPage; ?>" />
</p>
</form>
<ul class="new_arrivals_gallery">
<?php
if((int) $_GET['ComboName'] > 0){
$limit = (int) $_GET['ComboName'];
} else {
$limit = 12;
}
$mysql_link = mysql_connect("localhost", "root", "root");
mysql_select_db("new_arrivals_imgs") or die("Could not select database");
$query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());
if(!$query) {
echo "Cannot retrieve information from database.";
} else {
while($row = mysql_fetch_assoc($query)) {
echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
}
?>
</ul>