【问题标题】:Concatenating an integer based on an sql id for use in a html ID tag连接基于 sql id 的整数以在 html ID 标记中使用
【发布时间】:2016-08-17 06:35:26
【问题描述】:

我目前正在构建一个网站作为我课程的一部分。该网站是一个电子商务网站。我将产品存储在 SQL 数据库中,然后使用 php 脚本将每一行回显到表中。我已经成功地做到了这一点,但是,我现在希望在每个产品上添加一个按钮,该按钮将包含一个将所选产品添加到购物车的 href。唯一的问题是,要构建购物车数组我需要 ID,并且要在循环中设置 ID,您必须连接一个整数,而我不知道如何在我的脚本中执行此操作,这是我目前拥有的,任何帮助将不胜感激。

    while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td><br>".$row['Name']."</td><br>";
echo "<br>";
echo "<td><img src=".$row['Image']."height='200' width = '200'"."</td><br>";
echo "<br>";
echo "<td>Price: £".$row['Price']."</td><br>";
echo "<button type='button'><a href='test.php' id=>Buy</a></button>";
echo "</tr>";

}

提前谢谢,我希望我已经提供了足够的细节。

【问题讨论】:

    标签: php html mysql sql


    【解决方案1】:

    有几种方法可以解决这个问题。

    $id = 123; // as an example. Use the appropriate variable for this.
    
    echo "<button type='button'><a href='test.php' id=$id>Buy</a></button>";
    
    echo "<br>";
    
    echo "<button type='button'><a href='test.php' id=\"$id\">Buy</a></button>";
    

    HTML 源代码中显示的内容:

    <button type='button'><a href='test.php' id=123>Buy</a></button><br><button type='button'>
    
    <a href='test.php' id="123">Buy</a></button>
    

    取决于您是否希望引用它们,如果这就是问题所在。

    或者:

    echo "<button type='button'><a href='test.php' id='".$id."'>Buy</a></button>";
    

    显示 HTML 源代码:

    <button type='button'><a href='test.php' id='123'>Buy</a></button>
    

    然后你可以用 GET 数组拉取数据。

    即:

    $var = $_GET['id'];
    

    然后:

    echo "<button type='button'><a href='test.php?id=".$id."'>Buy</a></button>";
    

    HTML 源代码:

    <button type='button'><a href='test.php?id=123'>Buy</a></button>
    

    while 循环中,您可以将变量分配给 id。

    即:

    $id = $row['id'];
    

    我必须注意,如果您的数据包含单引号,则使用单引号可能会产生不利影响,因此最好使用双引号。

    即:(和转义双引号)

    echo "<button type=\"button\"><a href=\"test.php?id=".$id."\">Buy</a></button>";
    

    在 HTML 源代码中生成:

    <button type="button"><a href="test.php?id=123">Buy</a></button>
    

    实际上,我上周亲身体验了这一点,HTML 源显示了有关格式错误的 HTML 标记的警告,其中包含 Mark's Bar &amp; Grill 等数据。


    while ($row = mysqli_fetch_assoc($result)){
    
        $id = $row['id']; // assuming you have a row for id.
    
        // all your other code
    
        echo "<button type='button'><a href='test.php?id=".$id."'>Buy</a></button>";
    
    }
    

    例如:

    $name = "Bob's Bar & Grill";
    
    echo "<button type='button'><a href='test.php' name='".$name."'>Buy</a></button>";
    

    会在 HTML 源代码中产生 No space between attributes 警告。

    来自 HTML 源代码:

    <button type='button'><a href='test.php' name='Bob's Bar & Grill'>Buy</a></button>
    

    如果与 CSS 规则一起使用,可能会产生不利影响。


    这将与名称 $row['Name'] 等的另一行有关。

    • 请小心。

    【讨论】:

    • 非常感谢,这已经为我整理了代码,因为我在另一个 PHP 脚本中使用了 $_GET['id']。
    • @dannstuff 不客气,很高兴我能帮上忙。您确实看到了我关于使用单引号的说明吗?上周我自己遇到了一个问题,需要使用双引号。我猜的一切都是第一次 ;-) 像我的老爸爸曾经说过的那样生活和学习。 干杯
    【解决方案2】:

    别担心它是一个整数。只需使用从数据库返回的值,就像对字符串所做的那样。它将从 int 转换为 string。另请注意,您为创建字符串所做的连接并不是真正需要的。由于 php 变量以 $ 开头,您可以像这样内联编写它们:

    echo "<button type='button'><a href=\"test.php\" id=\"$row['id']\">Buy</a></button>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多