PHP MySQL显示表行的数据(可点击)

时间:2017-04-09 10:41:36

标签: php html mysql

我有表格是文章的发布日期和列文章摘要,这些表格是可点击的,当您点击它时应该转到编辑页面。单击该文章时应填写该文章,以便轻松编辑文本。我已经将表格行链接起来,但我认为我需要从表格中获取ID,以便它知道编辑页面应填写的内容。

这是我目前的代码:

<?php
include 'index.php';
include 'login2.php';
?>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="alleartikelen.css">
    </head>

    <body>
        <h2> Widget news admin </h2>
        <!-- Echo username -->
        <?php
        $user = $_SESSION['user'];
        echo "<p>You are logged in as <b>$user</b>.</p>"; 
        ?>
        <a class="logout" href="index.php">Log out </a>
        <hr>

        <h1> All Articles </h1>
        <?php 


            $sql = "SELECT art_date, art_head FROM article";
            $result = $conn->query($sql); //it makes the query

        ?>

        <style>
        .table td {
    font-family: Merriweather, serif;
    font-size: 16px;
    padding: 10px 5px;
    overflow: hidden;
    word-break: normal;
}

    .table th:first-child,
     .table td:first-child {
    width: 14%;
    height: 10%;
    }
     td,th{
    font-family: "Calibri ";
    }

     tr:nth-child(even){
    background-color: #ddd;
    }

    tr:hover {
        background-color: #F6F6F6;
    }

    th{
        color:white;
        }

        .aa {
    text-decoration: none;
    color: inherit;
     }
        </style>
      <section>   
                <div class="container">  
                    <table class="table">   
                    <tr>     
                        <th>Publication date</th>   
                        <th>Article</th>   
                    </tr>   
      <?php 
      $res=$result->fetch_all(MYSQL_ASSOC); //Splitting all rows in arrays
      $keys = array_keys($res[0]); //Arrays getting attached to $keys
      // Make the results as rows
      echo "<tbody>";
      foreach($res as $rows)
      {
      echo "<tr>";
        foreach($rows as $date)
      {
      echo "<td><a href='bewerkartikel.php'class ='aa'>$date</a>";
      echo "</td>";
     }
     echo "</tr>";
     }
     echo "</tr>";                  


}

?>




</section>  
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

您需要将ID传递到下一页,因此您需要做的是向网址添加ID 如下所示

"<td><a href='bewerkartikel.php?id=".$date['id']." ' class='aa' >$date</a></td>";

并使用

获取bewerkartikel页面上的ID
$id = $_GET['id'];

然后您将获得要编辑的特定ID。

答案 1 :(得分:1)

将文章id添加到您的链接?art_id=some_id

<?php
    include 'index.php';
    include 'login2.php';

?>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="alleartikelen.css">
    <style>
            .table td {
               font-family: Merriweather, serif;
               font-size: 16px;
               padding: 10px 5px;
               overflow: hidden;
               word-break: normal;
            }

            .table th:first-child,
            .table td:first-child {
               width: 14%;
               height: 10%;
            }
            td,th {
               font-family: "Calibri ";
            }

            tr:nth-child(even){
               background-color: #ddd;
            }

            tr:hover {
                background-color: #F6F6F6;
            }

            th {
                color:white;
             }

            .aa {
                text-decoration: none;
                color: inherit;
            }
     </style>
</head>
<body>
    <h2> Widget news admin </h2>
    <!-- Echo username -->
    <?php
        $user = $_SESSION['user'];
        echo "<p>You are logged in as <b>$user</b>.</p>"; 
    ?>
    <a class="logout" href="index.php">Log out </a>
    <hr>
    <h1> All Articles </h1>
    <?php 
        $sql = "SELECT art_date, art_head, art_id FROM article"; //get article id too
        $result = $conn->query($sql); //it makes the query
     ?>
     <section>   
                    <div class="container">  
                        <table class="table">   
                        <tr>     
                            <th>Publication date</th>   
                            <th>Article</th>   
                        </tr>   
    <?php 

    echo "<tbody>";
      while($rows = $result->fetch_assoc()) {
        echo "<tr>";

            echo "<td>". $rows['art_date'] . "</td>";
            echo "<td><a href='bewerkartikel.php?art_id=" . $rows['id'] . "' class ='aa'> " . $rows['art_head'] . "</a>";
            echo "</td>";

        echo "</tr>";
     }
        echo "</tr>";  

    ?>
    </section>  
        </body>
    </html>

截图 enter image description here