javascript - 使用 HTML 和 PHP 的动态表

标签 javascript php mysqli html-table

我向您展示我的以下问题..我基本上有以下数据库..为了理解它我认为一个实际的例子是最好的方法..

我有一个建筑工地,其中 N 位委托(delegate)人可以进行干预,这 N 位受托人可以依次委托(delegate) N 个子附属机构。 然后求和我在这三个表之间有一些关系(用我的语言是“Cantiere”“Affidatria”“SubAffidatria”)

建筑工地 1 ------> N 可靠

关联 1 -----> N 子分支机构

话虽如此,我的问题是以下我必须做一个控制面板,在其中我必须为每个建筑工地展示护理人员和分包商。 我想指出的是,子附属公司与造船厂没有“直接”联系,但他们拥有附属公司的 FK

它的结构必须如下...

在 Each Relevant ("Affidataria") 下必须放置 $ sql1 查询的结果 在每个子公司(“SubAffidatari”)下,必须放置 $ sql2 查询的结果。

问题是如何按列划分所有内容? 我尝试创建一个动态 HTML 表格..但它不起作用..

我附上我的证明......

<!DOCTYPE html>
<?php 
    session_start();
    if(!isset($_SESSION["username"])){
        header('location: ../index.php');
    }else
    {

?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_progettocantiere";


$idCantiere = $_GET["idCantiere"]; 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

    echo'<h1 align="center">' . $row["codiceCommessa"] . '-' . $row["nomeCantiere"] . '</h1>';
    }


} else {
    echo "0 results";
}
$conn1 = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn1->connect_error) {
    die("Connection failed: " . $conn1->connect_error);
} 

$sql1 = "SELECT idAffidataria,nomeCantiere FROM Affidataria WHERE fkIDCantiere = '$idCantiere'";
$result1 = $conn1->query($sql1);


//echo "<script type='text/javascript'>alert('$idAffidataria');</script>";

if ($result1->num_rows > 0)
    {
    // output data of each row
    while($row1 = $result1->fetch_assoc()) 
    {



        echo'<br><br><br>';
        echo '<h1> Affidataria</h1>';
       echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row1["idAffidataria"] . '">' . $row1["nomeCantiere"] . '</a>';
       echo '<h1> SubAffidatario</h1>';
        $idAffidataria = $row1["idAffidataria"];

        $conn2 = new mysqli($servername, $username, $password, $dbname);
// Check connection
    if ($conn2->connect_error) 
        {
    die("Connection failed: " . $conn2->connect_error);
        } 

    $sql2 = "SELECT * FROM SubAffidatario WHERE fkIdAffidataria = '$idAffidataria'";
    $result2 = $conn2->query($sql2);
    if ($result2->num_rows > 0)
        {
    // output data of each row
        while($row2 = $result2->fetch_assoc()) 
        {

      //  echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row1["idAffidataria"] . '">' . $row1["nomeCantiere"] . '</a>';
        echo'<br><br><br>';
        echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row2["ragioneSocialeSubAffidatario"] . '">' . $row2["ragioneSocialeSubAffidatario"] . '</a>';
        }
    //echo'<br><br><br>';
    //echo '<a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

        } 
        else {
        echo "0 results";
        }

    }
    //echo'<br><br><br>';
    //echo '<a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

    } 
    else
    {
    echo "0 results";
    }


$conn->close();

$conn1->close();


$conn2->close();


?>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Progetto Cantiere</title>
    <!-- Bootstrap -->
    <link href="../paginaIniziale/css/bootstrap-4.0.0.css" rel="stylesheet">
  </head>
    <style>

    .myButton {
    -moz-box-shadow:inset 0px 1px 0px 0px #97c4fe;
    -webkit-box-shadow:inset 0px 1px 0px 0px #97c4fe;
    box-shadow:inset 0px 1px 0px 0px #97c4fe;
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #3d94f6), color-stop(1, #1e62d0));
    background:-moz-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:-webkit-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:-o-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:-ms-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:linear-gradient(to bottom, #3d94f6 5%, #1e62d0 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#3d94f6', endColorstr='#1e62d0',GradientType=0);
    background-color:#3d94f6;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
    border:1px solid #0f73ff;
    display:inline-block;
    cursor:pointer;
    color:#ffffff;
    font-family:Arial;
    font-size:28px;
    font-weight:bold;
    padding:32px 76px;
    text-decoration:none;
    text-shadow:0px 1px 0px #1570cd;
}
.myButton:hover {
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #1e62d0), color-stop(1, #3d94f6));
    background:-moz-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:-webkit-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:-o-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:-ms-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:linear-gradient(to bottom, #1e62d0 5%, #3d94f6 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#1e62d0', endColorstr='#3d94f6',GradientType=0);
    background-color:#1e62d0;
}
.myButton:active {
    position:relative;
    top:1px;
}

    </style>

  <body>


  </body>
</html>
<?php } ?>

这是我得到的结果:

enter image description here

最佳答案

试试下面的代码..

<!DOCTYPE html>
<?php 
    session_start();
    if(!isset($_SESSION["username"])){
        header('location: ../index.php');
    }else
    {

?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_progettocantiere";


$idCantiere = $_GET["idCantiere"]; 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

    echo'<h1 align="center">' . $row["codiceCommessa"] . '-' . $row["nomeCantiere"] . '</h1>';
    }


} else {
    echo "0 results";
}
$conn1 = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn1->connect_error) {
    die("Connection failed: " . $conn1->connect_error);
} 

