如何返回找到的项目的结果.但也有未找到的项目列表 [英] How can I return results for items found. but also have a list of items not found

查看:36
本文介绍了如何返回找到的项目的结果.但也有未找到的项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 HTML 搜索表单,它接受多个输入(例如:123456、654321、789456).我正在使用 PHP 搜索数据库,以查看这些数字是否作为项目编号存在.然后它在回显表中返回​​有关这些项目的信息.

唯一的问题是,如果数据库中不存在某个数字,即使其他两项存在,它也不会返回任何结果.

我怎样才能让它返回关于确实存在的项目的信息,然后列出它无法找到记录的项目?

我的表格和表格生成如下:

<!-- [搜索表单]--><form method="post";动作=nweb.php";id=测试表"><h1>产品信息</h1><!-- <输入类型=文本"名称=搜索"需要/>--><文本区域名称=搜索"cols=40"行=5"form="testform"></textarea><输入类型=提交"值=搜索"/></表单><?phpif (isset($_POST['search'])) {//搜索项目需要2-search.php";//显示结果如果(计数($结果)> 0){echo "number found ".count($resultsArray)."
";echo "";echo "";回声<tr>";回声第个项目号";回声第 th 个可用库存";echo第个可用库存";echo "";echo "";回声第<子组";回声</tr>";echo "</thead>";foreach ($resultsArray 作为 $results) {foreach ($results as $r) {echo "<tbody>";回声<tr>";回声<td>".$r['item_number'] ."</td>";回声<td>".$r['stock_available'] ."</td>";回声<td>".$r['available_stock'] ."</td>";回声<td>".$r['detailed_desc'] .</td>";回声<td>".$r['division'] .</td>";回声<td>".$r['性别'] .</td>";回声<td>".$r['group'] .</td>";回声<td>".$r['sub_group'] .</td>";回声</tr>";echo "</tbody>";}}echo "</table>";} 别的 {echo "未找到结果";}}?>

2-search.php中的搜索代码为:

$searchFor = expand(",", trim($_POST['search']));$resultsArray = [];foreach ($searchFor 作为 $searchItem){回声 $searchItem;$stmt = $pdo->prepare ("SELECT * FROM dbo.[data] WHERE [item_number] = ?");$stmt->execute(["".$searchItem.""]);$results = $stmt->fetchAll();回声 $searchItem;array_push($resultsArray, $results);}

如果有人可以提供帮助,将不胜感激.

解决方案

搜索词在您的 $searchFor 数组中,所以已经处理过了.现在我们只需要找到某种方式来显示这些而不是表格.让我们修改处理结果计数的代码.

尝试这样的事情:

foreach ($resultsArray as $key => $results) {如果(计数($结果)> 0){foreach ($results as $r) {echo "<tbody>";...echo "</tbody>";}} 别的 {echo "没有找到"的结果.$searchFor[$key];}}

您希望计数检查在您的 foreach($resultsArray as $results) 中.这样,您将检查每个 as $results 数组.$key 包含当前迭代的索引(第 1 次 = 0、第 2 次 = 1 等),因此您可以使用它来访问 searchedFor 数组,其中包含您展开的搜索词.

I currently have a HTML search form which takes multiple inputs (for example: 123456, 654321, 789456). I am using PHP to search a database, to see if the numbers exist as an item number. It then returns information on those items in an echoed table.

The only issue is that if a number does not exist within the database it will return no results found, even if the other two items exist.

How can i have it so that it returns the information on the items that do exist and then lists out the items, which it was not able to find records for?

My form and table generation are below:

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

<?php
    if (isset($_POST['search'])) {
      // SEARCH FOR ITEMS
      require "2-search.php";
      
      // DISPLAY RESULTS
      if (count($results) > 0) {
        echo "number found ".count($resultsArray)."<br>";

 echo "<table>";
        echo "<thead>";
        echo  "<tr>";
        echo    "<th>Item number</th>";
        echo    "<th>Stock available</th>";
        echo    "<th>Available Stock</th>";
        echo    "<th>Detailed Description</th>";
        echo    "<th>Division</th>";
        echo    "<th>Gender</th>";
        echo    "<th>Group</th>";
        echo    "<th>Subgroup</th>";
        echo  "</tr>";
        echo "</thead>";
      foreach ($resultsArray as $results) {
        
        foreach ($results as $r) {

            echo "<tbody>";
            echo  "<tr>";
            echo    "<td>". $r['item_number'] ."</td>";
            echo    "<td>". $r['stock_available'] ."</td>";
            echo    "<td>". $r['available_stock'] ."</td>";
            echo    "<td>" . $r['detailed_desc'] . "</td>";
            echo    "<td>" . $r['division'] . "</td>";
            echo    "<td>" . $r['gender'] . "</td>";
            echo    "<td>" . $r['group'] . "</td>";
            echo    "<td>" . $r['sub_group'] . "</td>";
            echo  "</tr>";
            echo "</tbody>";
   }
      }
            echo "</table>";  
      } else {
        echo "No results found";
      }
    }
    ?>
    </div>

The search code in 2-search.php is:

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

$resultsArray = [];

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

If anyone could help, it would be much appreciated.

解决方案

The search terms are within your $searchFor array, so that's already dealt with. Now we only need to find some way to display these instead of a table. Let's modify the code that deals with the results count.

Try something like this:

foreach ($resultsArray as $key => $results) {
    if (count($results) > 0) {
        foreach ($results as $r) {
            echo "<tbody>";
            
            ...

            echo "</tbody>";
        }
    } else {
        echo "No results found for ". $searchFor[$key];
    }
}

You want the count checking to be in your foreach($resultsArray as $results). That way you'll check each of the as $results arrays. The $key contains the index for the current iteration (1st = 0, 2nd = 1, etc.,) so you can use it to access the searchedFor array, which contains your exploded search terms.

这篇关于如何返回找到的项目的结果.但也有未找到的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆
Detailed Description";echo "<th>Gender</th>";回声