更改查询的表示形式

时间:2016-04-22 17:32:58

标签: php html

我有这段代码从数据库输出记录

<?php
include 'database_conn.php';      // make db connection

$sql = "SELECT filmID, title, categoryName, notes FROM film";

$queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));

while ($row = mysqli_fetch_assoc($queryresult)) {
$filmID = $row['filmID'];
$filmTitle = $row['title'];
$filmCat = $row['categoryName'];
$filmNotes = $row['notes']; 

    echo "<div>$filmID, $filmTitle, $filmCat, $filmNotes</div>";
    } 

    mysqli_free_result($queryresult); 
    mysqli_close($conn);
    ?>

但是为了能够删除逗号并格式化我需要更改的内容的显示...

echo "<div>$filmID, $filmTitle, $filmCat, $filmNotes</div>";

为...

echo
"<div class="film">
<span class="filmID">5</span>
<span class="title">my darling clementine</span>
<span class="category">epic</span>
<span class="notes">Henry Fonda</span>
</div>";

我在提交表单时遇到错误(解析错误:语法错误,意外'&lt;'在第45行的/home/unn_w15025267/public_html/chollerton.php中)

第45行是:

"<div class="film">

记录目前看起来像这样: Patrick,Bloggs,15,Blogs Street,Durham,DH2 ABC,0191 123 4567,07001212111,patrick&amp; sheila@madeupemail.com,SMS

对此非常新,所以任何帮助都会很棒

4 个答案:

答案 0 :(得分:2)

正确的代码如下:

echo
"<div class='film'>
<span class='filmID'>5</span>
<span class='title'>my darling clementine</span>
<span class='category'>epic</span>
<span class='notes'>Henry Fonda</span>
</div>";

当第一个双引号开始字符串而下一个双引号关闭字符串。 所谓"<div class="film">是错误的。

或者你也可以逃避内部的双重引用:

echo
"<div class=\"film\">
<span class=\"filmID\">5</span>
<span class=\"title\">my darling clementine</span>
<span class=\"category\">epic</span>
<span class=\"notes\">Henry Fonda</span>
</div>";

答案 1 :(得分:1)

您在字符串文字中使用双引号。它会使解析器混淆。改为使用单引号:

echo
"<div class='film'>
<span class='filmID'>5</span>
<span class='title'>my darling clementine</span>
<span class='category'>epic</span>
<span class='notes'>Henry Fonda</span>
</div>";

答案 2 :(得分:0)

尝试heredoc

echo <<<HERE
<div class="film">
<span class="filmID">$filmID</span>
<span class="title">$filmTitle/span>
<span class="category">$filmCat</span>
<span class="notes">$filmNotes</span>
</div>

HERE;

答案 3 :(得分:0)

假设您想使用从DB获得的变量,我认为这就是您想要的。这个答案也假设在这个HTML之上没有PHP。如果有,你必须在这些行之前离开PHP解析(?>)。

<div class="film">
  <span class="filmID"><?php echo $filmID; ?></span>
  <span class="title"><?php echo $filmTitle; ?> </span>
  <span class="category"><?php echo $filmCat; ?></span>
  <span class="notes"><?php echo $filmNotes; ?></span>
</div>

<?php
    //resume PHP parsing (if needed)