我编写了一个PHP脚本,可以选择过滤记录并逐页显示过滤后的记录。
我的问题: 当我应用过滤器时,会显示记录,但是当我点击任何页码时,它会以页面方式显示所有记录,而不是逐页显示过滤后的记录。
我该如何纠正?
这是我的完整脚本,
<body>
<form style="background-color:darkorange; font-size: 13px;" id="form1" name="form1" method="post" action="displaydesign.php">
<label>Design Category</label>
<select name="dlocation">
<option value="">All</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY dlocation ORDER BY dlocation";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["dlocation"]."'".($row["dlocation"]==$_REQUEST["dlocation"] ? " selected" : "").">".$row["dlocation"]."</option>";
}
?>
</select>
<input type="text" name="string" id="string" value="<?php echo stripcslashes($_REQUEST["string"]); ?>" placeholder="Search by Name or City" />
<input type="submit" name="button" id="button" value="Filter" />
<a style="background-color:white;" href="displaydesign.php"> Reset</a>
</form>
<hr>
<?php
if ($_REQUEST["string"]<>'') {
$search_string = " AND (dname LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR dcity LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["dlocation"]<>'') {
$search_dlocation = " AND dlocation='".mysql_real_escape_string($_REQUEST["dlocation"])."'";
}
$per_page=2; // no.of records per page
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE did>0".$search_string.$search_dlocation." order by did desc LIMIT $start_from, $per_page";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {?>
<div class="row">
<?php while ($row = mysql_fetch_assoc($sql_result)) { ?>
<div class="col-sm-6">
<div class="card" >
<h3 class="card-header card-success text-center"><?php echo $row['dlocation'] ?></h3>
<img class="card-img-top img-fluid" src="<?php echo $row['dimage'] ?>" alt="Card image cap">
<div class="card-block ">
<h4><span class="badge badge-default">Designer Information</span></h4>
<h5 class="card-title"><?php echo $row['dname'] ?></h5>
<h6 class="card-subtitle mb-2 text-muted "><?php echo ($row['dcity'])?></h6>
<p class="text-right">
<a class="btn btn-danger btn-sm " data-toggle="collapse" href="#collapseExample<?php echo ($row['did'])?>" aria-expanded="false" aria-controls="collapseExample<?php echo ($row['did'])?>">
Know More..
</a>
</p>
<div class="collapse" id="collapseExample<?php echo ($row['did'])?>">
<div class="card card-block">
<h5><span class="badge badge-warning">Contact Info</span></h5>
<h6 class="card-subtitle mb-2 text-muted"><a href="mailto:<?php echo ($row['demail'])?>" target="_top"><?php echo ($row['demail'])?></a></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['dmobile'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['daddress'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['dcity'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><a href="<?php echo ($row['dwebsite'])?>"><?php echo ($row['dwebsite'])?></a></h6>
<!--ACCORDION START-->
<div id="accordion<?php echo ($row['did'])?>" role="tablist" aria-multiselectable="true">
<div class="card">
<div class="card-header" role="tab" id="headingOne<?php echo ($row['did'])?>">
<h5 class="mb-0 btn-sm">
<a data-toggle="collapse" data-parent="#accordion<?php echo ($row['did'])?>" href="#collapseOne<?php echo ($row['did'])?>" aria-expanded="true" aria-controls="collapseOne<?php echo ($row['did'])?>">
Image Description
</a>
</h5>
</div>
<div id="collapseOne<?php echo ($row['did'])?>" class="collapse show" role="tabpanel" aria-labelledby="headingOne<?php echo ($row['did'])?>">
<div class="card-block">
<?php echo $row['dimagedescription'] ?>
</div>
</div>
</div>
<div class="card">
<div class="card-header" role="tab" id="headingTwo<?php echo ($row['did'])?>">
<h5 class="mb-0 btn-sm">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion<?php echo ($row['did'])?>" href="#collapseTwo<?php echo ($row['did'])?>" aria-expanded="false" aria-controls="collapseTwo<?php echo ($row['did'])?>">
Software Used
</a>
</h5>
</div>
<div id="collapseTwo<?php echo ($row['did'])?>" class="collapse" role="tabpanel" aria-labelledby="headingTwo<?php echo ($row['did'])?>">
<div class="card-block">
<p class="card-text"><?php echo $row['dsoftwareused'] ?></p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" role="tab" id="headingThree<?php echo ($row['did'])?>">
<h5 class="mb-0 btn-sm">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion<?php echo ($row['did'])?>" href="#collapseThree<?php echo ($row['did'])?>" aria-expanded="false" aria-controls="collapseThree<?php echo ($row['did'])?>">
My Brand Recommendation
</a>
</h5>
</div>
<div id="collapseThree<?php echo ($row['did'])?>" class="collapse" role="tabpanel" aria-labelledby="headingThree<?php echo ($row['did'])?>">
<div class="card-block">
<div class="card">
<ul class="list-group list-group-flush">
<li class="list-group-item"><?php echo $row['dbrandname'] ?></li>
<li class="list-group-item"><?php echo $row['dbrandsegment'] ?></li>
<li class="list-group-item"><?php echo $row['dbrandwebsite'] ?></li>
<li class="list-group-item"><?php echo $row['dbrandemail'] ?></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!--ACCORDION END-->
</div>
</div>
</div>
<div class="card-footer">
<small class="text-muted">Design ID:- <?php echo stripcslashes($row['did']) ?> Submitted on :-<?php echo stripcslashes($row['dsubmissiondate']) ?></small>
<br>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } else { ?>
<h3>No results found for the desired Search.</h3>
<?php } ?>
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<div>
<?php //for page numbers display at bottom of page
//Now select all from table for pagination
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE did>0".$search_string.$search_dlocation." order by did desc";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
// Count the total records
$total_records = mysql_num_rows($sql_result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='displaydesign.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='displaydesign.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='displaydesign.php?page=$total_pages'>".'Last Page'."</a></center> ";
?>
</div>
</body>
答案 0 :(得分:0)
我一直在查看你的代码,我还设置了一个SQL-Fiddle(见下文)来玩。我最初认为,您使用的MySQL查询可能不会对特定值进行过滤,也不会对权利进行分页,但过滤权限正确。无论如何,小提琴中还有一些评论可以解释我所做的一些想法和假设。请随时要求编辑/更改/等。
Fwiw,分页和复杂的SQL查询都是非常重要的。它们也可能很昂贵,虽然我不了解您当前的代码平台/设置/环境,但我建议尽可能将逻辑和分页等内容推送到前端或JavaScript。 PHP也有用,但我大多试图指出,根据我的经验,SQL查询的复杂性通常比代码增加得更快,因为它是一种不同类型的框架集,即关系框架和程序框架。
您的代码还包含视图和业务逻辑以及SQL,因此要与您一起开放我可能找不到问题,或者可能存在多个问题。但我确实注意到了一些看你代码的事情。
(1)您的联系人列表查询的分页结果不是按页码排除 - 这意味着在某些情况下,结果中会包含前一个结果中的一行。
(2)如果WHERE条件值的每个空值都有奇怪的行为 - 特别是$ dname,$ dcity,$ dlocation。我没有设法从SQL小提琴中的SQL中重现你的确切问题,但是我想知道你是否会在列表查询中看到相同的问题。
修复建议:
修改SQL查询以使用排名和内部查询来对结果进行独占分页并消除空匹配问题。在我下面和小提琴中建议的查询中,我已经将 a AND(b或c)AND d 中的WHERE条件调整为 a AND((b或c)或d ) - 虽然这看起来很奇怪,但它表示&#34;确保我们在非空位置(假定的主要选择器)上匹配,或者我们有一个名称或城市来过滤&# 34 ;.对于LIKE条件检查,至少有一个变量必须匹配true,否则它不会为它返回结果行,而另一种方式需要一行的所有三个匹配(如果这些值总是非空的,那么这很好) )。
SET @name = "";
SET @city = "";
SET @location = "WA";
SET @per_page = 1;
-- Setting this arbitrarily - looking at different pages
-- mostly verifying yeah, the fetch/ pagination part works.
SET @page = 3;
SET @rownum = 0;
-- Basic Fetch Query used for getting list-for-page
SELECT
results.id, results.name,
results.location, results.city,
results.rank
FROM (
SELECT
c.id AS id
,c.name AS name
,c.location AS location
,c.city AS city
,@rownum := @rownum + 1 AS rank
FROM contacts AS c
-- ,(SELECT @rownum := 0) r
WHERE c.id > 0
AND ( (c.name LIKE @name OR c.city LIKE @city)
OR c.location LIKE @location )
ORDER BY c.id ASC
) AS results
WHERE rank BETWEEN ( (@page-1) * @per_page + 1) AND ( (@page) * @per_page )
<强>澄清强>
这个更大的查询中有2件新事物正在发生。一,有一种称为&#34;内部查询&#34; 用于预先选择一组行。其次,我指定了一个排名值,用于我的最终结果查询,以将表格行组织成&#34; paged&#34;结果。这是尝试使用
的解决方法WHERE ...
LIMIT ((@page-1)*@per_page), @per_page
甚至
WHERE ...
LIMIT ((@page-1)*@per_page) OFFSET @per_page
但是(参见我的CREDITS部分),显然一个人不能在SQL中使用@vars。您也可以使用PHP字符串模板或其他东西在代码中解决这个问题,就像您已经在将变量注入字符串一样。
请完全随时向我提供反馈,我会看看能否帮助您找出问题所在。哦,这就是SQL Fiddle,看看我做了什么假设以及我在做什么。制作或要求任何/所有更正!!谢谢Vikram:
SQL小提琴链接
http://sqlfiddle.com/#!9/d516e/26
<强> CREDITS 强>
显然,人们不能在LIMIT子句中使用像@name这样的用户变量,这是我以前不知道的。我找到了另一个Stack帖子来帮助我解决这个问题: Variable LIMIT Clause in MySQL