douqiu9529 2016-04-20 20:45
浏览 19
已采纳

是什么让这个PHP代码正在输出HTML?

I'm trying to output a button using PHP but I cant seem to get the ' and " correctly.

This is the output I should be getting from the PHP code below:

<button class="button button1" onclick="buttonTest(99.16918,-82.9191)"> Lithuania </button>    

This is my code:

foreach ($getClasses as $row){
echo '<button class="button button1" onclick="buttonTest(', $row->BUILDING_Latitude ,',',$row->BUILDING_Longtitude,)>', $row->COURSE_Title,'</button>';
}

how do i get php to output this html code above i get an error

syntax error, unexpected '>' (48) : eval()'d code

syntax error, unexpected ')', expecting ',' or ';' (48) : eval()'d code

Update

Also, please advice me the best way to rewrite this, so that these kind of issues do not occur in the future.

  • 写回答

1条回答 默认 最新

  • dongyue934001 2016-04-20 20:47
    关注

    You missed the ' here:

    tude, ')">', $ro
    //----^-^
    

    Full code:

    echo '<button class="button button1" onclick="buttonTest(', $row->BUILDING_Latitude ,',',$row->BUILDING_Longtitude, ')">', $row->COURSE_Title,'</button>';
    //------------------------------------------------------------------------------------------------------------------^-^
    

    I would really write this way:

    <?php
        foreach ($getClasses as $row) {
            echo "<button class=\"button button1\" onclick=\"buttonTest({$row->BUILDING_Latitude}, {$row->BUILDING_Longtitude})\">{$row->COURSE_Title}</button>";
        }
    

    Or even better:

    <?php
        foreach ($getClasses as $row) {
            $lat = $row->BUILDING_Latitude;
            $lon = $row->BUILDING_Longtitude;
            $course = $row->COURSE_Title;
            echo "<button class=\"button button1\" onclick=\"buttonTest($lat, $lon)\">$course</button>";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