我一直在这里和那里做一些代码,这些代码基本上从mysql中提取数据并通过php显示。结果在页面上一次显示 3 个,我从来没有遇到任何问题。
但是,我最近在数据库项目中添加了一个字段:“已售罄”和“有货”,并且正在尝试(悲惨地)进行两次搜索-首先我想显示库存中的项目,然后是那些已售罄的项目. 不幸的是,我多年来使用的分页代码不喜欢我运行两个 php 查询,而只是在页面中添加了 3 个额外的项目(如果适用)。
完整代码是:
<form name="form1" method="get" action="products.php">
<?php
if(!empty($msg)) {
echo $msg[0];
}
?>
<input name="q" class="textInput2" input type="search" id="q" placeholder="search image name..." autosave="applestyle_srch" results="5" onKeyUp="applesearch.onChange('srch_fld','srch_clear')" />
<input name="doSearch" type="hidden" id="doSearch2" value="Search">
<?php if ($get['doSearch'] == 'Search') {
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM products";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
if($get['q'] == '') {
$sql = "SELECT * FROM products WHERE status='sold' ORDER BY `price` ASC LIMIT $offset, $rowsperpage";
}
else {
$sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage";
}
$result1 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
if($get['q'] == '') {
$sql = "SELECT * FROM products WHERE status='in stock' ORDER BY `price` ASC LIMIT $offset, $rowsperpage";
}
else {
$sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage";
}
$result2 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
?></form></div>
<div id="pagination" style="float:right; display:inline; margin-right:10px;">
page:<?php
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$prevpage'><</a> ";
} // end if
// range of num links to show
$range = 3;
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>
</div>
</div>
</div>
</div></p>
<div class="category-products">
<form name "searchform" action="products.php" method="post">
<?php while($rrows = mysql_fetch_array($result2))
{
echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">';
echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /></a></div>';
echo '<div id="searchdetails">
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
<td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>';
echo '</td>
</tr>
<tr>
<td>' . $rrows['desc'] . '';
echo '</td>
</tr>
<tr>
<td><h3 class="price">£' . $rrows['price'] . ' ' . $rrows['pandp'] . '</h3>'; echo '</td>
</tr>
</table></div>';
echo '</div>';
}
?>
<?php while($rrows = mysql_fetch_array($result1))
{
echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">';
echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /><img src="soldout.png" id="soldout" /></a></div>';
echo '<div id="searchdetails">
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
<td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>';
echo '</td>
</tr>
<tr>
<td>' . $rrows['desc'] . '';
echo '</td>
</tr>
<tr>
<td><h3 class="price">£' . $rrows['price'] . ' ' . $rrows['pandp'] . '</h3>'; echo '</td>
</tr>
</table></div>';
echo '</div>';
}
?>
</form> <?php } ?>
我完全可以理解代码可以更简洁——(我多年来一直没有修改分页过程,因为它一直有效)——但我假设必须有一种更简单的方法来显示表格中的所有项目而不必运行 $result1 和 $result2 查询。
非常感谢任何帮助!
京东