带有单独列的PHP / HTML表

时间:2014-01-05 02:42:50

标签: php html mysql

我正在尝试在PHP / HTML中构建一个表,其中数据从数据库中提取并显示在网站上...我唯一的问题是我希望每个列都是独立的并且有自己的搜索功能当用户点击某个字段时,我希望其他列反映相同的行信息,如下所示: 我的问题是:我怎么能做这样的事情?我试过把它分成不同的表然后加入它们但是没有用。

链接示例:

enter image description here

我正在使用的代码示例:

<?php
    $con=mysqli_connect("example.com","peter","abc123","my_db");
    // Check connection
    if (mysqli_connect_errno())
    {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM Persons");
    echo "<table border='1'>
    <tr>
         <th>Firstname</th>
         <th>Lastname</th>
    </tr>";
    while($row = mysqli_fetch_array($result))
    {
      echo "<tr>";
        echo "<td>" . $row['FirstName'] . "</td>";
        echo "<td>" . $row['LastName'] . "</td>";
      echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
?> 

1 个答案:

答案 0 :(得分:1)

要使用不同的信息填充不同的表,请尝试....好吧,假设您有3个表要显示数据。

  

[tbl1 / tbl2 / tbl3]

首先设置3个php变量,将查询保存到数据库中。 (伪代码)(给你一个想法)

$tblData1 = Select query here;
$tblGETdata1 = mysqli_query( your connect var here, $tblData1 )

$tblData2 = Select query here;
$tblGETdata2 = mysqli_query( your connect var here, $tblData3 )

$tblData3 = Select query here;
$tblGETdata3 = mysqli_query( your connect var here, $tblData3 )

然后设置while循环,以查找上述任何一项的数据。 例如:

while($row = mysqli_fetch_array(tblGETdata1 )){
//output your echoed table content here;
}


while($row2 = mysqli_fetch_array(tblGETdata2 )){
//output your echoed table content here;
}



while($row3 = mysqli_fetch_array(tblGETdata3 )){
//output your echoed table content here;
}

或者如果你不想弄乱表格的回声,你可以像这样构建它:

<?php

while($row = mysqli_fetch_array(tblGETdata3 )){ ?>


<!-- put your normal HTML here. For example if your fetch has a first name you can do -->
<div class="firstName">This persons first name is: <?php echo $row['firstName']; ?> </div>

<?php
   }
?>

你当然喜欢这样做你想要的效果。

希望这适合你。