0

I would like to hyperlink my url data in popup, where user can click those website. However, it did not work.

enter image description here

Below is my source code:

if ($rowCount > 0) {
    //Array for JSON output
    $rows = array();

    //Get the SQL results
    while($row = $result->fetch_array()){
        $fid = $row["Id"];
        $Name = $row["Name"];
        $address = $row["address"];
        $Star = $row["Star"];
        $latitude = $row["latitude"];
        $longitude = $row["longitude"];
        $hp = $row["hp"];
        $url = $row["url"];

        $rows[] = array(
            "fid" => $fid,
            "Name" => $Name,
            "address" => $address,
            "Star" => $Star,
            "latitude" => $latitude,
            "longitude" => $longitude,
            "hp" => $hp,
            "url" => $url
        );
    }
    echo "var dataPOIs = ";
    //Output to JSON format
    echo json_encode($rows, JSON_NUMERIC_CHECK);
    echo ";\n";
    echo '
    for (var i=0; i<dataPOIs.length; i++){
        L.marker([dataPOIs[i].latitude, dataPOIs[i].longitude],{icon: createIcon(), title:dataPOIs[i].Name}).bindPopup("Name: " + " " + "<b>" + dataPOIs[i].Name + "</b>" + "</br>" + "</br>" + "Star: " + "<b>" + dataPOIs[i].Star + "</b>" + "</br>" + "</br>" + "Address: " + " " + "<b>" + dataPOIs[i].address + "</b>" + "</br>" + "</br>" + "Hp: " + " " + "<b>" + dataPOIs[i].hp + "</b>"  + "</br>" + "</br>" + "Website: " + " " + "<a href='" + dataPOIs[i].url + "'>" + "</a>").addTo(pois);
    }
    ';
}

It shows

Parse error: syntax error, unexpected '" + dataPOIs[i].url + "' (T_CONSTANT_ENCAPSED_STRING), expecting ';' or ','

in Popup line

ADyson
  • 57,178
  • 14
  • 51
  • 63
Fika
  • 1
  • 1
  • "did not works" tells us nothing useful about your issue. Please be more specific. What actually happens when you run the code? Do you get an error? Unexpected output / behaviour? make some attempt to debug your code and narrow down the problem a bit. – ADyson Jul 07 '21 at 11:34
  • It shows Parse error: syntax error, unexpected '" + dataPOIs[i].url + "' (T_CONSTANT_ENCAPSED_STRING), expecting ';' or ',' in Popup line – Fika Jul 07 '21 at 11:36
  • Ok. Any reason you didn't mention that in the question? Error messages are very useful information. I'll do it this time but in future please _edit your question_ instead of adding that kind of info into the comments. It's important it should be displayed prominently. – ADyson Jul 07 '21 at 11:48
  • Anyway the error is because you're using `'` inside a PHP string which is enclosed with `'`. You need to escape the inner `'`s. As per [the manual](https://www.php.net/manual/en/language.types.string.php): _To specify a literal single quote, escape it with a backslash ( \ )_. – ADyson Jul 07 '21 at 11:51

1 Answers1

-1

I can see from the image link you shared that you were trying to implement a tooltip on a map on the frontend but you shared a backend (php) script.

This has nothing to do with your php code.

You should focus more on reading the map's documentation(google map) and see how you can implement a tooltip with hyperlink.

What you are looking for is called Info Window.

https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple.

Ps:

Learn to write better codes. Try seperating views from business logic.

  • 1
    `but you shared a backend (php) script.` ...yeah because the PHP code uses the database data and generates some javascript which is then used to set the map markers. I don't think you understood the code. Also see the updated version where a specific PHP error message is now mentioned. – ADyson Jul 07 '21 at 11:52
  • Yes I understood the code and it is messy. You should try to seperate your views (html and js) from your backend logic(php) next time. Look the documentation what you trying to achieve is called Info Window. https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple – Chukwuezugo Okpara Jul 07 '21 at 12:01
  • I think you're mistaking me for the OP :-). It's not my code and I'm not going to separate anything. If you understood the code then why did you say the problem was "nothing to do with PHP" when it's clearly a PHP error. – ADyson Jul 07 '21 at 12:09
  • Yes, sorry about that :) but why did you downvote me :( It is really not a php error in a sense had he seperated the php from javascript. Map presentation and makers are all javascript. We are all here to educate people and also learn. Teach the OP the best approach to things and not just answering their questions. I have supplied the link to the map's javascript documentation for the problem. Again, it is javascript and not a php error. – Chukwuezugo Okpara Jul 07 '21 at 12:11
  • `It is really not a php error in a sense had he seperated the php from javascript`...yeah but he didn't, so it is still a PHP error. You have to deal with the reality, not the ideal scenario. If you're going to suggest separating the functionality as a means of solving the problem, then a useful answer would actually give an example of how to do it better. But actually the simple/quick solution is just to escape the problematic quote marks – ADyson Jul 07 '21 at 12:47