分页不显示isset方法的内容

时间:2017-06-19 05:45:27

标签: php pagination

我正在显示数据库中的照片。

<div class="col-md-10 bg col-md-push-2  ">
    <div class="align_center  gallery">
        <?php
            if(isset($_POST['submit'])){

                include "anj.php";

                $sql =  'SELECT * FROM new_photos WHERE weight BETWEEN 10 AND 15';
                // function anjaan has code for showing images
                anjaan($sql);
            }else{

                include "anj.php";
                $sql='select * from new_photos';
                anjaan($sql);

            }
        ?>
    </div>
    <div class="  align_center ">
        <div class=" col-md-12 pagination gallery">
            <?php    
            echo $paginationctrl;
            ?>
        </div>
    </div>
</div>

我正在使用post方法

<form method="post" action="index.php">
    <input type="Submit" name="submit" value="submit">
</form>

我点击提交按钮,然后它显示第一个查询的图像(“SELECT * FROM new_photos WHERE weight BETWEEN 10 AND 15”),但是当我从分页点击第2页时,它从else加载内容(第二个查询“SELECT *来自new_phoos“)方法。

分页代码 我的anj.php

<?php
 function anjaan($sql ){
 include 'database.php';
 function make_thumb($src, $dest, $desired_width) {

    /* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);

/* find the "desired height" of this thumbnail, relative to the desired 
width  */
$desired_height = floor($height * ($desired_width / $width));

/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);

/* copy source image at a resized size */
 imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, 
 $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
 }
$sql  ;
$query = mysqli_query($db,"$sql" );
$row  = mysqli_num_rows($query);
$rows =$row[0];
$page_rows=8;
$last=ceil($row/$page_rows);
if($last<1){
     $last= 1;
}
 $pagenum=1;
if(isset($_GET['pn'])){
    $pagenum = (int) $_GET['pn'];
}

if($pagenum<1){
     $pagenum=1;
}
else if($pagenum>$last){
     $pagenum=$last;
}

 $limit='LIMIT' .($pagenum-1) * $page_rows.','.$page_rows;
$n=($pagenum-1) * $page_rows;
 $sql1  = "  $sql LIMIT $n ,$page_rows";
  $query = mysqli_query($db,$sql1 );
  global $paginationctrl;
  $paginationctrl='';
 if($last !=1){

     if($pagenum>1){
              $previous = $pagenum-1;
              $paginationctrl .='<a  href="'.$_SERVER['PHP_SELF'].'?
pn='.$previous.'">Pr</a>'.'&nbsp;';
              for($i = $pagenum-3 ;$i<$pagenum;$i++){
                       if($i>0){
                                $paginationctrl .='<a  class="active" 
href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>'.'&nbsp;';
                       }
              }
     }

    $paginationctrl .=''.$pagenum.'&nbsp;'  ;
        for($i = $pagenum+1 ;$i<$last;$i++){
                 $paginationctrl .='<a  class="active" 
   href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>'.'&nbsp;';
                       if($i>=$pagenum+4){
                                break;
                       }
              }
      if($pagenum != $last){
              $next =$pagenum+1;
             $paginationctrl .=' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
     }
 }
  while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
  $r=$row['path'];
     $id=$row['weight'];
  $image = "new_photos/".$row['path'];
 $image1 = "images/thumb/".$row['path'];
$path = 'images/thumb/'.$r;
$thumb =  make_thumb($image, $path,1000);
  ?>
<a  class="fancybox col-md-6" href="<?php echo $image; ?>" data-
 fancybox="image"    >

 <img class =  " galleryimage" src= "<?php echo $image1; ?>" >
<p class=" align_center"><?php echo $id; ?> gm</p>
 </a>

 <?php }}

     ?>

1 个答案:

答案 0 :(得分:0)

这是因为当您使用form和post方法时,它会在$_POST['submit']中发布提交变量,但在您使用<a></a>标记后表示您没有$_POST['submit']变量,这就是为什么else条件正在运行。最好你可以使用url参数和$_GET之类的。

表格

<form method="post" action="index.php?sort=weight">
    <input type="Submit" name="submit" value="submit">
</form>

在视图中使用html

<div class="col-md-10 bg col-md-push-2  ">
    <div class="align_center  gallery">
        <?php
            if(isset($_GET['sort']) && $_GET['sort'] == 'weight'){

                include "anj.php";

                $sql =  'SELECT * FROM new_photos WHERE weight BETWEEN 10 AND 15';
                // function anjaan has code for showing images
                anjaan($sql);
            }else{

                include "anj.php";
                $sql='select * from new_photos';
                anjaan($sql);

            }
        ?>
    </div>
    <div class="  align_center ">
        <div class=" col-md-12 pagination gallery">
            <?php    
            echo $paginationctrl;
            ?>
        </div>
    </div>
</div>

在分页中

if($last !=1){

     if($pagenum>1){
              $previous = $pagenum-1;
              $paginationctrl .='<a  href="'.$_SERVER['PHP_SELF'].'?
sort=weight&pn='.$previous.'">Pr</a>'.'&nbsp;';
              for($i = $pagenum-3 ;$i<$pagenum;$i++){
                       if($i>0){
                                $paginationctrl .='<a  class="active" 
href="'.$_SERVER['PHP_SELF'].'?sort=weight&pn='.$i.'">'.$i.'</a>'.'&nbsp;';
                       }
              }
     }

    $paginationctrl .=''.$pagenum.'&nbsp;'  ;
        for($i = $pagenum+1 ;$i<$last;$i++){
                 $paginationctrl .='<a  class="active" 
   href="'.$_SERVER['PHP_SELF'].'?sort=weight&pn='.$i.'">'.$i.'</a>'.'&nbsp;';
                       if($i>=$pagenum+4){
                                break;
                       }
              }
      if($pagenum != $last){
              $next =$pagenum+1;
             $paginationctrl .=' <a href="'.$_SERVER['PHP_SELF'].'?sort=weight&pn='.$next.'">Next</a>';
     }
 }

检查$_POST['submit']包含null或是否存在,因为如果您的$_POST['submit']包含null,则isset()将返回false,这意味着将执行 例如: -

$array = ['key' => null];
var_dump($array['key']) // false #because key has null value 
var_dump($array['key1']) // false #key not exist