-1

I'm willing to make the search box in php, in the search box when I input data stored in mysql terms then the stored data above is unique there is not the same and has a length of 1000 characters.

maybe based on unique ip or most recent searches..

How to create a list of 20 searches to eternity or 20 most wanted search?

How do I make result search box like that? is there a tutorial or want to give the file?


-- updated --

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>

here 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;

Please your help.. Sorry my bad english

sikuda
  • 15
  • 5

1 Answers1

1

What you are looking for is pagination. After fetching your data from your database, you can create pagination for all your data to, in your case, 20 searches per page menu.
This is one of the tutorials on pagination for data fetching.
This page from SO also provides a simple solution to pagination.
Quoted from: (Simple PHP Pagination script), the explanations are all in the comments.

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>';
}
Community
  • 1
  • 1
DunnoHowToCode
  • 531
  • 1
  • 4
  • 14
  • @MichaelGaskill I have edited this post to include the codes. I am new here so please give me comments on whether such edits are acceptable – DunnoHowToCode Jun 27 '16 at 05:28
  • Your answer had been automatically flagged as a "low quality" answer. There are numerous reviewers that will respond to that flag, and confirm whether it is (or not) a "low quality" answer. Now that you've updated the answer with answer with real content, your answer will be seen as a "high quality". You might be interested in reading about how to make sure that you give high quality answers at the [Help Center](/help) and on the [Meta Stack Exchange](http://meta.stackexchange.com) and [Meta Stack Overflow](http://meta.stackoverflow.com). – Michael Gaskill Jun 27 '16 at 05:35
  • @MichaelGaskill Thanks for the advice, I will take note next time. – DunnoHowToCode Jun 27 '16 at 05:39
  • Hey, no worries. It's a great thing that you're trying to share your knowledge and provide good answers. This site happens to work differently than most programming forums, so new users tend to have a steeper learning curve. If you pay attention to what others are doing, and do some reading on the links that I sent you, you'll do just fine here. Good luck and enjoy yourself. :D – Michael Gaskill Jun 27 '16 at 05:43
  • @DunnoHowToCode sorry late reply, but i try is search box result not pagination, please your help – sikuda Jun 28 '16 at 03:09