Reputation: 713
Newb here. Ideally, my if statement should load so that it asks the user to enter a term to search the text file to see if there is a match or not, if there is a match it will return the match (i still have to work that out).
Currently the instructions contained in the final else statement of dictionary_search.php does not show (#line #37-#44/else statement). I placed an isset around the overall if statement (i also experimented with an 'empty statement) as I had an undefined index notice which I believe was due to the user not having entered a submitted search yet. If i remove the 'islet', i get an error that reads as' Undefined index: dTerm in dictionary_search.php on line 15'.
I've gone through my code and looked at the bracketing, etc. and I've not been able to determine the reason for the malfunction. I want the page to initially load with the 'Enter the term you would like to search for below....' showing.
I am testing on my local server(MAMP)
Below is the actual pasted code from my 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>
Config File:
$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;
}
Upvotes: 0
Views: 90
Reputation: 39
First thing. You'r form is using post method:
method="post"
So you must do a:
if(isset($_POST['submit'])) {
Remember that a string format is an object not a simple variable.
It contains an array of "char".
Its not fully empty. But still counts as null.
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/>";
}
It will display
not empty
empty
empty
Check it in a diferent way. Like in the given example ;)
To display a variable in -> ' you must conkatinate it outside using dots.
echo 'tekst'.$variable.' some "more" tekst'
using -> " you must use a backslash in front of specjal chars becouse the code breaks but you can also display the variable like so
echo "tekst $variable some \"more\" tekst";
or
echo "tekst {$variable} some \"more\" tekst";
The secound is safer.
You are using the same tekst more than once, so u can make the code simplyer.
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> ';
Upvotes: 1
Reputation: 522
You should understand a little bit of the 'echo' function for this. Everything you type between your quotes after the echo ( echo 'hello luke' ) will be converted into plain html. So, you're saying to your script to actually write down what ever you type between the two quotes.
If you want to add an variable in your echo, you've to break your echo's string, add the variable and might have to open it ( in your case you do ) and write the rest of your string. Like so:
echo ' Definition of the term $dTerm: <br /> '. $dTerm .' <br /><br /> <a href="dictionary_search.php">Search Again?</a> ';
That would solve your problem for now. :)
Upvotes: 1
Reputation: 424
If I am not mistaken, you have an extra } at the end of your bunch of if statements (amount of { does not equal amount of }, so you need to remove the } just before your links at the end.
This should work.
Upvotes: 1