【问题标题】:Needle in a Haystack for Php [closed]Php 的大海捞针[关闭]
【发布时间】:2016-07-30 07:23:49
【问题描述】:

我需要使用一维数组在 haystack 程序中制作针,这样当用户输入他们的输入时,脚本会搜索表格和数组,直到找到针并显示其位置。我是编程新手,对如何达到这一点感到困惑。

<?php
 $haystack = array('cs85','cs60','cs61', 'cs80', 'cs81');
 $js = $_REQUEST['js'];
 $php = $_REQUEST['php'];
 $net = $_REQUEST['net'];
 $int = $_REQUEST['int'];
 $data = $_REQUEST['data'];

if(!array_search($needle, $haystack)) {
    echo $needle."Needle Not Found";
}else{
    echo $needle. "Needle Found!";
}

?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8"/>
<title>Find needle in haystack</title>
<style type = "text/css">

</style>
</head>
<body>
<center>
<h1>Find the Needle in the Haystack</h1>
<table>
    <h1>Haystack</h1>
<tr class = 'php'><td>php</td></tr>
<tr class = 'js'><td>javascript</td></tr>
<tr class = 'int'><td>intermediate</td></tr>
<tr class = 'net'><td>Network</td></tr>
<tr class = 'data'><td>Data</td></tr>
</table>
   <form method = 'get'>
       <label>Needle:
          <input type = "textbox" name = "needle" id = "needle" value = "break">
        </label>
        <input type = "submit" value = "submit">
    </form>
  </body>
</html>

【问题讨论】:

  • 你可以从定义$needle开始。

标签: php html arrays html-table


【解决方案1】:

PHP 的官方documentation 是一个很好的参考,在发布这样的问题之前请看一下!

您可能需要在代码中更改以下内容:

// Set a name attribute for the submit button
<input type = "submit" value = "submit" name="submit">


<?php
    if(isset($_POST['submit'])) {
      // setup variables
      $haystack = array('cs85','cs60','cs61', 'cs80', 'cs81');
      $needle = strip_tags($_POST['needle']);

      if(!array_search($needle, $haystack)) {
        echo $needle. " Needle Not Found";
      } else {
        echo $needle. " Needle Found!";
      }
    }
?>

W3 Schools 有一个关于类似搜索的好例子。

Check this out了解更多详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2019-05-19
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多