//This is index.php
<?php
session_start();
include_once 'dbconnect.php';
include 'template_header.php';
include("functions.php");
?>
<html>
<head>
<meta charset="utf-8">
<title>eCommerce template By Adobe Dreamweaver CC</title>
<link href="eCommerceAssets/styles/eCommerceStyle.css" rel="stylesheet" type="text/css">
<style type="text/css">
#content #text-center nav p {
font-family: Cambria, Hoefler Text, Liberation Serif, Times, Times New Roman, serif;
}
#content #text-center nav {
font-family: Lucida Grande, Lucida Sans Unicode, Lucida Sans, DejaVu Sans, Verdana, sans-serif;
}
</style>
<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script><script src="http://use.edgefonts.net/montserrat:n4:default;source-sans-pro:n2:default.js" type="text/javascript"></script>
</head>
<body>
<div id="content">
<section class="sidebar">
<!-- This adds a sidebar with 1 searchbox,2 menusets, each with 4 links -->
<input type="text" id="search" value="search">
<input type="text" id="search" user_query" placeholder="Search a Product" />
<div id="menubar">
<nav class="menu">
<h2><!-- Title for menuset 1 --> Product Categories </h2>
<hr>
<form action="">
<input type="checkbox" name="type" value="Digital">Digital<br>
<input type="checkbox" name="type" value="Mechanical">Mechanical<br>
<input type="checkbox" name="type" value="Men">Men<br>
<input type="checkbox" name="type" value="Women">Women<br>
<input type="checkbox" name="type" value="Kids">Kids
</form>
</nav>
<nav class="menu">
<h2>Product Brands</h2>
<!-- Title for menuset 2 -->
<hr>
<form action="">
<input type="checkbox" name="type" value="Swatch">Swatch<br>
<input type="checkbox" name="type" value="Rado">Rado<br>
<input type="checkbox" name="type" value="Rolex">Rolex<br>
<input type="checkbox" name="type" value="Citizen">Citizen
</form>
</nav>
<nav class="menu">
<h2>Product Price</h2>
<!-- Title for menuset 3 -->
<hr>
<form action="">
<input type="checkbox" name="type" value="BelowRM100">Below RM 100<br>
<input type="checkbox" name="type" value="RM101 - RM500">RM100 - RM200<br>
<input type="checkbox" name="type" value="RM501 - RM1000">RM501 - RM1000<br>
<input type="checkbox" name="type" value="RM1001 - RM1500">RM1001 - RM1500<br>
<input type="checkbox" name="type" value="RM1501++">RM1501++
</form>
</nav>
<div id="form">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="submit" name="search" value="Search" />
</form>
</div>
</div>
</section>
<div>
<section class='mainContent'>
<?php
getPro();
getcatPro();
getBrandPro();
?>
</section>
<div>
<center>
<style>
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
}
ul.pagination li a.active {
background-color:#000000;
color:black;
}
div.center {
text-align: center;
}
ul.pagination li a:hover:not(.active) {background-color:#14E318;}
</style>
<?php
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 6; //if you want to dispaly 10 records per page then you have to change here
$startpoint = ($page * $limit) - $limit;
$statement = "products order by product_id asc"; //you have to pass your query over here
$res=mysql_query("select * from {$statement} LIMIT {$startpoint} , {$limit}");
while($row=mysql_fetch_array($res))
{
echo $row["name"];
echo "<br>";
}
?>
<?php
echo "<div id='pagingg' >";
echo pagination ($statement,$limit,$page);
echo "</div>";
?>
</center>
</div>
</div>
<?php include 'template_footer.php';?>
</body>
</html>
//This is functions.php
<?php
$db = mysqli_connect("localhost","root","","ecommerce");
function getPro(){
global $db;
if(!isset($_GET['type'])){
if(!isset($_GET['brand'])){
$get_products = "select * from products order by rand() LIMIT 0,6";
$run_products = mysqli_query($db, $get_products);
while ($row_products=mysqli_fetch_array($run_products)){
$pro_id = $row_products['product_id'];
$pro_title = $row_products['product_title'];
$pro_desc = $row_products['product_desc'];
$pro_price = $row_products['product_price'];
$pro_image = $row_products['product_img1'];
echo "
<div id='single_product'>
<h3>$pro_title</h3>
<img src='Images/$pro_image' width='180' height='180' /><br>
<p><b>Price: RM $pro_price </b></p>
<a href='details.php?pro_id=$pro_id' style='float:left;'>Details</a>
<a href='index.php?add_cart=$pro_id'><button style='float:right;'>Add to Cart</button></a>
</div>
";
}
}
}
}
function getcatPro(){
global $db;
if(isset($_GET['cat'])){
$cat_id = $_GET['cat'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($db, $get_cat_pro);
$count = mysqli_num_rows($run_cat_pro);
if($count==0){
echo "<h2>No Products found in this category!</h2>";
}
while ($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
echo "
<div id='single_product'>
<h3>$pro_title</h3>
<img src='Images/$pro_image' width='180' height='180' /><br>
<p><b>Price: RM $pro_price </b></p>
<a href='details.php?pro_id=$pro_id' style='float:left;'>Details</a>
<a href='index.php?add_cart=$pro_id'><button style='float:right;'>Add to Cart</button></a>
</div>
";
}
}
}
function getBrandPro(){
global $db;
if(isset($_GET['brand'])){
$brand_id = $_GET['brand'];
$get_brand_pro = "select * from products where brand_id='$brand_id'";
$run_brand_pro = mysqli_query($db, $get_brand_pro);
$count = mysqli_num_rows($run_brand_pro);
if($count==0){
echo "<h2>No Products found for this brand!!</h2>";
}
while ($row_brand_pro=mysqli_fetch_array($run_brand_pro)){
$pro_id = $row_brand_pro['product_id'];
$pro_title = $row_brand_pro['product_title'];
$pro_desc = $row_brand_pro['product_desc'];
$pro_price = $row_brand_pro['product_price'];
$pro_image = $row_brand_pro['product_img1'];
echo "
<div id='single_product'>
<h3>$pro_title</h3>
<img src='Images/$pro_image' width='180' height='180' /><br>
<p><b>Price: RM $pro_price </b></p>
<a href='details.php?pro_id=$pro_id' style='float:left;'>Details</a>
<a href='index.php?add_cart=$pro_id'><button style='float:right;'>Add to Cart</button></a>
</div>
";
}
}
}
?>
<?php
/**
* @link: http://www.Awcore.com/dev
*/
function pagination($query, $per_page = 10,$page = 1, $url = '?'){
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysql_fetch_array(mysql_query($query));
$total = $row['num'];
$adjacents = "2";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class='pagination'>";
$pagination .= "<li class='details' style='margin-top:2px'><p>Page $page of $lastpage</p></li>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";
}
else
{
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
}else{
$pagination.= "<li><a class='current'>Next</a></li>";
$pagination.= "<li><a class='current'>Last</a></li>";
}
$pagination.= "</ul>'n";
}
return $pagination;
}
?>
我正在尝试制作一个简单的电子商务网站,我设法制作了一个分页,但功能有点奇怪,它可以工作,但不是想象中的样子。我在我的网站上上传了7个产品。1页可以包含6个产品。我想看看它的工作原理,这就是为什么我插入7个产品。通常当你点击第2页时,只有产品7会出现,但在我的情况下,第1页的其他5个产品都会出现产品7。我不知道如何解决这个问题,我需要帮助。而不是给出截图,或者在这里复制粘贴代码。这是我的项目链接,所以你可以直接彻底地检查:
https://www.dropbox.com/s/efpt3z33crsckl7/E-commerce.zip?dl=0
如果你足够善良,请指出我在项目中还做错了什么:D
编辑:
确保URL具有page=2声明,如果它的page=1,那么它将不适用于第二个页面。