根据 PHP 中的搜索条件加载图像

我试图弄清楚用户按下搜索按钮后如何从本地文件夹加载某个图像。我目前的代码是:


<!-- [SEARCH FORM] -->

<form method="post">

  <h1>

    SEARCH FOR USERS

  </h1>

  <input type="text" name="search" required/>

  <input type="submit" value="Search"/>

</form>


<!-- [SEARCH RESULTS] -->


<?php

if (isset($_POST['search'])) {

  if (count($results) > 0) {

    foreach ($results as $r) {

        echo("<table  align='center' width='1800'>");

        echo("<tr>");

        echo("<td width='600' align='left'>$r[name]</td>");

        echo("<td width='600' align='center'>$r[item_type]</td>");

        echo("<td width='600' align='center'>$r[oldname]</td>");

        echo("</tr>");

        echo("</table>");

    }

  } else {

    echo "No results found";

  }

}

?>

这部分有效。它从 mysql 数据库中检索所有结果。我接下来要做的是从本地文件夹中为搜索查询中的每个项目检索图像。图像的名称等于搜索查询结果 ($r[name])。


我的目标是这样的:

http://img2.mukewang.com/6353ad7d00012cd008880091.jpg

首先是图像-> 然后是名称-> 然后是其他东西等等……我尝试了一些东西,对 php 来说是新手,我还没有弄清楚。任何帮助,将不胜感激。



收到一只叮咚
浏览 157回答 1
1回答

倚天杖

我对你的代码做了一些小改动,希望对你有帮助<!-- [SEARCH FORM] --><form method="post">&nbsp; <h1>&nbsp; &nbsp; SEARCH FOR USERS&nbsp; </h1>&nbsp; <input type="text" name="search" required/>&nbsp; <input type="submit" value="Search"/></form><!-- [SEARCH RESULTS] --><?phpif (isset($_POST['search'])) {&nbsp; $stmt = $pdo->prepare("SELECT * FROM etcitem WHERE name LIKE ?");&nbsp;&nbsp; $stmt->execute(["%" . $_POST['search'] . "%"]);&nbsp;&nbsp; $results = $stmt->fetchAll();&nbsp;&nbsp; if (count($results) > 0) {&nbsp; &nbsp; echo("<table&nbsp; align='center' width='1800'>");&nbsp; &nbsp; foreach ($results as $r) {&nbsp; &nbsp; &nbsp; &nbsp; echo("<tr>");&nbsp; &nbsp; &nbsp; &nbsp; echo("<td width='600' align='left'><img src='/Image/$r[name]'></td>");&nbsp; &nbsp; &nbsp; &nbsp; echo("<td width='600' align='center'>$r[item_type]</td>");&nbsp; &nbsp; &nbsp; &nbsp; echo("<td width='600' align='center'>$r[oldname]</td>");&nbsp; &nbsp; &nbsp; &nbsp; echo("</tr>");&nbsp; &nbsp; }&nbsp; &nbsp; echo("</table>");&nbsp; } else {&nbsp; &nbsp; echo "No results found";&nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP