Jump to content

syntax problem


HalRau

Recommended Posts

I am using the code below to attempt to show stored value only when data is present in the record set.

 $results[] = "
if ({$row['MapLocation']} != NULL ){
echo <a href=\"{$row['MapLocation']}\" target=_blank>Map</a>}
";
            $i++;

What I see on the page is: if ( != NULL ){ echo Map}  with Map being a link.

 

I know there is a syntax error somewhere but can't find it. Open to suggestions.

Thanks for your help

Link to comment
Share on other sites

Thanks for your help. It is responding, just not correctly. For instance, if the MapLocation field is populated the results page shows "if (http://maps.google.com/maps?q=16303+Truman+RD+Ellisville,+MO+63011-4731&hl=en&sll=37.0625,-95.677068&sspn=60.116586,135.263672&t=h&z=17 != NULL ){ echo Map} " which is the MapLocation data AND the hyper link is correctly populated.

 

The if statement is already inside a php "result" from a search.

Bottom line, if the MapLocation field is null I want the result page to show nothing, no "Map" hyper link text or anything. If the MapLocation field is populated then show the word "Map" with the hyperlink embedded.

 

The full $results looks like this

$results[] = "<strong>{$row['BusinessName']}</strong><br />
		{$row['BusinessDescription']}<br />

		{$row['BusinessStreet']} {$row['BusinessCity']} MO, {$row['BusinessPostalCode']}
		if ({$row['MapLocation']} != NULL ){
echo <a href=\"{$row['MapLocation']}\" target=_blank>Map</a>}
	 <br />
		{$row['BusinessPhone']}<br />
		<a href=\"{$row['WebAddress']}\" target=_blank>Visit our website</a><br /><br />";
            $i++;

 

Thanks for your help

Link to comment
Share on other sites

jotorres1

If I take out the curly brackets I get a syntax error

"Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/85/6282385/html/search.php on line 54"

So it seems they are needed.

Link to comment
Share on other sites

Oh wait, I have a better suggestion.

This should do the trick:

 <?php
$results[] = "<strong>{$row['BusinessName']}</strong><br />
		{$row['BusinessDescription']}<br />

		{$row['BusinessStreet']} {$row['BusinessCity']} MO, {$row['BusinessPostalCode']}";

		if ({$row['MapLocation']} != NULL ){
 $result[] .= '<a href="'.{$row['MapLocation']}.'" target=_blank>Map</a>';
                            }
$result[] .="<br />
		{$row['BusinessPhone']}<br />
		<a href=\"{$row['WebAddress']}\" target=_blank>Visit our website</a><br /><br />";
?>

 

 

Link to comment
Share on other sites

Here is the entire query and result code. Right now it throws this error Parse error: syntax error, unexpected '{' in /home/content/85/6282385/html/search.php on line 55  Line 55 is  $result[] .= '<a href="'.{$row['MapLocation']}.'" target=_blank>Map</a>';

  $searchSQL = "SELECT * FROM members WHERE LiveOnWeb ='1' AND 
  BusinessName LIKE '%{$searchTermDB}%'
      OR BusinessDescription LIKE '%{$searchTermDB}%'
  OR SearchKeyWords LIKE '%{$searchTermDB}%'
  ORDER BY BusinessName";
      
    

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
      
      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "<strong>Sorry, the search term "{$searchTerms}" yielded no results.</strong>";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "<strong>{$row['BusinessName']}</strong><br />
		{$row['BusinessDescription']}<br />
		{$row['BusinessStreet']} {$row['BusinessCity']} MO, {$row['BusinessPostalCode']}<br />";

	if ($row['MapLocation'] != NULL ) 
 $result[] .= '<a href="'.{$row['MapLocation']}.'" target=_blank>Map</a>';
                           
$result[] .="<br />
		{$row['BusinessPhone']}<br />
		<a href=\"{$row['WebAddress']}\" target=_blank>Visit our website</a><br /><br />";
            $i++;
         }
      }
   }

Link to comment
Share on other sites

These are the kinds of errors that inconsistency ultimately leads to. You're using different methods of quoting from line to line, and getting confused. Why are you trying to push the strings into an array (the name of which changes from $results to $result halfway through, BTW) then using a concatenation operator to do so? That code is all over the place, and you should really take a few minutes and figure out what you're trying to do with it.

 

