2017-04-09 41 views
1

我有一篇文章和一篇文章摘要的發佈日期的表格,這些表格是可點擊的,並且當您點擊它時應該進入編輯頁面。該文章應該在您點擊時填寫,以便您輕鬆編輯文本。我已經建立了表格行,但我認爲我需要從表格中獲取ID,以便知道編輯頁面應該填充的內容。PHP MySQL與表格行(可點擊)顯示數據

這裏是我當前的代碼:

<?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> 
+0

你需要一個id/CSS添加到你想成爲可編輯的每個單元/容器。使用JavaScript,您需要爲每個可編輯的單元格/容器開發一個onclick監聽器。 Onclick顯示一個輸入字段或textarea。你可以使用一個id或css類。你需要的是一個相應的事件監聽器。儘管ID使事情變得更容易。 – AMartinNo1

+0

你能告訴我怎麼樣?因爲我是初學者,不知道如何去做一些事情。 :/ – Realcookie

+0

w3schools通常是一個好的開始:https://www.w3schools.com/js/js_htmldom_eventlistener.asp – AMartinNo1

回答

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

+0

它給了我一個錯誤:注意:未定義的變量:res中的C:\ xampp \ htdocs \ dtt \ alleartikelen.php在線77 警告:無效的參數爲foreach \ xampp \ htdocs \ dtt \ alleartikelen.php on line 77 – Realcookie

+0

@Realcookie您使用的是「PDO」嗎? – julekgwa

+0

將$ res更改爲$ result後,只出現此錯誤:警告:提供的無效參數爲foreach()在C:\ xampp \ htdocs \ dtt \ alleartikelen.php在線77 – Realcookie

1

您需要通過id來翻頁,所以你需要做的就是添加一個ID什麼網址 像下面

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

,並得到在bewerkartikel頁上使用的編號

$id = $_GET['id']; 

然後你得到特定的編輯id。

+0

獲取有關錯誤字符串的警告 – Realcookie