添加指向 HTML table 中数据的链接使用 PHP 和 MYSQL 数据库创建
Add links to data in HTML table Created using PHP and a MYSQL database
我正在尝试从 PHPmyadmin 中的 table 提取数据,并根据一些过滤掉不需要的客户表单输入将其转换为 HTML table行。下面的代码就可以了。问题是我的两个专栏需要包含 links.
使用 PHP 将 table 数据更改为 link 使用 strtolower()
和 str_replace()
删除空格就足够容易了,然后连接 "www.website.com/" 和“.html”。但是我正在使用 foreach 循环来获取我需要的所有行,但我不知道如何只更改每行的一个值。
我曾尝试在 PHPmyadmin 中使用 "Broswer Display Transformations" 和 "Input Transformations",但这似乎只影响 PHPmyadmin 中的数据,而不是在我访问数据时通过 PHP.
我当前的代码:
//* Code for Table
$query = "SELECT $searchFields FROM `hose_reels` $searchPhrase ORDER BY `model` ASC";
$result = mysqli_query($cxn,$query);
if ($row[$key] != "0") {
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
echo "<th>" . $key . "</th>";
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
echo "<td>$row[$key]</td>";
}
echo "</tr>";
}
echo "</table>";
}
else {
echo "<p>No results match your selection. Please broaden your search.</p>";
}
只需在 php 代码中添加 <a>
标签。下面是代码。还有一件事你在 echo "<td>$row[$key]</td>";
行中有错误。它打印 <td>$row[$key]</td>
而不是您从数据库中获取的结果。
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
$i = 1;
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
if($i == 1 || $i ==3){
echo "<th><a href='".key ."'" . $key . "</a></th>";
}else{
echo "<th>" . $key . "</th>";
}
$i++;
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
$j =1;
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
if($i == 1 || $i ==3){
echo "<td><a href='".$row[$key]."'".$row[$key]."</a></td>";
}else{
echo "<td>$row[$key]</td>";
}
}
echo "</tr>";
}
echo "</table>";
我正在尝试从 PHPmyadmin 中的 table 提取数据,并根据一些过滤掉不需要的客户表单输入将其转换为 HTML table行。下面的代码就可以了。问题是我的两个专栏需要包含 links.
使用 PHP 将 table 数据更改为 link 使用 strtolower()
和 str_replace()
删除空格就足够容易了,然后连接 "www.website.com/" 和“.html”。但是我正在使用 foreach 循环来获取我需要的所有行,但我不知道如何只更改每行的一个值。
我曾尝试在 PHPmyadmin 中使用 "Broswer Display Transformations" 和 "Input Transformations",但这似乎只影响 PHPmyadmin 中的数据,而不是在我访问数据时通过 PHP.
我当前的代码:
//* Code for Table
$query = "SELECT $searchFields FROM `hose_reels` $searchPhrase ORDER BY `model` ASC";
$result = mysqli_query($cxn,$query);
if ($row[$key] != "0") {
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
echo "<th>" . $key . "</th>";
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
echo "<td>$row[$key]</td>";
}
echo "</tr>";
}
echo "</table>";
}
else {
echo "<p>No results match your selection. Please broaden your search.</p>";
}
只需在 php 代码中添加 <a>
标签。下面是代码。还有一件事你在 echo "<td>$row[$key]</td>";
行中有错误。它打印 <td>$row[$key]</td>
而不是您从数据库中获取的结果。
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
$i = 1;
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
if($i == 1 || $i ==3){
echo "<th><a href='".key ."'" . $key . "</a></th>";
}else{
echo "<th>" . $key . "</th>";
}
$i++;
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
$j =1;
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
if($i == 1 || $i ==3){
echo "<td><a href='".$row[$key]."'".$row[$key]."</a></td>";
}else{
echo "<td>$row[$key]</td>";
}
}
echo "</tr>";
}
echo "</table>";