If you want to store the results to display later, just push them into a new array and add the html for formatting as the code is echoed. That would probably be quite easy to do in a foreach() loop later in the code.

Link to comment
Share on other sites

To jotorres1,

 

I took out the {} and it allowed the page to load the results but the displayed results ends after the "; and jumps to the next result.

The map link doesn't show nor does the link to the website.

 

Here is the code as it stands right now.

$results[] = "<strong>{$row['BusinessName']}</strong><br />
		{$row['BusinessDescription']}<br />
		{$row['BusinessStreet']} {$row['BusinessCity']} MO, {$row['BusinessPostalCode']}<br />";

	if ($row['MapLocation'] != NULL ) 
 $result[] .= '<a href="'.$row['MapLocation'].'" target=_blank>Map</a>';
                           
$result[] .="<br />
		{$row['BusinessPhone']}<br />
		<a href=\"{$row['WebAddress']}\" target=_blank>Visit our website</a><br /><br />";
            $i++;

 

I noticed that you are using " $result[] .=" inside the "$results[] =" tag. Could you explain this to the newbie?

Thank you for your help.

Link to comment
Share on other sites

The .= means: concatenate the string.

 

I notice that you do not close <strong> tag.  That might be the problem right there.

<strong>

Try closing the tag.  Also, if that doesn't work, then try doing this last attempt.

 

Add another variable in that loop and make it look like this. 

$my_string = "";
$my_string .= "<strong>{$row['BusinessName']}</strong><br />
		{$row['BusinessDescription']}<br />
		{$row['BusinessStreet']} {$row['BusinessCity']} MO, {$row['BusinessPostalCode']}<br />";

	if ($row['MapLocation'] != NULL ) 
 $my_string .= '<a href="'.$row['MapLocation'].'" target=_blank>Map</a>';
                           
$my-string .="<br />
		{$row['BusinessPhone']}<br />
		<a href=\"{$row['WebAddress']}\" target=_blank>Visit our website</a><br /><br />";
$results [] = $my_string;
            $i++;

 

 

 

 

Link to comment
Share on other sites

jotorres1, Your code works!!! Thank you very much. Now I have one last addition that I am trying to get to work. I want the business address, city, state and zipcode to show only if a stored value (ShowAddress) equals 1 or True.

 

Here is the code I have tried:

$my_string = "";
$my_string .= "<strong>{$row['BusinessName']}</strong><br />
		{$row['BusinessDescription']}<br />";
if ($row['ShowAddress'] == 1 ) 
 $my_string .= "Location: "'.$row['BusinessStreet'].' '.$row['BusinessCity'].'" MO, "'.$row['BusinessPostalCode'].';
	if ($row['MapLocation'] != NULL) AND ($row['ShowAddress'] == 1 ) 
 $my_string .= '<a href="'.$row['MapLocation'].'" target=_blank>Map</a>';
$my_string .="<br />
		{$row['BusinessPhone']}<br />";
	if ($row['WebAddress'] != NULL ) 
 $my_string .= '<a href="'.$row['WebAddress'].'" target=_blank>Visit our website</a>';
 $my_string .="<br /><br />";
$results [] = $my_string;
            $i++;

I get this error message: "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/85/6282385/html/search.php on line 54" Line 54 being,

 $my_string .= "Location: "'.$row['BusinessStreet'].' '.$row['BusinessCity'].'" MO, "'.$row['BusinessPostalCode'].';

 

I'm sure the problem is in the single or double quotes somewhere.

 

Also, am I doing the if statement for the map correctly now? if ($row['MapLocation'] != NULL) AND ($row['ShowAddress'] == 1 )

 

Thank you so much for your time and helping this php newbie learn.

 

Link to comment
Share on other sites

Yup.  Take a look at my post regarding a simple send mail script.  It is very generic, and very easy to understand.  Since you already have basic knowledge, and know what to do regarding the database insertion, then all you have to do is modify the code to fit your needs.

 

Here's the link:

http://www.jotorres.com/en/2011/10/simple-php-send-mail/

 

If you have any questions, feel free to ask.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.