• Hello!
    I am working on implementing a search function to access information (an inventory) in a DB independent of WordPress. My plan is to have just plain text data (a few bits of information about any pieces of inventory that match the query) be returned in the search results instead of what currently appears in the search results (pages on the site). I have the script functioning locally on a plain old barebones HTML page, but now I’m just trying to figure out what to modify to implement it into WordPress. I’m using a modified version of the twentythirteen theme. Where should I be looking to modify the method by which the search function operates? Is all of the search functionality within search.php, and as such, would only modifying that be enough?

    If it’s at all helpful, here’s my code.

    index.php (just used for displaying the form):

    <!DOCTYPE html">
    <html>
    <body>
        <form action="search.php" method="GET">
            <input type="text" name="query" />
            <input type="submit" value="Search" />
        </form>
    </body>
    </html>

    search.php (with generic variables):

    <?php
        mysql_connect("location", "username", "pass") or die("Error connecting to database: ".mysql_error());
    
        mysql_select_db("databasename") or die(mysql_error());
    ?>
    
    <!DOCTYPE html>
    <html>
    <body>
    <?php
        $query = $_GET['query']; 
    
        $min_length = 3;
    
        if(strlen($query) >= $min_length){ 
    
            $query = htmlspecialchars($query); 
    
            $query = mysql_real_escape_string($query);
    
            $raw_results = mysql_query("SELECT * FROM tablename
                WHERE ('column1' LIKE '%".$query."%') OR ('column2' LIKE '%".$query."%')") or die(mysql_error());
    
            if(mysql_num_rows($raw_results) > 0){ 
    
                while($results = mysql_fetch_array($raw_results)){
    
                    echo $results['column1']. " ";
                    echo $results['column2']."<br>";
    
                }
    
            }
            else{
                echo "No results";
            }
    
        }
        else{
            echo "Minimum length is ".$min_length;
        }
    ?>
    </body>
    </html>

    I’m pretty new to PHP/MySQL, so any help you can provide at all is very much appreciated. Thanks a lot!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Implementing a custom search’ is closed to new replies.