1

我想使用生成的链接列表来优化搜索。

我有一个产品数据库(面板仪表),每一行都有一个唯一的型号和有关该型号的信息。我创建了一个搜索,允许用户搜索型号和输入类型(这些是可以接收标准 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); ?> 来打印出链接数组。

我想要发生的是,当用户单击链接时,它会通过重新提交原始 MySQL 查询并附加精炼查询来缩小搜索结果。这种行为的一个例子出现在亚马逊搜索中,用户可以单击左侧的链接,该链接将保留相同的搜索,但将其缩小到某个部门,甚至是该产品独有的选项(即搜索时的内存数量)搜索相机时的计算机或像素数。)

在我的情况下,如果我的用户通过搜索型号和输入类型(默认的两个键)来开始搜索,比如使用术语“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>&nbsp;{$row['sio']}</td>  <td>&nbsp;{$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>" . "&nbsp;&nbsp;Page {$_GET['page']}/{$totalpages}&nbsp;&nbsp;" . "<a href='/search.php?search={$_GET['search']}&page={$nextpage}'>Next</a>":"<br>Page {$_GET['page']}/{$totalpages}&nbsp;&nbsp;" . "<a href='/search.php?search={$_GET['search']}&page={$nextpage}'>Next</a>";
                            }

                            echo ($resultcount > 0)?"&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;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 to 20”相同的结果,但我可以做到,而且它还没有在我的列表顶部。

我还希望以前的搜索能够持续。因此搜索从“4 到 20”开始,然后缩小到只有 4 到 20 个过程仪表,使用“120 V AC 电池支持数据”电源,然后再次缩小范围将保持“4 到 20 个过程,120 V AC电池支持的数据”之前的改进以及新的改进。

如果我在任何时候不清楚,我会整天在这里,所以如果你对我有想法,请不要害怕质疑或纠正我。在此先感谢任何人的帮助。我已经完成了我们新网站的大部分功能,除了这个搜索我遇到了一些麻烦,所以完成这意味着我的任务(几乎)完成了。

4

0 回答 0