【问题标题】:Error when outputting php and javascript from inside quotes or heredoc从引号或 heredoc 中输出 php 和 javascript 时出错
【发布时间】:2013-07-24 12:38:27
【问题描述】:

尝试使用以下代码时,我不断收到错误“解析错误:语法错误,意外的 T_SL”:

<?php 
$names = array(1 => 'example1', 2 => 'example2',    3 => 'example3', 4 => 'example4');

$code = <<<HEREDOC<div><h3>$names[$i]</h3>
    <div class="rating"><div class="id$i" id="0
        $stmt = mysqli_prepare($dbc, 'SELECT userid, rating FROM ranks WHERE userid = ? AND id =?');
            mysqli_stmt_bind_param($stmt, 'ii', $userid, $i);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $userid, $i, $rating);
            while (mysqli_stmt_fetch($stmt)){
                echo ($rating);
            }_$i"><script type="text/javascript">$(document).ready(function(){$(".id' . $i. '").jRating({isDisabled : false});});</script></div></div><div class="push"> 
        $query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."'");
        $result = mysqli_query($dbc, $query) or die("Error querying database.");
        while($row = mysqli_fetch_array($result)){
            echo "<h3 class='average'>" . $row["ROUND( AVG(rating),1 )"] . "/5" . "</h3>";}
        </div><br/></div><div class="line"></div>;HEREDOC;


for($i=1; $i<5; $i++)
{
    echo $code
}?>

我确定我做错了,我尝试在每次切换到 JS/HTML 时使用 php 打开和关闭标签,还尝试使用单引号并出现错误。感谢您的宝贵时间!

【问题讨论】:

    标签: php javascript html for-loop heredoc


    【解决方案1】:

    heredoc的正确样式是:

    $code = <<<HEREDOC
    
    HEREDOC;
    

    每个符号都应该在行首并以换行符结束。

    更新:正如@Mike 所提到的

    不能在 HEREDOC 表示法中分配变量或执行 while 循环

    【讨论】:

    • 更不用说您不能在 HEREDOC 表示法中分配变量或执行 while 循环。
    • 谢谢@Mike 我添加了你的评论
    • 那么我应该如何处理这个问题?使用 php 标签来区分 html 和 JS,还是使用引号和 .'s? (这两个我以前都没有工作过。)
    【解决方案2】:

    首先:代码有错误。 线

    $query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."'");
    

    应该是

    $query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."')";
    

    第二:另一种解决方案是连接您的内容

    <?php 
    $names = array(1 => 'example1', 2 => 'example2',    3 => 'example3', 4 => 'example4');
    
    $code = '<div><h3>'.$names[$i].'</h3>
        <div class="rating"><div class="id'.$i.'" id="0'.(
            $stmt = mysqli_prepare($dbc, 'SELECT userid, rating FROM ranks WHERE userid = ? AND id =?');
                mysqli_stmt_bind_param($stmt, 'ii', $userid, $i);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_bind_result($stmt, $userid, $i, $rating);
                while (mysqli_stmt_fetch($stmt)){
                    echo ($rating);
                }).'_'.$i.'"><script type="text/javascript">$(document).ready(function(){$(".id' . $i. '").jRating({isDisabled : false});});</script></div></div><div class="push">'.( 
            $query = "SELECT ROUND( AVG(rating),1 ) FROM ranks WHERE fight_id ='".$i."')";
            $result = mysqli_query($dbc, $query) or die("Error querying database.");
            while($row = mysqli_fetch_array($result)){
                echo "<h3 class='average'>" . $row["ROUND( AVG(rating),1 )"] . "/5" . "</h3>";}).'
            </div><br/></div><div class="line"></div>';
    
    
    for($i=1; $i<5; $i++)
    {
        echo $code
    }?>
    

    建议:转换函数中的查询(以及在需要时调用这些函数)之类的内容,以便您的代码更简洁、更易于理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2013-06-07
      • 2013-11-24
      • 2019-03-12
      相关资源
      最近更新 更多