新来的。理想情况下,我的if语句应该加载,以便它要求用户输入一个词来搜索文本文件,看看是否有匹配,如果有匹配,它将返回匹配(我仍然需要计算出来)。
当前包含在dictionary_search.php最后的else语句中的指令不显示(#line #37-#44/else语句)。我在整个if语句周围放置了一个isset(我也尝试了一个"空语句"),因为我有一个未定义的索引通知,我认为这是由于用户还没有输入提交的搜索。如果我删除'islet',我得到一个错误,读为' Undefined index: dTerm in dictionary_search.php on line 15'。
我已经检查了我的代码,并查看了括号等,但我无法确定故障的原因。我希望页面最初加载'输入您想要在下面搜索的术语....的显示。
我正在测试我的本地服务器(MAMP)
下面是我的IDE (BBedit v10)中实际粘贴的代码A010 | | 216
<h3>Dictionary</h3>
<?php //make sure the user submitted
if(isset($_REQUEST['submit'])) {
$dTerm = $_POST['dTerm'];//Term = name of dictionary entry
//make sure a non-empty request was entered
if(!empty($dTerm)) {
//check if term is in list
if(chekListing($dTerm)) {
echo '
Definition of the term $dTerm:
$dTerm <!--how to display definition?-->
<br /><br />
<a href="dictionary_search.php">Search Again?</a>
';
$search = true;
} else {
echo '
<p>No matching results found</p>
<br /><br />
<a href="dictionary_search.php">Search Again?</a>
';
}
}else {
//Our beginning default state
echo '
<p>Entry the term you would like to search for below.</p>
<form action="dictionary_search.php" method="post">
<input type="text" name="dTerm"/>
<br />
<input type ="submit" name = "submit" value="Search Current Entries"/>
</form>
<br /><br />
';
}
}
?>
<a href="dictionary_listings.php">Listings</a>
<a href="dictionary_contribute.php">Contribute</a>
</body>
</html>
配置文件:
$title = "Dictionary";
define('FILE_NAME', 'dictionary.txt');//holds the file name
//Compares data entered against dictionary.txt file
function chekListing($dTerm) { //Term = name of dictionary entry
$fileName = "dictionary.txt";
$file = fopen($fileName, "r");
//loop through the file and compare to username
while(!feof($file)) {
$dItem = fgets($file);
$dItem = trim($dItem);//trim whitespace
$dTerm = trim($dTerm);
//converts both strings to lower case then compares
if(strtolower($dTerm) == strtolower($dItem)) {
fclose($file);
return true; }
}
fclose($file);
return false;
}
如果我没弄错的话,你在一堆If语句的末尾有一个额外的}(amount of{不等于amount of}),所以你需要在链接结束前删除}。
您应该对此了解一点'echo'函数。你在echo (echo 'hello luke')后面的引号之间输入的所有内容都将被转换为纯html。所以,你告诉你的脚本实际写下你在两个引号之间输入的内容。
如果你想在你的echo中添加一个变量,你必须打破你的echo字符串,添加变量,并可能不得不打开它(在你的情况下你这样做),然后写你的字符串的其余部分。像这样:
echo ' Definition of the term $dTerm: <br /> '. $dTerm .' <br /><br /> <a href="dictionary_search.php">Search Again?</a> ';
这样你的问题就解决了。:)
第一件事。你的表单正在使用post方法:
method="post"
所以你必须做a:
if(isset($_POST['submit'])) {
记住字符串格式是一个对象,而不是一个简单的变量。
它包含一个char数组。它不是完全空的。但仍然是空的。
if(!empty($dTerm)) {
echo "empty<br/>";
} else {
echo "not empty<br/>";
}
if($dTerm=="") {
echo "empty<br/>";
} else {
echo "not empty<br/>";
}
if($dTerm==null) {
echo "empty<br/>";
} else {
echo "not empty<br/>";
}
显示
not empty
empty
empty
用另一种方式检查。如给定的示例;)
要在-> '中显示变量,必须在外部使用点连接它。
echo 'tekst'.$variable.' some "more" tekst'
使用-> "你必须在特殊字符前使用反斜杠,因为代码会中断,但你也可以像这样显示变量
echo "tekst $variable some '"more'" tekst";
或
echo "tekst {$variable} some '"more'" tekst";
第二种方法更安全。
您多次使用相同的测试,因此您可以使代码更简单。
if(chekListing($dTerm)) {
echo 'Definition of the term '.$dTerm.':';
$search = true;
} else {
echo '
<p>No matching results found</p>
';
}
echo '<br /><br />
<a href="dictionary_search.php">Search Again?</a> ';