如何使用最新搜索的搜索框生成结果?

时间:2016-06-27 02:17:53

标签: php mysql

我愿意在php中创建搜索框, 当我输入以mysql术语存储的数据时,在搜索框中 那么上面存储的数据是唯一的,不一样,长度为1000个字符。

可能基于独特的IP或最近的搜索..

如何创建20个搜索到永恒或20个最想搜索的列表?

如何设置结果搜索框? 是否有教程或想要提供文件?

- 更新 -

PHP

<?php
/*-----------------------
First part: db connection
-------------------------*/
$dbhost = "localhost";
$dbname = "aa";
$dbuser = "root";
$dbpass = "";
$db=mysql_connect($dbhost, $dbuser, $dbpass);
if ($db==FALSE)
die("Error while connecting to MYSQL ".mysql_error());
mysql_select_db($dbname ,$db);
/*------------------------------
Read and save the search query
-------------------------------*/
$querystat = mysql_real_escape_string($_GET['q']);
$datetime = time();

if( ($_SERVER['HTTP_REFERER'] == '') AND ( ($querystat != $_SESSION['prev_search']) OR ( ($datetime - $_SESSION['datetime']) > 60) ) ) {
$insertquery = "INSERT INTO `query` ( `query` , `datetime`) VALUES ( '$querystat' , '$datetime');";
mysql_query($insertquery, $db);
}

$_SESSION['datetime'] = $datetime;
$_SESSION['prev_search'] = $querystat;
?>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  Form:<br>
  <input type="text" name="q" value=""><br><br>
  <input type="submit" value="Submit">
</form>

这里是sql

CREATE TABLE `query` (
  `id` int(11) NOT NULL,
  `query` varchar(255) NOT NULL DEFAULT '',
  `datetime` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

请你的帮助.. 对不起,我的英文不好

1 个答案:

答案 0 :(得分:1)

你要找的是分页。从数据库中获取数据后,您可以为所有数据创建分页,在您的情况下,每页菜单搜索20次。
This是有关数据提取分页的教程之一。 来自SO的This页面也提供了一个简单的分页解决方案 引自:(Simple PHP Pagination script),解释都在评论中。

try {

    // Find out how many items are in the table
    $total = $dbh->query('
        SELECT
            COUNT(*)
        FROM
            table
    ')->fetchColumn();

    // How many items to list per page
    $limit = 20;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Display the paging information
    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

    // Prepare the paged query
    $stmt = $dbh->prepare('
        SELECT
            *
        FROM
            table
        ORDER BY
            name
        LIMIT
            :limit
        OFFSET
            :offset
    ');

    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();

    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        // Display the results
        foreach ($iterator as $row) {
            echo '<p>', $row['name'], '</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}