douyuan5600 2019-07-16 19:52
浏览 53
已采纳

使用$ _POST回显大数组中特定子数组的代码片段

i have a big array with a lot of subarrays. Each subarray has a few values. Every subarray is an item and values are parameters of that item. I have a searchbar with a POST method. One of the values of the item is name of the item. So if the client is looking for item_one, I need to echo some piece of code with parameters of item_one.

My array looks like this:

$database = [
    [
        'name'=> 'item_one',
        'img_src'=> 'pictures/item_one.jpg',
        'preview_href'=> 'item_site.php?id='.item_preview($database).'',
        'description'=> 'This product is.....' ,
    ],
    [
        'name'=> 'item_two',
        'img_src'=> 'pictures/item_two.jpg',
        'preview_href'=> 'item_site.php?id='.item_preview($database).'',
        'description'=> 'This product is.....' ,
    ],
    // ...
];

and this piece of code i need to use for specific subarray:

echo '<div class="item">
      <a href="' . $item['preview_href'] . '" title="' . $item['name'] . '">
          <img src="' . $item['img_src'] . '">
          <div class="item_description"> ' 
             . $item['name'] . ' (' 
             . $item['release']
             . ') </div>
      </a>
</div>'

I have tried loads of different things, for example:

$post = $_POST['search'];

foreach ($database as $item) {
    if ($item['name'] == $post) {
        echo '<div class="item">
            <a href="' . $item['preview_href'] . '" title="' . $item['name'] . '">
                <img src="' . $item['img_src'] . '">
                <div class="item_description"> ' . $item['name'] . ' (' . $item['release'] . ') </div>
            </a>
        </div>'
    }
}

I have been trying for a few days so I will be happy for any advice. Thanks

  • 写回答

2条回答 默认 最新

  • douming4359 2019-07-16 20:19
    关注

    Your code is not very forgiving. If the user does not type the name exactly as you have it in your database, you will not get a match.

    I would try a couple of things.

    if you need exact match:

    if( strtoupper($post) === strtoupper($item['name']))
        { // item found}
    

    or, if you want to search for substring, try

    if( stripos($item['name'], $post) !== false)
        { //item was found }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题