Jump to content

Help with an If statement


The Praetorian

Recommended Posts

This is only my third attempt at editing a php script. The other two attempts were successful, but this one I can't figure out.I'm using this code to display search results. I'm trying to add another if statement so that if they enter text but it doesn't match any of the results, it will display a message. Here's the script.

while ($r = mysql_fetch_array($result)){  if ($r['word'] != "")  {	echo '<h2>'.$r['word'].'</h2>';	if ($r['category'] != "")	  echo '<span class="blue">Category</span> <b>:</b> '.$r['category'].'<br />';	if ($r['pronounce'] != "")	  echo '<span class="blue">Pronunciation</span> <b>:</b> '.$r['pronounce'].'<br />';	if ($r['definition'] != "")	  echo ''.$r['definition'].'<br />';	if ($r['definitiontwo'] != "")	  echo ''.$r['definitiontwo'].'<br />';	if ($r['related'] != "")	  echo '<span class="blue">Related Entries</span> <b>:</b> '.$r['related'].'<br />';	  echo '<br />';  }

This is the portion I tried to add. I added it as another if statement, within the While statement but after the first if statement. It's giving me a Syntax error ("was expecting ',' or ';' on line 100") on the echo line. Not really sure which part is wrong. Also not sure on the syntax of the if part where I insert the variable..

if ($r['word'] != ($search_string))  {	 echo '<p>There are no matches for the word you entered. Please make sure that the word is spelled correctly, and that you haven't left out any spaces or apostrophes. If your search still doesn't return a match, then we probably haven't added it yet.</p>';  }

Link to comment
Share on other sites

I guess I need more details about the code. For the result that the while loop is looping over, is that result a list of words that match only? Or are there more words in the result then only words that match? If the result only contains words that match, then you can use something like this to display a message if there were no matches:

if (!mysql_num_rows($result))  echo "no matches";while ($r = mysql_fetch_array($result)){  ...}

Link to comment
Share on other sites

Here's the whole code as it appears. The sql query pulls out exact matches and similar matches. I don't mind that. What I want it to do is when it matches nothing, so that the output would be just blank, it would show the message.Here's the script including the part I added that's not working. Again, still not sure on the syntax for that last if statement.

$dbh=mysql_connect ("localhost", "*****", "****") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("author_localhost");$search_string = $_GET['s'];$result = mysql_query ("SELECT * FROM dictionary WHERE word LIKE '%" . mysql_real_escape_string($search_string) . "%' ORDER BY word ASC") or die(mysql_error());while ($r = mysql_fetch_array($result)){  if ($r['word'] != "")  {	echo '<h2>'.$r['word'].'</h2>';	if ($r['category'] != "")	  echo '<span class="blue">Category</span> <b>:</b> '.$r['category'].'<br />';	if ($r['pronounce'] != "")	  echo '<span class="blue">Pronunciation</span> <b>:</b> '.$r['pronounce'].'<br />';	if ($r['definition'] != "")	  echo ''.$r['definition'].'<br />';	if ($r['definitiontwo'] != "")	  echo ''.$r['definitiontwo'].'<br />';	if ($r['related'] != "")	  echo '<span class="blue">Related Entries</span> <b>:</b> '.$r['related'].'<br />';	  echo '<br />';  if ($r['word'] != $search_string)	 echo "<p>There are no matches for the word you entered. Please make sure that the word is spelled correctly, and that you haven't left out any spaces or apostrophes. If your search still doesn't return a match, then we probably haven't added it yet.</p>";  }}?>

On a side note, what's the php syntax so far as quotes are concerned? How many of those single quotes are supposed to be double quotes?

Link to comment
Share on other sites

Move the if statement above the while statement like I showed above. If there are no results, then it won't enter the while loop at all.You can use single or double quotes wherever you want, you just need to know the difference between them. The main difference is variable replacement. This:echo "the value is $var";Will replace the $var in the string with the value of the variable $var. This:echo 'the value is $var';Will actually write the string "$var".

Link to comment
Share on other sites

Okay. Now, the paragraph in the if statement is appearing when there are matches.I want it to appear when there are no matches, but be invisible for partial matches. As the search script is now, if someone hits search on say K, then the script will return everything with a k in it. I'd like it not to do that too, if I could. It's fine if it returns partials that start with K, but not partials that start with anything else.The point is, I want the paragraph to appear if there are no matches in the database at all. So it should appear only if the script would output a blank entry.

Link to comment
Share on other sites

It appears that you are not using the if that justsomeguy suggests to use. Check the number of rows returned instead, as he suggests.

if (!mysql_num_rows($result))  echo "no matches";

Because you are using a "LIKE" clause, your attempt to "equals" will fail.

if ($r['word'] != $search_string)

my 2 cents worth, anyways.

Link to comment
Share on other sites

You can also try this:

if (mysql_num_rows($result) == 0)  echo "no matches";

As far as the 1-letter search goes, you can use an if statement to check if the search term is only 1 letter, and if so, make sure the only matches are at the beginning. In order to do that, you would leave the first % off the SQL statement.

Link to comment
Share on other sites

Okay. Didn't see this response here. I tried using that code snippet, but I'm not sure if I did it right. Where should I put it in?As it is right now, the structure I have is...if (word != search string) echo "results";if (mysql num rows (results) == 0) echo "error message";Not sure if that's right or not. Obviously the syntax is wrong since I just wrote that bit structurally and not correctly, but that's what my structure is right now. Do the results need to be an else of the num_rows if?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...