在PHP中显示带有分页的搜索结果

时间:2019-03-12 11:07:05

标签: php mysql

如何使我的搜索从一个班级开始……我可以在主要页面上显示搜索结果,但我希望它显示所有结果,如果该结果超出分页结果,则客户可以轻松地移至另一页

    <?php 
        // if it's going to need the database, then it's
        //probably smart to require it before we start.
        @require_once(LIB_PATH.DS.'database.php');

        class Product extends DatabaseObject {
            protected static $table_name="products";
            protected static $db_fields=array('id', 'product_id', 'image', 'title', 'slug', 'description', 'price');

            public $id;
            public $product_id;
            public $image;
            public $title;
            public $slug;
            public $description;
            public $price;

        public static function search(){
                global $database;
                $sql  = "SELECT * FROM products WHERE image LIKE '{%$search%'";
                $sql .= " OR title LIKE '{%$search%}'";
                $sql .= " OR slug LIKE '%$search%'";
                $sql .= " OR description LIKE '%$search%'";
                $sql .= " OR price LIKE '%$search%'";
                $total_count = count($sql);
                $result_set = $database->query($total_count);
                $row = $database->fetch_assoc($result_set);
                return array_shift($row);
            }

        ?>

请检查我的代码...我有点困惑。

<?php if(empty($_POST['search'])){
    $session->message("<div class='error-msg'>Search cannot be empty</div>");
    redirect_to('photos.php'); 
} 
?>
<?php include_layout_template('header2.php'); ?>
<div class="container">
    <div class="row">
        <?php
    if(isset($_POST['submit'])){
        // 1. the current page number ($current_page)
        $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
        // 2. records per page ($per_page)
        $per_page = 10;
        // 3. total record count ($total_count)
        //$total_count = Product::count_all();
        $total_count = Product::search();
        // Find all photos
        // use pagination instead
        $pagination = new Pagination($page, $per_page, $total_count);
        // Instead of finding all records, just find the records 

        $search = $database->escape_value($_POST['search']);
//      $sql  = "SELECT * FROM products WHERE image LIKE '%$search%'";
//      $sql .= " OR title LIKE '%$search%'";
//      $sql .= " OR slug LIKE '%$search%'";
//      $sql .= " OR description LIKE '%$search%'";
//      $sql .= " OR price LIKE '%$search%'";
//      $sql .= " LIMIT {$per_page} ";
//      $sql .= "OFFSET {$pagination->offset()}";
        $search = new Product();
        $search->search = $search;


        $photos = Product::find_by_sql($search);
        //$total_count = count($photos);
        //echo $numresults = '<p class="error-msg">There are '.$total_count.' results in your search</p><br/><br/>';

    foreach ($photos as $photo): ?>
    <div class="col-md-4 col-sm-6">
    <div class="row">
<div id="pagination" style="clear: both;">
<nav aria-label="Page navigation example">
  <ul class="pagination">  
    <?php 
    for($i=1; $i <= $pagination->total_pages(); $i++){
        if($i == $page) {
//          echo " <span class=\"selected\">{$i}</span> ";
//      } else{
//      echo " <a href=\"search.php?page={$i}\">{$i}</a> ";
    }
    }
    if($pagination->total_pages() > 1) {
        if($pagination->has_previous_page()) {
            echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
            echo $pagination->previous_page();
            echo "\">&laquo; Previous</a></li> ";
        }


        if($pagination->has_next_page()){
            echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
            echo $pagination->next_page();
            echo "\">Next &raquo;</a></li>";
        }
    }
    ?>
</ul>
    </nav>
</div>
</div>
    <div class="thumbnail">
     <form method="post" action="cart.php?action=add&id=<?php echo $photo->id; ?>" role="form" class="form-vertical">
     <a href="order_review.php?id=<?php echo $photo->id; ?>"><img src="<?php echo $photo->image_path(); 
     ?>" class="img-thumbnail" alt="responsive image"></a>
    <div class="caption">
    <h3 class="text-info text-center"><?php echo $photo->title; ?></h3>
    <p class="text-muted text-center price card-header"><span class="currency">N</span><?php echo $photo->price; ?></p>
    <p class="text-center"><em><?php echo $photo->description; ?></em></p>
    <input type="hidden" name="id" class="form-control" value="<?php echo $photo->id; ?>" />
    <input type="hidden" name="title" class="form-control" value="<?php echo $photo->title; ?>" />
    <input type="hidden" name="slug" class="form-control" value="<?php echo $photo->slug; ?>" />
    <input type="hidden" name="description" class="form-control" value="<?php echo $photo->description; ?>" />
    <input type="hidden" name="price" class="form-control" value="<?php echo $photo->price; ?>"/>
    <div class="col-sm-4 mx-auto d-block">
    <select name="quantity" class="form-control" name="quantity">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    </div>
      </div>
      <div class="col-sm-4 col-sm-push-3 mx-auto d-block">
      <input type="submit" name="add_to_cart" class="btn bg-success" value="Add to cart" />
      </div><br/>
      </form>
    </div>
     </div>
    <?php endforeach; }
    ?>
    </div>
</div>

请注意我注释掉的代码...我有些困惑。

0 个答案:

没有答案