duankui6150 2016-01-27 00:11
浏览 53

用循环时的php分页

So this has been stumpping me for a good bit now, I get the concept i get the tutorials, but a lot of the time they refer to simple raw out put of the data, while my is looping like an image gallery as such example of the page

  <?php
        //external pages area
       include_once('config/database.php');
    include_once('object/chair.php');
        //grabs info from the various pages.sql files
        $database = new Database();
        $conn = $database->getConnection();
        $chair = new Chair($conn);
        //connection made with sql file
        $stmt = $chair->readAll();
        //reading all the data in the sql file

    $rows = $row[0];
            $page_rows = 10;
            $last = ceil($rows/$page_rows);
                if($last < 1){
                    $last = 1;
                }
                   $pagenum = 1;
                   if(isset($_GET['pn'])){
                       $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);   
                   }
                    if ($pagenum < 1) {
                        $pagenum = 1;
                    }else if ($pagenum > $last){
                        $pagenum = $last;
                    }

                   $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

            $sql = "SELECT id, THUMB, chair_name, PRICE FROM office_chairs $limit";

            $textline1 = "pages (<b>$rows</b>)";
            $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";            

            $paginationCtrls = '';
            if($last != 1){

            if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';

        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }    

    }

   $paginationCtrls .= ''.$pagenum.' &nbsp; ';             
for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }


    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }              



}

This is however the code that actually will out put.

   while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

The obvious error is that $rows is equal to an undefined element but I'm not sure what it should be equal too, it doesn't like being equal to any variant of the stmt var.

    <div class="product_box" >


   <a href="chair-details.php?detailsid=<?php echo $row['ID'];?>">
      <img src="img/<?php echo $row['THUMB']; ?>" alt="chair_image"> 
    </a>


  <div class="productdetailwrapper">
    <div class="productleft">
            <div class="productnamebanner">
            <p class="productName" ><?php echo $row['chair_name'];?></p>
            </div>
        <em class="d-price"><?php echo $row['PRICE'];?></em>
            <span class="ratings">
            Not rated
                </span>

       </div>
       <div class="ProductActionAdd" style="display:;">
         <a href="chair-details.php?detailsid=<?php echo $row['ID'];?>" class="btn">More Info!</a>
  </div>


    </div>

</div>


<?php
    }
?>    
  • 写回答

0条回答 默认 最新

    报告相同问题?