vendredi 3 mars 2017

php mysql if else update database and display different links based on database

I am learning php and I am trying to add some buttons that modify the status of a project. I managed to make my code do this and it's working but I am sure it can be written better and I wonder if this can be done without refreshing the page? Any ideas will be appreciated. Thanks.

if (isset($_GET['status'])) {
$id = (int)$_GET['id'];
$status = (int)$_GET['status'];
$statusSql = "UPDATE projects SET status = '$status' WHERE id ='$id'";
$db->query($statusSql);
header('Location: projects.php');
}
........
<?php
      if(($project['status']) == 0 ) {
         echo "<a href='projects.php?status=1&id={$project['id']}' class='btn btn-xs btn-default'>
           <span class='glyphicon glyphicon-play'></span>
           </a>&nbsp New Project";
      }elseif(($project['status']) == 1 ){
         echo "<a href='projects.php?status=2&id={$project['id']}' class='btn btn-xs btn-default'>
          <span class='glyphicon glyphicon-pause'></span>
          </a>&nbsp In Progress &nbsp<a href='projects.php?status=3&id={$project['id']}' class='btn btn-xs btn-default'>
           <span class='glyphicon glyphicon-stop'></span>
           </a>";
      }elseif(($project['status']) == 2 ){
        echo "<a href='projects.php?status=1&id={$project['id']}' class='btn btn-xs btn-default'>
         <span class='glyphicon glyphicon-play'></span>
         </a>&nbsp On Hold</span>
          </a>";
      }elseif(($project['status']) == 3 ){
        echo "<a href='projects.php?status=1&id={$project['id']}' class='btn btn-xs btn-default'>
         <span class='glyphicon glyphicon-play'></span>
         </a>&nbsp Finished</span>
          </a>";
      };

?>

Aucun commentaire:

Enregistrer un commentaire