dsa1230000 2019-05-07 09:47
浏览 103

如何使用`echo`混合PHP和JavaScript代码?

First of all it has been almost a week that I've started coding, so even though some essential knowledge is there, I'm still missing lots of things. I'm making a PWA and need a map echoed when someone logs in with an account. Accounts are working, getting info from databases is working so into the problem we go. First of all I'm guessing that the script inside the echo is trying to get some things from the js files but fails to do so because of probably the way the code is written. In this php code here, everything works for other map examples, but I wanted to use another more suitable map for my needs. Concluding, when I run the site, even though the whole thing loads, map doesn't show. I'm currently guessing that there is a problem with the way I use "" or '' or the import command. I've been trying for 2 days now changing things inside the code but nothing seems to be working. I hope I gave you the info you need and looking forward to your suggestions.

I've tried using " instead of ' inside script. Doesn't work cause it gets recognised as php code. Tried using additional ' to the "from" part of the import, no bueno. Tried using heredoc, but doesn't work cause I get the "syntax error ,page has unixpected end" or some sort which probably means I'm doing it the wrong way. Have changed the " to ' insite the attributions and url links cause it was either marked as a //message or it was recognised as a "php code". Here is the link to the map i want to use https://openlayers.org/en/latest/examples/xyz-esri.html

<?php
  $sql = "SELECT * FROM users WHERE uidUsers = '" . $_SESSION['userUid'] . "'";
  $result = mysqli_query($conn, $sql);
  $resultCheck = mysqli_num_rows($result);
  if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result) ){
      $lon = $row['lon'];
      $lat = $row['lat'];
      $lonlat = $lon.", ".$lat;
      echo "<script type='text/javascript'>
    import Map from 'jsc/ol/Map.js';
    import View from 'jsc/ol/View.js';
    import TileLayer from 'jsc/ol/layer/Tile.js';
    import {fromLonLat} from 'jsc/ol/proj.js';
    import XYZ from 'jsc/ol/source/XYZ.js';
    var map = new Map({
        target: 'map',
        layers: [
          new TileLayer({
            source: new XYZ({
              attributions: 'Tiles © <a href=\"https://services.arcgisonline.com/ArcGIS/' +
                  'rest/services/World_Topo_Map/MapServer\">ArcGIS</a>',
              url: 'https://server.arcgisonline.com/ArcGIS/rest/services/' +
                  'World_Topo_Map/MapServer/tile/{z}/{y}/{x}'
            })
          })
        ],
        view: new View({
          center: fromLonLat([$lonlat]),
          zoom: 15
        })
      });
           </script>";
    }
  }
  ?>
  • 写回答

2条回答 默认 最新

  • dqzow3859 2019-05-07 10:07
    关注

    Try the following code. It's your same code, except, I've closed PHP code before javascript starts and reopened after javascript ends. In between for passing the variable lonlat, I used the notation, <?= $lonlat ?>.

    Note: This is untested code.

    <?php
      $sql = "SELECT * FROM users WHERE uidUsers = '" . $_SESSION['userUid'] . "'";
      $result = mysqli_query($conn, $sql);
      $resultCheck = mysqli_num_rows($result);
      if ($resultCheck > 0) {
        while ($row = mysqli_fetch_assoc($result) ){
          $lon = $row['lon'];
          $lat = $row['lat'];
          $lonlat = $lon.", ".$lat;
          ?>
          <script type='text/javascript'>
        import Map from 'jsc/ol/Map.js';
        import View from 'jsc/ol/View.js';
        import TileLayer from 'jsc/ol/layer/Tile.js';
        import {fromLonLat} from 'jsc/ol/proj.js';
        import XYZ from 'jsc/ol/source/XYZ.js';
        var map = new Map({
            target: 'map',
            layers: [
              new TileLayer({
                source: new XYZ({
                  attributions: 'Tiles © <a href='https://services.arcgisonline.com/ArcGIS/' +
                      'rest/services/World_Topo_Map/MapServer'>ArcGIS</a>',
                  url: 'https://server.arcgisonline.com/ArcGIS/rest/services/' +
                      'World_Topo_Map/MapServer/tile/{z}/{y}/{x}'
                })
              })
            ],
            view: new View({
              center: fromLonLat([<?= $lonlat ?>]),
              zoom: 15
            })
          });
               </script>
        <?php
        }   // while loop ends
      } // closing if condition
      ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