简体   繁体   中英

How can I search (In DB row)

I'm trying to find out how to search in a db row, but I have no IDEA... Maybe some of you guys can help out. I'm making a magic the gathering collection website for my boss. In this game you have multiple sets and each set had a card. Theres a page in which you can see all sets, and in this page I want a search bar to find the sets. Want I want is a search bar at the top of the page and when you typ in "Strong" you get to see every set name which contains "Strong".

Output : http://prntscr.com/5ubsa2 List continues quite a bit...

As you can see in the output, all the sets are listed. I want that you can see the search results there.

Input :

<body>

<?php
    // Get data
    $query = 'select * from sets ORDER BY releaseDate;';
    $sets = $conn->query($query);

    // Generate HTML
    $htmlSets = '';
    foreach($sets as $row){
        $htmlSets .= '<div class="col-md-4 magic-set"><a href="'.$baseURL.'set.php?id='.$row['id'].'"><h5>'.$row['name'].'</h5></a></div>';
    }
    //set.php?fname=areg&lname=aerga

?>

<!-- Page Content -->
<div class="container">
    <?php include_once 'inc/navigation.php';?>
    <div class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="text-center">
                    <h3>Magic: the Gathering Sets</h3>
                    <hr>
                </div>
            </div>
        </div>

        <div class="row">
            <?= $htmlSets ?>
        </div>
        <!-- /.row -->
    </div>

    <?php include_once 'inc/footer.php';?>

</div>
<!-- /.container -->
</body>

It looks like you haven't started your MySQL connection, nor have you exited it.

<?php

    define("DB_SERVER", "localhost");
    define("DB_USER", "username");
    define("DB_PASS", "password");
    define("DB_NAME", "database_name");

    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

    // If the connection didn't succeed, run this error:

    if (mysqli_connect_errno()) {
        die("Database connection failed: " .
            mysqli_connect_error() .
            " (" . mysqli_connect_errno() . ")"
        );
    }
?>

This connection should be at the very start of your document. You also have to have mysqli_close($connection); at the end of your document in order to close your connection.

You also have a semicolon at the end of your query, so be sure to delete that. This should be the correct formating of the query:

$query = 'SELECT * FROM sets ORDER BY releaseDate';

You cannot use foreach loops for MySQL queries, you can only use while loops (unless you have auto-increment turned off for your primary id). Here is what your code will look like as a while loop:

<?php

    // You can also use mysqli_fetch_row()
    while ($row = mysqli_fetch_assoc($sets)) {
        $htmlSets .= '<div class="col-md-4 magic-set"><a href="'.$baseURL.'set.php?id='.$row['id'].'"><h5>'.$row['name'].'</h5></a></div>';
    }

    // Since your query is of a 'read' type and
    // not an 'update', you are required the free the result:

    mysqli_free_result($sets) ;

?>

Be sure to free your result, in case you need to use it again later.

It is very simple bro actually you have to check that your saerch form is get or post you have always check if user search anything or it is empty for example it is get form and your query argument is "q" in php you will only need to do this

$query = "select * from sets ORDER BY releaseDate";
if( if(!empty($_GET['q'])))
{
    $query = "select * from sets WHERE name LIKE '%" .$_GET['q']. "%'    ORDER BY releaseDate";
}

this is just basic concept...

You can do this with a bit of AJAX. Here's a sample. Just replace with your values and CSS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM