<?php
include 'dbconnect.php';

if(isset($_GET['type']) && $_GET['type']!=''){
  $type = $_GET['type'];

  if($type=='delete'){ 
      $id = $_GET['id'];
      $delete_sql = "delete from news where id='$id'";
      mysqli_query($con,$delete_sql);
  }
}

if(isset($_POST['submit'])){
  $desc = $_POST['description'];
  
  $sql = "insert into news(description) values('$desc')";
  $res = mysqli_query($con,$sql);
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Document</title>
</head>
<body>
<?php require "navbar.php"; ?>
<div class="container">

  <div class="my-4">
    <h4>News Form</h4>
    <form method="post">
        <div class="form-group">
            <label>Description</label>
            <textarea name="description" class="form-control" cols="30" rows="5"></textarea>
        </div>
        <div class="form-group">
            <button class="btn btn-primary" name="submit">Submit</button>
        </div>
    </form>
</div>
<div class="container-fluid my-3">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Description</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php
                $sql = "select * from news order by id desc";
                $result = mysqli_query($con,$sql);
                while($row1=mysqli_fetch_assoc($result)){
            ?>
            <tr>
                <td><?php echo $row1['description'] ?></td>
                <td class="text-right">
                    <?php 
                    echo "<a class='btn btn-danger' href='?type=delete&id=".$row1['id']."'>Delete</a>";
                    ?>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
</div>
</body>
</html> 