Realcookie
Realcookie

Reputation: 39

PHP MySQL display data with table rows (clickable)

I have tables that are the publish date of a article and a columns article summary, and those are clickable and should go to a edit page when you click on this. The article should be filled in when you click on it, so you can easy edit the text. I have make the table rows linked but I think I need to take the ID from the tables so it knows what the edit page should be filled in with.

Here is my current code:

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

Upvotes: 2

Views: 5143

Answers (2)

Junius L
Junius L

Reputation: 16122

Add article id to your link ?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>

Screenshot enter image description here

Upvotes: 1

Alive ColdJuan
Alive ColdJuan

Reputation: 50

You need to pass the id to next page so what you need to do is add a id to url like below

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

and get the id on bewerkartikel page using

$id = $_GET['id'];

and then you get the particular id to edit.

Upvotes: 1

Related Questions