1

I can't seem to get a URL into echo. I want to make a link to open Google Maps:

enter image description here

I can't figure out what's wrong with my code:

$query = mysql_query("SELECT * FROM cev")or die(mysql_error());
while($row = mysql_fetch_array($query))
{
  $name = $row['sitecode'];
  $lat = $row['latitude'];
  $lon = $row['longitude'];
  $type = $row['sitetype']; 
  $city = $row['city']; 
  $id = $row['id'];

  echo("addMarker($lat, $lon,'<b>$name</b><a href="editcev.php?id=' . $row['id'] . '">View</a><br><br/>$type<br/>$city');\n");

3 Answers 3

2

You have to fix the quotes:

 echo "addMarker($lat, $lon,'<b>$name</b><a href=\"editcev.php?id={$row['id']}\">View</a><br><br/>$type<br/>$city');\n";

Alternative ways

Here document

echo <<<EOS
addMarker($lat, $lon, '<b>$name</b><a href="editcev.php?id={$row['id']}">View</a><br><br/>$type<br/>$city');

EOS;

Concatenation

echo "addMarker($lat, $lon, '<b>$name</b>" .
  "<a href=\"editcev.php?id={$row['id']}\">View</a>" .
  "<br><br/>$type<br/>$city)";

Using addshashes

The addMarker looks like a JavaScript function. You might pre-process the HTML string by means of addslashes:

$html = <<<EOS
<b>$name</b><a href="editcev.php?id={$row['id']}">View</a><br><br/>$type<br/>$city
EOS;
$html = addslashes($html);

echo "addMarker($lat, $lon, '$html');\n";

Recommendations

I recommend using an editor with support of syntax highlighting.

Read about PHP strings. Especially the matter of escaping.

Finally, I wouldn't recommend writing any HTML/JavaScript within a PHP code. Use template engines such as Smarty or Twig instead.

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

1 Comment

for 2 answers, you could use heredocs/newdocs for this. php.net/manual/en/language.types.string.php
0

It seems like you are trying to use method inside the echo statement. If you want to use methods, variables or some php stuffs you should not use quotes at most case unless it is an eval featured object or method.

Try like this

echo addmarker($lat, $lon, 
               '<b>'.$name.'</b> <a href="'.editcev.php?id=.' '.$row['id'].
               ".'>View</a><br><br/>'
               .$type.
               '<br/>'
               .$city.');'."\n");

I don't know your exact situation but i think this works

Comments

-1
  echo("addMarker(".$lat.",".$lon.",<b>".$name."</b><a href=ditcev.php?id=" . $row['id'] . ">View</a><br><br/>".$type."<br/>".$city.");\n");

Comments

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.