添加HTML表格中数据的链接使用PHP和MYSQL数据库创建

时间:2016-01-05 20:29:03

标签: php html mysql phpmyadmin

我正在尝试从PHPmyadmin中的表中提取数据,并根据某些客户表单输入将其转换为HTML表,该表将过滤掉不需要的行。下面的代码就可以了。问题是我的两个列需要包含链接。

使用PHP将表格数据更改为链接,使用strtolower()str_replace()删除空格,然后整合" www.website.com/&会很容易#34;和#34; .html"。但是我使用foreach循环来获取我需要的所有行,而且我不知道如何只改变每行的一个值。

我尝试过使用" Broswer Display Transformations"和"输入转换"在PHPmyadmin中,但这似乎只影响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>";
}

1 个答案:

答案 0 :(得分:0)

只需在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>";