0

谁能帮忙?

抱歉,如果我很愚蠢,但我对此很陌生,而且我真的不知道自己在做什么...

我已经生成了一个使用 php 从 mysql 数据库中获取数据的表,但是我想在(每一行的)末尾添加一个额外的列,其中包含一个链接。该链接将指向有关该条目的更多详细信息。

我拥有的显示表格的代码如下:

$result = mysql_query("SELECT * FROM orders");

echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
</tr>";

// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['order_no'] . "</td>";
  echo "<td>" . $row['ord_date'] . "</td>";
  echo "<td>" . $row['est_completion_date'] . "</td>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['invoice_date'] . "</td>";
  echo "<td>" . $row['inv_amount'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['fName'] . "</td>";
  echo "<td>" . $row['lName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

就像我说的,我是初学者。基本上我很高兴(使用上面的代码)制作显示 mysql 查询结果的 html 表。但是,我需要用户能够单击行/单元格以链接到其他表。

非常感谢任何帮助...

4

2 回答 2

1
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<!-- extra column here -->
<th>&nbsp;</th>
</tr>";

while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['order_no'] . "</td>";
    echo "<td>" . $row['ord_date'] . "</td>";
    echo "<td>" . $row['est_completion_date'] . "</td>";
    echo "<td>" . $row['status'] . "</td>";
    echo "<td>" . $row['invoice_date'] . "</td>";
    echo "<td>" . $row['inv_amount'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['fName'] . "</td>";
    echo "<td>" . $row['lName'] . "</td>";
    // add link here
    echo "<td><a href=''>link</a></td>";
    echo "</tr>";
}

请注意:您应该停止使用mysql_*函数。它们正在被弃用。而是使用PDO(从 PHP 5.1 开始支持)或mysqli(从 PHP 4.1 开始支持)。如果您不确定要使用哪一个,请阅读这篇文章

于 2012-08-23T16:52:43.163 回答
0

尝试这个。添加一列。它将链接到另一个页面“view_more_details.php”。要唯一标识行,请使用此链接传递唯一 ID。在这里,我通过了 order_no。在“view_more_details.php”页面中,您可以使用 $_GET['key'] 选择此值

$result = mysql_query("SELECT * FROM orders");

echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<th>&nbsp;</th>
</tr>";

// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['order_no'] . "</td>";
  echo "<td>" . $row['ord_date'] . "</td>";
  echo "<td>" . $row['est_completion_date'] . "</td>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['invoice_date'] . "</td>";
  echo "<td>" . $row['inv_amount'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['fName'] . "</td>";
  echo "<td>" . $row['lName'] . "</td>";
  echo "<td>" . "<a href="view_more_details.php?key=$row['order_no']">View More</a>" . "</td>";
  echo "</tr>";
  }
echo "</table>";
于 2012-08-23T17:14:38.433 回答