0

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>&nbsp; &nbsp;
                    ';
                    $search = true;
                    } else {
                    echo '
                        <p>No matching results found</p>
                        <br /><br />
                        <a href="dictionary_search.php">Search Again?</a>&nbsp; &nbsp;
                    ';
                        }
                }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>&nbsp; &nbsp;
        <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;
    }

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this suggestion, and that was one of the first things that I thought might be the issue. However if I remove what might appear to be an extra Right Bracket (}), the file returns an error of 'syntax error, unexpected end of file in dictionary_search.php on line 58
1

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>&nbsp; &nbsp; ';

That would solve your problem for now. :)

3 Comments

Oh Duh! I completely forgot about that, not that i learned about that all that long ago and I'm still processing it. Still, completely forgot about it. I will check this and I do apologize as I thought that I'd checked everything before coming to ask for help.
Even the best programmers under us are getting errors because of the things we learn at the very beginning. :)
It's still not working, but this is beyond me and i'm trying things so that when i go to class and we start to cover these sorts of things I will have some idea of the concepts we're exposed to.
1

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>&nbsp; &nbsp;';  

2 Comments

I tried this '<?php //make sure the user submitted if(isset($_POST['submit'])) { $dTerm = $_POST['dTerm'];//Term = name of dictionary entry //check if term is in list 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/>"; } } ?>' still it loads 'empty'
Also tried this 'if(isset($_POST['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.':'; $search = true; } else { echo ' <p>No matching results found</p> '; } }else{echo 'fudge';} }', still the initial problem persists that nothing displays on first load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.