$sql1 = "SELECT * FROM Affidataria WHERE fkIDCantiere = '$idCantiere'";
$result1 = $conn1->query($sql1);


//echo "<script type='text/javascript'>alert('$idAffidataria');</script>";

if ($result1->num_rows > 0)
    {
    // output data of each row
    while($row1 = $result1->fetch_assoc()) 
    {



        echo '<table align="center" border="3px" width = "900px">';

    echo '<tr>';
        echo '<td><h1>Anagrafica</h1>';
        echo '<td><h1> Affidataria</h1></td>';
        echo '<td><h1> SubAffidatario</h1></td></tr>';
        echo '<tr><td><a class="myButton" href="../../pagineHtml/modifica/modifica/inserimentoCantiere.php?idCantiere=' . $row1["fkIDCantiere"] . '">Anagrafica</a></td>';
        echo '<td><a class="myButton" href="../../pagineHtml/modifica/modifica/affidatario.php?idAffidataria=' . $row1["idAffidataria"] . '">' . $row1["nomeCantiere"] . '</a></td></tr>';

        $idAffidataria = $row1["idAffidataria"];

        $conn2 = new mysqli($servername, $username, $password, $dbname);
// Check connection
    if ($conn2->connect_error) 
        {
    die("Connection failed: " . $conn2->connect_error);
        } 

    $sql2 = "SELECT * FROM SubAffidatario WHERE fkIdAffidataria = '$idAffidataria'";
    $result2 = $conn2->query($sql2);
    if ($result2->num_rows > 0)
        {
    // output data of each row
        while($row2 = $result2->fetch_assoc()) 
        {

      //  echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row1["idAffidataria"] . '">' . $row1["nomeCantiere"] . '</a>';
        echo'<br><br><br>';
        echo '<tr>';
        echo '<td></td>';
        echo '<td></td>';
        echo '<td><a class="myButton" href="../../pagineHtml/modifica/modifica/subAffidatario.php?idSubAffidatario=' . $row2["idSubAffidatario"] . '">' . $row2["ragioneSocialeSubAffidatario"] . '</a></td>';
        }
    //echo'<br><br><br>';
    //echo '<a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

        } 
        else {
        echo "0 results";
        }

    }
    //echo'<br><br><br>';
    //echo '<a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

    } 
    else
    {
    echo "0 results";
    }
    echo'</tr>';
    echo '</table>';

$conn->close();

$conn1->close();


$conn2->close();


?>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Progetto Cantiere</title>
    <!-- Bootstrap -->
    <link href="../paginaIniziale/css/bootstrap-4.0.0.css" rel="stylesheet">
  </head>
    <style>

    .myButton {
    -moz-box-shadow:inset 0px 1px 0px 0px #97c4fe;
    -webkit-box-shadow:inset 0px 1px 0px 0px #97c4fe;
    box-shadow:inset 0px 1px 0px 0px #97c4fe;
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #3d94f6), color-stop(1, #1e62d0));
    background:-moz-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:-webkit-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:-o-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:-ms-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
    background:linear-gradient(to bottom, #3d94f6 5%, #1e62d0 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#3d94f6', endColorstr='#1e62d0',GradientType=0);
    background-color:#3d94f6;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
    border:1px solid #0f73ff;
    display:inline-block;
    cursor:pointer;
    color:#ffffff;
    font-family:Arial;
    font-size:28px;
    font-weight:bold;
    padding:32px 76px;
    text-decoration:none;
    text-shadow:0px 1px 0px #1570cd;
}
.myButton:hover {
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #1e62d0), color-stop(1, #3d94f6));
    background:-moz-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:-webkit-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:-o-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:-ms-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
    background:linear-gradient(to bottom, #1e62d0 5%, #3d94f6 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#1e62d0', endColorstr='#3d94f6',GradientType=0);
    background-color:#1e62d0;
}
.myButton:active {
    position:relative;
    top:1px;
}

    </style>

  <body>


  </body>
</html>
<?php } ?>

关于javascript - 使用 HTML 和 PHP 的动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53419986/

相关文章:

javascript - Chunk.entrypoints : Use Chunks. groupsIterable 并通过 instanceof Entrypoint 过滤

javascript - jQuery SlideDown 和 Border - 奇怪的跳转

javascript - 使用javascript动态生成文本字段的日期选择器

php - 从我的站点登录到 gmail

php - 对于大数据集,哪个更快? mysql、mysqli 还是 PDO?

php - mysqli_query 返回不满足条件

javascript - 嵌入youtube视频+ noscript?

php - 使用 php 计算 Sql 表行并在 HTML 中显示

php - SQL 错误 : Returned value is boolean?

php - 将 href 传递并签名到 php 页面帮助