【问题标题】:What is the correct syntax for using inline styling using PHP and a while loop in an HTML table?在 HTML 表中使用 PHP 和 while 循环使用内联样式的正确语法是什么?
【发布时间】:2019-04-29 03:13:04
【问题描述】:

我需要使用 php 变量和 while 循环在表中显示行,但我不知道我做错了什么。我尝试了各种不同的单引号和双引号组合,但我仍然无法使用正确的语法来使这些行在表中输出而不会产生任何错误。我正在使用 Dreamweaver 2019 对其进行编码。我使用了 Netbeans 8.2,但我仍然无法弄清楚这段代码的正确语法。

到目前为止,这也是我在 stackoverflow 上找到的内容,但我仍然没有找到我真正需要的内容。在这种情况下,我也无法准确找到如何使用谷歌使用正确的语法:

php - for loop inside a while loop, correct syntax?

Inline Styling in PHP

https://stackoverflow.com/search?q=use+inline+styling+with+php+html+table

html tables & inline styles

HTML Table with inline PHP

<?php 

    include 'connection.php'; // includes the connection.php file to connect 
    to the database

    // query to database and prepares and executes
    $query = "SELECT id, first_name, last_name FROM customers ORDER BY 
    last_name asc";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($id, $first_name, $last_name);

    // count the number of customers
    $total_customers = $stmt->num_rows;

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>

<!--start container to center design in browser-->
<div class="container">

    <div class="row" style="margin-bottom:30px">
        <div class="col-xs-12">
            <ul class="nav nav-pills">
              <li class="nav-item">
                <a class="nav-link" href="index.php">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="customers.php">Customers</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="">Search</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="">Add New</a>
              </li>
            </ul>
        </div>
    </div>


<div class="row">

    <div class="col-xs-12">
      <table class="table table-bordered table-striped table-hover">
           <p><strong>There are a total of <?PHP echo $total_customers; ?> 
           customers.</strong></p> <!-- output total number of customers -->
              <thead>
                <tr class="success">
                  <th class="text-center">#</th>
                  <th>Customer ID</th>
                  <th>Last Name</th>
                  <th>First name</th>
                  <th class="text-center">Details</th>
                </tr>
              </thead>
              <tbody>           

               <tr>
               <?php   

                 while($result = $stmt->fetch()) {                   
                  echo '<th class="text-center">'#'</th>'
                  echo  <th>"$result"</th>
                  echo <th>"$result"</th>
                  echo <th>"$result"</th>
                  echo <th class="text-center">"Details"</th>
                  }

                ?>
                  </tr>
              </tbody>
            </table>
    </div>

</div>

</div>
<!--end container to center design in browser--> 

</body>

</html>

【问题讨论】:

  • 一些分号似乎是一个好的开始
  • error reporting添加到文件顶部测试时在您打开PHP标签之后,例如&lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);
  • 这条线看起来有点奇怪! echo '&lt;th class="text-center"&gt;'#'&lt;/th&gt;'S 你到底想要什么输出??
  • 那里没有人报告这个问题? ... ?&gt;[newline][newline]&lt;!doctype html&gt;
  • 感谢@RiggsFolly 对使用分号的建议,尤其是使用错误报告!在第一个回声语句后不小心输入了一个 S :-)

标签: php loops html-table syntax


【解决方案1】:

因为您使用了$stmt-&gt;bind_result($id, $first_name, $last_name);,所以您选择的列将由$stmt-&gt;fetch()返回到名为$id$first_name$last_name的变量中

还要记住每个echo 是一个不同的语句,应该以; 结尾

另请注意,当使用双引号字符串文字时,您的变量将自动扩展。同样在双引号文字中,您可以使用单引号而不会导致文字提前终止的问题。这也适用于在单引号文字中使用双引号。

while($stmt->fetch()) {                   
    echo "<th class='text-center'>'#'</th>";
    echo "<th>$id</th>";
    echo "<th>$first_name</th>";
    echo "<th>$last_name</th>";
    echo "<th class='text-center'>Details</th>";
}

【讨论】:

  • 再次感谢@riggsfolly! cmets 也很有帮助!我正在打印此页面以供大家查看和学习所有非常有用的 cmets 和代码。
【解决方案2】:

更好的方法是更接近类似模板的解决方案。

<tbody>   
<?php while($result = $stmt->fetch()): ?>        
<tr>
    <td clas="text-center">#</td>
    <td><?php echo $id; ?> </td>
    <td><?php echo $first_name; ?> </td>
    <td><?php echo $last_name; ?> </td>
    <td class="text-center">Details</td>
</tr>
<?php endwhile; ?>

【讨论】:

  • 谢谢@unamatasanatari!这对我也很有帮助!即将准备好将这一切打印出来,以便在今天的工作中复习和学习 :-)
【解决方案3】:

首先您需要了解如何使用循环。您在循环中的代码不正确。有很多报价问题。此外,根据您的代码,我相信您正在获取表格而不是使用循环中的列,例如 $result['id']$result-&gt;id

您可能还需要了解 PHP 串联的工作原理。 PHP 允许对字符串使用单引号 ' 和双引号 "。如果您使用相同的代码来包装变量,则必须使用反斜杠 \ 对其进行转义。

试试下面的代码,或许可以解决你的问题。

<?php
    while($result = $stmt->fetch()) {    

        echo '<th class="text-center">#</th>';
        echo '<th>' . $id . '</th>';
        echo '<th>' . $first_name . '</th>';
        echo '<th>' . $last_name . '</th>';     
        echo '<th class="text-center">Details</th>';

    }
?>

另外,您应该检查this 以了解 PHP 字符串和连接

【讨论】:

    【解决方案4】:

    尝试使用以下代码

    这样写'$id, $last_name, $firs_name

    <?php   
    
    while($result = $stmt->fetch()) {                   
        echo '<th class="text-center">#</th>';
        echo  '<th>'.$id.'</th>';
        echo '<th>'.$lastt_name.'</th>';
        echo '<th>'.$first_name.'</th>';
        echo '<th class="text-center">Details/th>';
    }
    
    ?>
    

    【讨论】:

    • Nudge OP 使用了$stmt-&gt;bind_result($id, $first_name, $last_name); 所以这不是一个将被返回的对象
    • 感谢您提供代码@bhargavchudasama!在编写此代码时,我无法准确地跟踪您:'.$result->id if object else $result['id'] if array 在这个 while 循环中。在上面的代码中发布后,我该如何做到这一点而不会出现此错误?注意:尝试在第 75 行的 C:\wamp64\www\seanvzwebguy\A10\customers.php 中获取非对象的属性“id”
    【解决方案5】:

    尝试使用&lt;?=$string; ?&gt;,如下所示:

    <?php while($result = $stmt->fetch()): ?>        
    <tr>
        <td clas="text-center">#</td>
        <td><?=$id; ?> </td>
        <td><?=$first_name; ?> </td>
        <td><?=$last_name; ?> </td>
        <td class="text-center">Details</td>
    </tr>
    <?php endwhile; ?>
    

    在你的 html 中使用更少的 php 来保持它的整洁。

    【讨论】:

      猜你喜欢
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      相关资源
      最近更新 更多