我想使用生成的链接列表来优化搜索。
我有一个产品数据库(面板仪表),每行都有一个唯一的型号和有关该型号的信息。我创建了一个搜索,允许用户搜索型号和输入类型(这些是可以接收标准4-20mA或自定义范围等信号的过程仪表)。除了搜索之外,脚本还会创建3个唯一的链接数组,我希望可以点击这些链接以缩小搜索结果。 到目前为止,这样做会带来这些php的片段创建链接列表:
//create a series list
$uniqueseries[] = "<a href='#' onclick='document.searchform.submit()'>{$row['series']}</a>";
//create a inputs list
$uniqueinputs[] = "<a href='#' onclick='document.searchform.submit()'>{$row['sio']}</a>";
//create a series list
$uniquepowers[] = "<a href='#' onclick='document.searchform.submit()'>{$row['pso']}</a>";
和
//create array of unique series types
$uniqueseries = array_unique($uniqueseries);
//create array of unique input types
$uniqueinputs = array_unique($uniqueinputs);
//create array of unique power types
$uniquepowers = array_unique($uniquepowers);
哪些位于“后端”脚本中(稍稍如下)。
对于每组链接,它首先创建一个非唯一链接数组(一个用于MySQL最初返回的结果)然后我使用array_unique将数组减少到只有唯一链接。然后在我的搜索页面上,我使用类似“,$ uniqueseries”的内容;?&gt;来打印出链接数组。
我想要发生的是当用户点击链接时,通过重新提交原始MySQL查询并附加精炼查询来缩小搜索结果。这种行为的一个示例出现在亚马逊搜索中,用户可以单击左侧的链接,该链接将保持相同的搜索范围,但将其缩小到某个部门,甚至是该产品特有的选项(即搜索时的ram数量)搜索相机时计算机或像素数。)
在我的情况下,如果我的用户通过搜索型号和输入类型(默认的两个键)开始搜索,比如使用术语“4到20”,我的用户将返回超过3500个结果(我们有一个非常大的产品目录。)在左边将是我想要优化结果的唯一链接列表。因此,如果用户在“电源”选项下看到“带电池备份数据的120 V AC”,我想点击该链接返回所有具有4到20 mA输入的电表(在生成的输入选项下可以看到多种变化)但是只有当他们将“带电池备份数据的120 V AC”作为“电源”时才会使用。
我觉得自己走在了正确的轨道上,但在PHP方面,我仍然是一个新手。
为了提供帮助,这里是我用于搜索“后端”的代码:
<?php
//creates DB connection
$dbHost = 'host';
$dbUser = 'user';
$dbPass = 'pass';
$dbDatabase = 'db';
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());
mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
$time_start = microtime(true);
//create arrays for results and errors
$error = array();
$results = array();
//creates arrays holding values for narrowing results
$uniqueseries = array();
$uniqueinputs = array();
$uniquepowers = array();
$uniquedigsize = array();
$uniquedignum = array ();
$uniquedigcol = array();
$uniquedistype = array();
//reset some variables if needed
isset($_GET['page'])?"":$_GET['page'] = 1;
isset($resultcount)?"":$resultcount = 0;
if (isset($_GET['search'])) {
//removes HTML/JS
$searchString = trim($_GET['search']);
$searchStripped = strip_tags($searchString);
//prevents SQL injections
$searchSan = mysql_real_escape_string($searchStripped);
if (count($error) <1) { //search continues if no errors exist
$searchSQL = "SELECT series, model, img, pso, sio, dig_size, dig_num, dig_col FROM meters WHERE levenshtein('%{$searchSan}%',model) < 4 OR levenshtein('%{$searchSan}%',sio) < 4 ORDER BY model";
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error with MySQL. " . mysql_error() . "<br> Query was {$searchSQL}.");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term '{$searchString}' yielded no results.";
} else {
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "<tr><td align='center'>{$i}</td> <td align='center'><a href='/catalog/{$row['series']}.pdf'>{$row['series']}</a></td> <td align='center'><img src='/img/productimg/small/{$row['img']}'></td> <td align='center'>{$row['model']}</td> <td> {$row['sio']}</td> <td> {$row['pso']}</td> <td align='center'>{$row['dig_num']}</td> <td align='center'>{$row['dig_size']}\"</td> <td align='center'>{$row['dig_col']}</td></tr>";
//create a series list
$uniqueseries[] = "<a href='#' onclick='document.searchform.submit()'>{$row['series']}</a>";
//create a inputs list
$uniqueinputs[] = "<a href='#' onclick='document.searchform.submit()'>{$row['sio']}</a>";
//create a series list
$uniquepowers[] = "<a href='#' onclick='document.searchform.submit()'>{$row['pso']}</a>";
$i++;
}
}
//create array of unique series types
$uniqueseries = array_unique($uniqueseries);
//create array of unique input types
$uniqueinputs = array_unique($uniqueinputs);
//create array of unique power types
$uniquepowers = array_unique($uniquepowers);
//results paginator
$resultcount = count($results); //number of results
$perpage = 5; //number of results per page
$totalpages = ceil($resultcount / $perpage); //calculates total number of pages
$startresult = (($_GET['page'] - 1) * $perpage); //calculates the first result number for page
$brokenresults = array_slice($results, $startresult, $perpage); //slices array into groups set by $perpage
$nextpage = $_GET['page'] + 1; //calculate next page number
$lastpage = $_GET['page'] - 1; //calculates last page number
}
}
function removeEmpty($var) {
return (!empty($var));
}
$execution_time = (microtime (true) - $time_start); //calculates script processing time
?>
这是我用于“前端”的代码:
<body>
<!-- Google Analytics Script -->
<?php include_once("scripts/analyticstracking.php") ?>
<div class="wrapper"> <!-- Sticky Footer Wrapper -->
<div id="panorama"></div>
<div id="header">
<?php include("include/header/banner.php") ?>
<?php include("include/header/nav.php") ?>
<?php include("include/header/quicksearch.php") ?>
</div>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchform" id="searchform">
<div id="content">
<?php include("scripts/searchbackend.php") ?>
<?php include("scripts/searchform.php") ?>
<?php include("scripts/searchoptions.php") ?>
<div id="searchresults">
<center>
<?php
echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":"";
echo ($resultcount > 0)?"<table id='searchtable' ><thead align='center'><tr><th width='50px'></th><th width='50px'>Series</th><th width='50px'>Image</th><th width='85px'>Model</th><th width='50px'>Input</th><th width='50px'>Power Supply</th><th width='50px'># of Digits</th><th width='50px'>Digit Size</th><th width='50px'>Display Color</th></tr></thead><tbody>" . implode("", $brokenresults) . "</tbody></table>":""; //returns results
?>
<div id="resultsfooter">
<?php
if (count($results) > 5) {
echo ($_GET['page'] > 1)?"<br><a href='/search.php?search={$_GET['search']}&page={$lastpage}'>Previous</a>" . " Page {$_GET['page']}/{$totalpages} " . "<a href='/search.php?search={$_GET['search']}&page={$nextpage}'>Next</a>":"<br>Page {$_GET['page']}/{$totalpages} " . "<a href='/search.php?search={$_GET['search']}&page={$nextpage}'>Next</a>";
}
echo ($resultcount > 0)?" | Search found {$resultcount} results in {$execution_time} seconds.":""; //result count and time
?>
</div>
</center>
</div>
</div>
</form>
</div>
<div class="footer">
<?php include("include/footer/footer.php") ?>
</div>
</body>
同样,这是我的“searchoptions.php”的内容包括:
<div id="searchoptions">
<fieldset id="searchin" class="searchoptions">
<legend>Serach In</legend>
<input type="checkbox" class="checkbox" name="model" <?php echo isset($_GET['model'])?"checked":''; ?> /><label class='checklabel'>Model</label><br>
<input type="checkbox" class="checkbox" name="input" <?php echo isset($_GET['input'])?"checked":''; ?> /><label class='checklabel'>Input</label>
</fieldset>
<fieldset class="searchoptions" style="<?php if (count($uniqueseries) < 2) {echo "Display: none;";} ?>">
<legend>Series</legend>
<?php echo implode("<br>", $uniqueseries); ?>
</fieldset>
<fieldset class="searchoptions" style="<?php if (count($uniqueinputs) < 2) {echo "Display: none;";} ?>">
<legend>Input</legend>
<?php echo implode("<br>", $uniqueinputs); ?>
</fieldset>
<fieldset class="searchoptions" style="<?php if (count($uniquepowers) < 2) {echo "Display: none;";} ?>">
<legend>Power</legend>
<?php echo implode("<br>", $uniquepowers) ?>
</fieldset>
<input type="submit" class="" value="Update Search" style="" <?php if (!isset($types)) { if (isset($_GET['model'])) {"`model` LIKE '%{$searchSan}%'";} elseif (isset($_GET['input'])) {"`input` LIKE '%{$searchSan}%'";} } ?> />
</div>
我的测试网站上有最新的更改,您可以在此处访问: new.foxmeter.com 尝试搜索“FM”或“4到20”,就像我在我的示例中使用的那样。截至目前,“4-20”和“4 20”不会返回与“4到20”相同的结果,但我可以做到并且它还不在我的列表顶部。
我也希望之前的搜索能够持续存在。因此,搜索从“4到20”开始,然后通过“120 V AC with battery backed data”电源缩小到只有4到20个过程表,然后再次缩小将保持“4到20过程与120 V AC”电池备份数据“从之前的改进以及新的改进。
如果我在任何时候都不清楚,我会整天待在这里,所以如果你对我有所了解,不要害怕质疑或纠正我。在此先感谢任何人的帮助。我已经完成了新网站的大部分功能,除了这个搜索我遇到了一些麻烦,所以完成这意味着我的任务(几乎)完成。