【问题标题】:How could you use PHP and a SQL database to change HTML <h> content?您如何使用 PHP 和 SQL 数据库来更改 HTML <h> 内容?
【发布时间】:2018-06-23 20:00:38
【问题描述】:

我有一个网页,显示从表格中的第一辆车到最后一辆车的汽车,并带有一个 while 循环。

我有以下列:品牌、型号、价格。在我的语法中,我在 Make 行周围有一个锚标记,链接到 Make you click on 的描述页面。

我希望我的&lt;h&gt; 标签更改为相应品牌的型号。

我已经花了一个多小时试图实现这一目标,但我能想到的是:

<?php
$query = "SELECT Model FROM inventory;";

$Vtitle = $conn->query($query);
$Vtitle_ar = mysqli_fetch_assoc($Vtitle);
echo "<h1>".$Vtitle_ar['Model']."</h1>";
?>

这在一定程度上有效。

我单击的每个锚点都会将&lt;h&gt; 标记替换为数据库中模型列下的第一个结果。

这是整个汽车库存页面的代码

<?php
    $query = "SELECT * FROM inventory;";
    /* Try to query the database */
    if ($result = $conn->query($query)) {
      // Don't do anything if successful.
    }
    else {
      echo "Error getting cars from database:" .$conn->error()."<br>";
    }

    // Create the table headers
    echo "<table id='Grid' style='width: 80%'><tr>";
    echo "<th style='width: 50px'>Make</th>";
    echo "<th style='width: 50px'>Model</th>";
    echo "<th style='width: 50px'>Asking Price</th>";
    echo "</tr>\n";

    $class = "odd"; // keep track of whether a row was even or odd, so we can style it later

    // Loop through all the rows returned by the query, creating a table for each row
    while ($result_ar = mysqli_fetch_assoc($result)) {
      echo "<tr class=\"$class\">";
      echo "<td><a href='viewcar.php?VIN=".$result_ar['VIN']."'>".$result_ar['Make']."<a></td>";
      echo "<td>".$result_ar['Model']."</td>";
      echo "<td>".$result_ar['ASKING_PRICE']."</td>";
      echo "</td></tr>\n";

      // if the last row was even, make the next one odd and vice-versa
      if ($class=="odd") {
        $class = "even";
      }
      else {
        $class = "odd";
      }
    }
    echo "</table>";
    $conn->close();
?>

有谁可以做到这一点?

我是编程新手,我正在尝试将它用于我正在为美发沙龙网站工作的实际项目

【问题讨论】:

  • SELECT Model FROM inventory WHERE VIN=YOUR_VALUE

标签: php html mysql select while-loop


【解决方案1】:

在查询中添加WHERE 子句。

$vin = $_GET['VIN'];
$stmt = $conn->prepare("SELECT Model FROM inventory WHERE VIN = ?");
$stmt->bind_param("s", $vin);
$stmt->execute();
$stmt->bind_result($model);
$stmt->fetch();
echo "<h1>$model</h1>";

【讨论】:

  • 这行得通,我相信你知道这一点。谢谢我其实也理解它背后的概念。我正在考虑使用准备好的语句,我只是不知道该怎么做,但这很有帮助!
【解决方案2】:

虽然在格式化代码时使用@Barmar 给出的where 子句无法解决解决方案,但我确实在HTML 中发现了一个不明显的错误

echo "&lt;/td&gt;&lt;/tr&gt;\n"; 行有一个额外的&lt;/td&gt;,这会破坏 html 的流动并可能产生不利影响。此外,// Don't do anything if successful. 没有意义 - 如果有结果则处理记录集,否则显示错误;-)

<?php

    $query = "SELECT * FROM inventory;";

    if ( $result = $conn->query( $query ) ) {
        echo "
        <table id='Grid' style='width: 80%'>
            <tr>
                <th style='width: 50px'>Make</th>
                <th style='width: 50px'>Model</th>
                <th style='width: 50px'>Asking Price</th>
            </tr>";

        $i=0;


        while( $result_ar = mysqli_fetch_assoc( $result ) ) {
            $class = $i %2 == 0 ? 'even' : 'odd';

            echo "
                <tr class='$class'>
                    <td><a href='viewcar.php?VIN={$result_ar['VIN']}'>{$result_ar['Make']}<a></td>
                    <td>{$result_ar['Model']}</td>
                    <td>{$result_ar['ASKING_PRICE']}</td>
                </tr>";

            $i++;
        }

        echo "</table>";

    } else {
      echo "Error getting cars from database:" .$conn->error()."<br>";
    }
    $conn->close();
?>

对于替换表格行的样式(上面使用modulus 函数来计算奇数/偶数),您可以使用一些简单的 CSS - 例如

tr:nth-child( odd ){/* rules */}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多