2016-04-08 9 views
-6

私はhaystackプログラムで1次元配列を使って針を作る必要があるので、ユーザーが入力を入力すると、針が見つかるまでスクリプトがテーブルと配列を検索してロケーション。私はプログラミングには新しく、その点に到達する方法については混乱しています。PHPのための乾草の針針

<?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> 
+5

あなたは '$ needle'を定義することから始めることができます。 – AbraCadaver

答えて

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詳細は

関連する問題