【问题标题】:PHP Table - Different Hyperlink on each rowPHP表 - 每行不同的超链接
【发布时间】:2012-05-10 06:25:36
【问题描述】:

我想要做的是得到它,因此每行回显都有一个不同的超链接地址。代码如下:

    echo "<table border='1' cellpadding='10'>";
    echo "<tr>  <th>Product Name</th> <th>Product Description</th> <th>Product Price</th> <th>Product Image</th> <th>View Product Details</th></tr>";

    while($row = mysql_fetch_array( $result )) {

            echo "<tr>";
            echo '<td>' . $row['Product_Name'] . '</td>';
    echo '<td>' . $row['Product_Description'] . '</td>';
            echo '<td>' . $row['Product_Price'] . '</td>';
            echo '<td><a href="print_pic.php">Picture Enlarge</a></td>';
            echo '<td><a href="phone1.php?id=1">View Details</a></td>';
            echo "</tr>";

    } 
  echo "</table>";

【问题讨论】:

  • 超链接中的内容是什么?行号?
  • 另外,你要超链接到什么? ...虽然您可以在整行周围放置一个标签,但这是您想要做的吗?

标签: php html mysql hyperlink html-table


【解决方案1】:

您可以像处理其他变量一样将 php 变量内联到 echo'ed 锚点中。

假设您在数据库中使用id 字段,您可以这样做:

echo '<td><a href="phone1.php?id=' . $row['id'] . '">View Details</a></td>';

如果您 echo 一个 php 变量(例如 $row['id']),那么它将回显到 HTML(不一定是文本)。因此,由于这个包含在锚标记中(在 HTML 中),它会回显到锚标记并构建 id 部分。 :)

【讨论】:

  • 您还可以在一个回显中在多行上回显 HTML 内容,而不是在每个新行上都使用回显。使其更具可读性。
  • @Sven 你能举例说明你的意思吗?
【解决方案2】:
<table border='1' cellpadding='10'>
<tr>  
    <th>Product Name</th> 
    <th>Product Description</th> 
    <th>Product Price</th>
    <th>Product Image</th>
    <th>View Product Details</th>
</tr>

    while($row = mysql_fetch_array( $result )) {

        <tr>
            <td> <?php echo $row['Product_Name']; ?></td>
            <td><?php echo $row['Product_Description']; ?></td>
            <td><?php $row['Product_Price']; ?></td>
            <td><a href="print_pic.php">Picture Enlarge</a></td>
            <td><a href="phone1.php?id="<?php echo $row['tableRowId']; ?>">View Details</a></td>
        </tr>

    } 
</table>

我可能会去做类似的事情。这假设您的表返回一个唯一的 id,例如自动递增的列。

这不包括任何转义,因此可能容易受到利用。我也会研究模板。它可以帮助您进行演示,让您更轻松地阅读混合的 html 和 php。

【讨论】:

  • 您好,我刚刚尝试在我的代码中实现这一点,但是,不是所有的 / 东西都是 HTML 吗?因为当我将它放入我的 php 文件时,它无法识别代码。当我将它从 php 中取出并放入 HTML 部分时,它无法识别 while 循环。
【解决方案3】:
"<td align=\"center\" valign=\"top\"><a href=\"$prevMonth\">&lt;&lt;</a>")  . "</td>\n"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2021-03-31
    • 2012-08-27
    相关资源
    最近更新 更多