1

I currently have a HTML search form which takes the users input (for example 123456) and using PHP searches a database to see if that number exists as an item number. It then returns information on that item in a table.

Is it possible to search for multiple items at once for example 123456, 654321, 000000 and have the results for each displayed in a table ? I currently have not been able to find any documentation on how I could achieve this. Any help would be greatly appreciated.

My current code which searches and brings back the data for one item is.

<div id="div1">
            <!-- [SEARCH FORM] -->
        <form method="post" action="nweb.php">
        <h1>Product Information</h1>
        <input type="text" name="search" required/>
        <input type="submit" value="Search"/>
        </form>
        <?php

         if (isset($_POST['search'])) {
         require "2-search.php";
         if (count($results) > 0) {
         foreach ($results as $r) {
           echo "<table>";  
       
      
            echo "<tr><td>Item number</td><td>" . $r['item_number'] . "</td></tr>";  
            echo "<tr><td>Stock available</td><td>" . $r['stock_available'] . "</td></tr>";
            echo "<tr><td>Available Stock</td><td>" . $r['available_stock'] . "</td></tr>";  
            echo "<tr><td>Detailed Description</td><td>" . $r['detailed_desc'] . "</td></tr>";   
            echo "<tr><td>Gender</td><td>" . $r['gender'] . "</td></tr>"; 
            echo "<tr><td>Group</td><td>" . $r['group'] . "</td></tr>"; 
            echo "<tr><td>Subgroup</td><td>" . $r['sub_group'] . "</td></tr>"; 
        }
            echo "</table>";  
      } else {
        echo "No results found";
      }
    }
    ?>
    </div>

My search code is.

try {
  $pdo = new PDO(
    "sqlsrv:Server=$server;Database=$database", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
   } catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
   }
$stmt = $pdo->prepare ("SELECT * FROM dbo.[data] WHERE [item_number] LIKE ? OR [stock_available] LIKE ?");
$stmt->execute(["%" . $_POST['search'] . "%", "%" . $_POST['search'] . "%"]);
$results = $stmt->fetchAll();
if (isset($_POST['ajax'])) { echo json_encode($results); }
?>
0

1 Answer 1

1

One simple way, without too many drastic changes in your code, would be to choose a separator (maybe a comma) and write your items like that, then, you'd separate these items into an array of search items:

$searchFor = explode(',', $_POST['search']);

And search for them one by one:

$resultsArray = [];

foreach ($searchFor as $searchItem){
    $stmt = $pdo->prepare ("SELECT * FROM dbo.[data] WHERE [item_number] LIKE ? OR [stock_available] LIKE ?");
    $stmt->execute(["%" .$searchItem . "%", "%" . $searchItem . "%"]);
    $results = $stmt->fetchAll();
    array_push($resultsArray, $results);
}

Finally, you'd echo the tables almost the same way you did until now:

foreach ($resultsArray as $results) {
...
    foreach ($results as $r) {
    ...
Sign up to request clarification or add additional context in comments.

5 Comments

This works. Thank you so much. So to understand this does it store the entries separated by a comma in an array and then iterate through each one ? and return the results ?
pretty much, yes, the results it returns during the iteration are stored in the resultsArray, which you can then use to echo your tables
with this. how would i show items it couldn't find or say for example the item number didnt exist in the table because currently if one item number is not in the table it just shows no results found even though the others exist
I'd check the $results count before doing the foreach ($results as $r). If this isn't enough, please create another question about that (with the updated code), as it's a new issue
okay thankyou again. i have posted another question stackoverflow.com/questions/63848367/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.