在php echoed语句

时间:2017-01-18 15:20:03

标签: javascript php html

在下面的php语句中我想为EmployeeName分配ID。我将使用ID标签按名称搜索回显列表中的元素。我在哪里犯了错误?



<?php  while( $toprow4 = sqlsrv_fetch_array( $stmt4) ) {

        echo  "<div class='parent-div'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" id = ' . $toprow4['EmployeeName'] .' "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";

      } ?>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

将id作为<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td class="left"> <span class="copyTarget">Code 1</span> <a class="copyButton">[Copy code]</a> </td> </tr> <tr> <td class="left"> <span class="copyTarget">Code 2</span> <a class="copyButton">[Copy code]</a> </td> </tr> <tr> <td class="left"> <span class="copyTarget">Code 3</span> <a class="copyButton">[Copy code]</a> </td> </tr> </table>添加到div元素,而不是attribute

textContent

并尝试通过删除空格或将其替换为echo "<div class='parent-div' id='" . $toprow4['EmployeeName'] . "'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" . $toprow4['EmployeeName'] . "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>"; 来使该名称成为有效ID。

答案 1 :(得分:1)

您需要检查报价。如果您还没有,我强烈建议您使用带代码突出显示的编辑器。

<?php
while ($toprow4 = sqlsrv_fetch_array( $stmt4)) {  
    $rank = $toprow4['rank'];
    $id = $toprow4['EmployeeName'];
    $points = $toprow4['pointsRewarded'];
    echo "<div class='parent-div'><span class='rank'>$rank</span>";
    echo "<span class='name' id='$id'></span><span class='points'>$points<span></div>";
}
?>

答案 2 :(得分:0)

好像你有一个很大的问题,正确地转义你的输出字符串。也许您应该尝试使用模板和sprintf()来编写它,就像在此示例中一样。

我没有测试你给出的while语句的逻辑

<?php
  $template  = '<div class="parent-div">';
  $template .= '<span class="rank">%s</span>';
  $template .= '<span class="name">id = %s</span>';
  $template .= '<span class="points">%s</span>';
  $template .= '</div>';

  while($toprow = sqlsrv_fetch_array($stmt4) {
    echo sprintf(
      $template, 
      $toprow['rank'], 
      $toprow['EmployeeName'], 
      $toprow['pointsRewarded']
    )
  }
?>

您的代码不干净,因此您输入正确的引号和单引号失败。