This question already has answers here:
Output a php multi-dimensional array to a html table
(3 answers)
Closed 3 years ago.
I need to create REST API in PHP. I take JSON data from https://jsonplaceholder.typicode.com/ . Now I need to put all that data in table HTML. It is one array, inside with other 200 array. How can i create all row automaticly, without typing whole code by hands.
This is the code how i take data from https://jsonplaceholder.typicode.com/ :
<?php
$url = 'https://jsonplaceholder.typicode.com/todos/';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
$result = curl_exec($cURL);
curl_close($cURL);
$arrays = json_decode($result);
?>
I tried like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSON</title>
<link rel="shortcut icon" type="image/x-icon" href="icon.ico">
<meta name="description" content="HTML, PHP, JSON, REST API">
<style>table, th, td {border: 1px solid black;}</style>
</head>
<body>
<?php foreach ($arrays as $key => $value) { ?>
<div style="width:80px;border:2px solid black;height:20px;"> <?php echo 'User ID: ' . $value -> userId ?> </div><br />
<div style="width:50px;border:2px solid black;height:20px;"> <?php echo 'ID: ' . $value -> id ?> </div><br />
<div style="width:550px;border:2px solid black;height:20px;"> <?php echo 'Title: ' . $value -> title ?> </div><br />
<div style="width:90px;border:2px solid black;height:20px;"> <?php echo 'Completed: ' . $value -> completed ?> </div><br />
<br /> <?php } ?>
<table table style="width:100%"; table class="data-table">
<tr>
<th> User ID:</th> <th>ID:</th> <th>Title:</th><th>Completed</th>
</tr>
<tr>
<td><?php echo $value -> userId ?> </td> <td><?php echo $value -> id ?> </td> <td><?php echo $value -> title ?> </td><td><?php echo $value -> completed ?> </td>
</tr>
<tr>
<td><?php echo $value -> userId ?> </td> <td><?php echo $value -> id ?> </td> <td><?php echo $value -> title ?> </td><td><?php echo $value -> completed ?> </td>
</tr>
</table>
</body>
</html>
But i dont know to do it by once for all 200 row. Any suggestion?
use foreach() one more time and put <table> outside of it:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSON</title>
<link rel="shortcut icon" type="image/x-icon" href="icon.ico">
<meta name="description" content="HTML, PHP, JSON, REST API">
<style>table, th, td {border: 1px solid black;}</style>
</head>
<body>
<?php foreach ($arrays as $value) { ?>
<div style="width:80px;border:2px solid black;height:20px;"> <?php echo 'User ID: ' . $value -> userId ?> </div><br />
<div style="width:50px;border:2px solid black;height:20px;"> <?php echo 'ID: ' . $value -> id ?> </div><br />
<div style="width:550px;border:2px solid black;height:20px;"> <?php echo 'Title: ' . $value -> title ?> </div><br />
<div style="width:90px;border:2px solid black;height:20px;"> <?php echo 'Completed: ' . $value -> completed ?> </div><br />
<br />
<?php } ?>
<table style="width:100%"; class="data-table">
<tr>
<th> User ID:</th>
<th>ID:</th>
<th>Title:</th>
<th>Completed</th>
</tr>
<?php foreach ($arrays as $value) { ?>
<tr>
<td><?php echo $value -> userId ?> </td>
<td><?php echo $value -> id ?> </td>
<td><?php echo $value -> title ?> </td>
<td><?php echo $value -> completed ?> </td>
</tr>
<?php } ?>
</table>
</body>
</html>
Note:- I am not sure that you want first foreach() inside <body> or not? so please check and if not needed then remove it.
Related
The code does not display the data.
I try to display the values of each ReportItems in a table.
The code does not display any error when I call it.
Please can someone help me? Thank you very much.
//The code does not display the data.
I try to display the values of each ReportItems in a table.
The code does not display any error when I call it.
Please can someone help me? Thank you very much.
<?php
$url =
"https://api.booker.com/v4.1/merchant/location/.../Report/DailySales?access_token={...}&date_from={...}&date_to={...}";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Ocp-Apim-Subscription-Key: {...}",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
$obj = json_decode($resp, true);
$i = 0;
$nw= array_filter($obj['ReportItems'], function($v,$k){
return in_array($k, ['Date', 'DateOffset', 'Discount', 'Discount', 'GiftCertificate', 'GiftCertificateRefund', 'Orders', 'Refunds', 'Services', 'SubTotal', 'Tax', 'Tip', 'Total']);
}, ARRAY_FILTER_USE_BOTH);
$i=0;
foreach($nw as $k => $v){
$last = $i !== count($nw)-1 ? ','.PHP_EOL : '';
echo "\"$k\": \"$v\"".$last;
$i++;
}
$arrays = json_decode($resp);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSON</title>
<link rel="shortcut icon" type="image/x-icon" href="icon.ico">
<meta name="description" content="HTML, PHP, JSON, REST API">
<style>table, th, td {border: 1px solid black;}</style>
</head>
<body>
<?php foreach ($nw as $k => $v) { ?>
<div style="width:200px;border:2px solid black;height:20px;">
<?php echo 'Date: ' . $v -> Date ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'DateOffset: ' . $v -> DateOffset ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Discount: ' . $v -> Discount ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'GiftCertificate: ' . $v -> GiftCertificate ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'GiftCertificateRefund: ' . $v -> GiftCertificateRefund ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Orders: ' . $v -> Orders ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Refund: ' . $v -> Refund ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Services: ' . $v -> Services ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'SubTotal: ' . $v -> SubTotal ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Tax: ' . $v -> Tax ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Tip: ' . $v -> Tip ?> </div><br />
<div style="width:200px;border:2px solid black;height:20px;"> <?php echo 'Total: ' . $v -> Total ?> </div><br />
<br />
<?php } ?>
<table style="width:100%"; class="data-table">
<tr>
<th>Date:</th>
<th>DateOffset:</th>
<th>Discount:</th>
<th>GiftCertificate:</th>
<th>GiftCertificateRefund:</th>
<th>Orders:</th>
<th>Refund:</th>
<th>Services:</th>
<th>SubTotal:</th>
<th>Tax:</th>
<th>Tip:</th>
<th>Total:</th>
</tr>
<?php foreach ($nw as $k => $v) { ?>
<tr>
<td><?php echo $v -> Date ?> </td>
<td><?php echo $v -> DateOffset ?> </td>
<td><?php echo $v -> Discount ?> </td>
<td><?php echo $v -> GiftCertificate ?> </td>
<td><?php echo $v -> GiftCertificateRefund ?> </td>
<td><?php echo $v -> Orders ?> </td>
<td><?php echo $v -> Refund ?> </td>
<td><?php echo $v -> Services ?> </td>
<td><?php echo $v -> SubTotal ?> </td>
<td><?php echo $v -> Tax ?> </td>
<td><?php echo $v -> Tip ?> </td>
<td><?php echo $v -> Total ?> </td>
</tr>
<?php } ?>
</table>
</body>
</html>
Please can someone help me?
Thank you.
Whenever I click on the add to cart button it just goes to another page instead of showing it in the table
<?php
require "includes/dbh.php";
if(isset($_post['add'])){
if(isset($_SESSION['shopping_cart'])){
This is how I get the data in the html
$item_array_id=array_column($_SESSION['shopping_Cart'],'item_id');
if(!in_array($_GET['id'],$item_array_id)){
$count=count($_SESSION['shopping_cart']);
$item_array=array(
'item_id' =>$_GET['id'],
'item_name'=>$_POST['hidden_name'],
'item_price'=>$_POST['hidden_price'],
'item_quantity'=>$_POST['quantity'],
);
$_SESSION['shopping_cart'][$count]=$item_array;
}else{
echo "<script> alert('item already added')</script>";
echo "window.location='displayitems.php'></script>";
}
}else{
$item_array=array(
'item_id' =>$_GET['id'],
'item_name'=>$_POST['hidden_name'],
'item_price'=>$_POST['hidden_price'],
'item_quantity'=>$_POST['quantity'],
);
$_SESSION['shopping_cart'][0]=$item_array;
}
}
if(isset($_get['action'])){
if($_get['action']=='delete'){
foreach($_SESSION['shopping_cart'] as $keys =>$values){
if($values['item_id']==$_get['id'])
{
unset($_SESSION['shopping_cart'][$keys]);
echo '<script>alert("item removed")</script>';
echo '<script>window.location="displayitems.php</script>';
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
<style>
.product{
border:1px solid
margin:-1px 19px 3px -1px;
padding: 10px;
text-align:center;
bacgkround-color:#efefef;
}
</style>
</head>
<body>
<?php
if(isset($_SESSION['userId'])){
echo '<div id="items" >';
include_once 'includes/dbh.php';
require 'includes/gallery-upload.in.php';
$sql="SELECT * FROM gallery ORDER BY orderitems DESC;";
$stmt=mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt,$sql)){
echo "sql statement failed in displayitems.php";
}else{
mysqli_stmt_execute($stmt);
$result=mysqli_stmt_get_result($stmt);
while($row=mysqli_fetch_assoc($result))
{
?>
This is my form in adding to cart button
<div class="col-md-3">
<form method="POST" action="displayitems.php?action=add&id=<?php
echo $row["idGallery"]?>">
<div class="product">
<img src=images/<?php echo $row['imgFullNameGallery'] ?>
<br>
<h3><?php echo $row['nameitem']?></h3>
<h3><?php echo $row['price']?></h3>
<input type='number' name='quantity' value="1">
<input type='hidden' name='hidden_name' value="<?php echo
$row['nameitem']?>">
<input type='hidden' name='hidden_price' value="<?php
echo
$row['price']?>">
<button type='submit' name='add'>Add to cart</button>
</div>
</form>
</div>
<?php
}
}
}
?>
<div style="clear":both"></div>
<h3 class="title2">Shopping cart details</h3>
<div class="table table-bordered">
<table>
<tr>
<th width=40%>Item Name</th>
<th width=10%>Quantity</th>
<th width=20%>Price</th>
<th width=15%>Total</th>
<th width=5%>Action</th>
</tr>
<?php
if(!empty($_SESSION['shopping_cart'])){
$total=0;
foreach($_SESSION['shopping_cart']as $keys => $values){
?>
<tr>
<td><?php echo $values['item_name'];?></td>
<td><?php echo $values['item_quantity'];?></td>
<td>$<?php echo $values['item_price'];?></td>
<td><?php echo
number_format($values['item_quantity']*$values['item_price'],2);?></td>
<td>REMOVE</span></td>
</tr>
<?php
$total= $total+($values['item_quantity']*$values['item_price']);
}
?>
<tr>
<td coslpan="3" align="right">total</td>
<td align="right"><?php echo number_format($total,2);?></td>
</td></td>
<?php
}
?>
</table>
</div>
</body>
</html>
It doesn't show in the table and the url is just changing to http://localhost/Soft/displayitems.php?action=add&id=1 and the page is blank
because of you using form and the button type is submit so it's expecting a callback function. Regarding you don't have one and you want to display everything in the same page so you have instead to make the submit functionality to a link instead.
Change :
<button type='submit' name='add'>Add to cart</button>
to
<a type='submit' name='add'>Add to cart</a>
or
<input type="submit" value="Add to cart">
here is a mozilla ref
I don't get any errors but I can't update MySQL PHP. I want to change dropdown status from "sedang diproses" dropdown into "Berjaya or Tidak Berjaya". Also I can't change in the database "sedang diproses" into "Berjaya or Tidak Berjaya"
This is my Code Html
<?php
session_start();
include ('include/myFunction.php');
require('include/connect.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<link href="css/myStyle.css" rel="stylesheet" type="text/css" />
<link href="css/myStyle1.css" rel="stylesheet" type="text/css" />
<!-- Button to open the modal login form -->
<link href="css/myStyle3.css" rel="stylesheet" type="text/css" />
<body>
<div align="right">
<a href="logout.php" ><img src="img/logout.png" width="63" height="46" /></a>
</div>
<div class="header" align="left"><img src="img/gmbar.PNG" width="223" height="126" /><!-- end .header --><!-- end .header --></div>
<div class="content" style="font:Verdana, Geneva, sans-serif">
<div class="content" style="font:Verdana, Geneva, sans-serif">
<div class="topnav" id="myTopnav" style="font-family:Verdana, Geneva, sans-serif">Kembali
</div>
<p> </p>
<?php if($_SESSION['namaadmin'] ==null)
{
header('location:index.php');
}
else
{
?>
<form action="prosesstatus.php" method="post">
<table width="1002" height="87" border="0" align="center" bgcolor="#CCCCFF">
<tr bgcolor="#00CCFF">
<td width="30"><strong>Bil</strong></td>
<td width="298"><strong>Nama</strong></td>
<td width="176"><strong>Nama Kursus</strong></td>
<td width="150"><strong>Tarikh Daftar</strong></td>
<td width="150"><strong>Status</strong></td>
<td width="168"><strong>Kemaskini Status</strong></td>
<td width="131"><strong>Pilihan</strong></td>
</tr>
<?php
$namakursus = '';
if( isset( $_GET['namakursus'])) {
$namakursus = $_GET['namakursus'];
}
$sql1 = "Select * from pemohonan INNER JOIN kursus ON pemohonan.idkursus = kursus.idkursus
INNER JOIN pemohon ON pemohonan.idPemohon = pemohon.idPemohon WHERE kursus.namakursus = '$namakursus' ";
$result1=mysqli_query($dbc,$sql1) or die (mysqli_error());
$i=1;
while($row1 = mysqli_fetch_assoc($result1))
{
?>
<tr>
<td><strong><?php echo $i; ?><input name="idkursus" type="hidden" size="50" value="<?php echo $row1['idkursus'];?>"/><input name="idPemohonan" type="hidden" size="50" value="<?php echo $row1['idPemohonan'];?>"/></strong></td>
<td><strong><?php echo $row1['nama']; ?></strong><input name="id" type="hidden" size="50" value="<?php echo $row1['idPemohon'];?>"/></strong></td>
<td><strong><?php echo $row1['namakursus']; ?></strong></td>
<td><strong><?php echo $row1['tarikhpemohon']; ?></strong></td>
<td><strong><?php echo $row1['status']; ?></strong></td>
<td><select name="status" value="<?php echo $row1['status']; ?>">
<option value="sila pilih">-Sila Pilih-</option>
<option value="Berjaya">Berjaya</option>
<option value="Tidak Berjaya">Tidak Berjaya</option>
</select></td>
<td><input name="btnKemaskini" type="submit" value="Kemaskini" /></td>
</tr>
<?php
$i++;
}
mysqli_close($dbc);
}
?>
</table></form>
<p> </p>
<p> </p>
<div class="footer" style="font-family:Arial, Helvetica, sans-serif">
<?php footertext(); ?>
</div>
</body>
</html>
This is Image
This Is My Image DB
This is my Code Php
<?php
require ('include/connect.php');
$idPemohonan = '';
$idPemohon = '';
$idkursus = '';
if (isset($_GET['idPemohon'],$_GET['idkursus'],$_GET['idPemohonan']))
{
$idPemohon = $_GET['idPemohon'];
$idkursus = $_GET['idkursus'];
$idPemohonan = $_GET['idPemohonan'];
}
$tarikhharini=date('y-m-d');
$status=$_POST['status'];
$sql1 = "UPDATE pemohonan SET idPemohonan ='$idPemohonan' , idPemohon ='$idPemohon' , idkursus ='$idkursus' , tarikhpemohon ='$tarikhharini' , status='$status'
where idPemohonan = '$idPemohonan' and idPemohon = '$idPemohon'";
$result1 = mysqli_query($dbc,$sql1) or die (mysqli_error());
$num_row1 = mysqli_affected_rows($dbc);
echo "<script language=\"JavaScript\">\n";
echo "alert('Status Pemohonan Telah Dihantar!');\n";
echo "window.location='senaraipemohon.php'";
echo "</script>";
mysqli_close($dbc);
?>
Your code is not working because you haven't sent in
<form action="prosesstatus.php" method="post">
Parameters:
$idPemohon = $_GET['idPemohon'];
$idkursus = $_GET['idkursus'];
$idPemohonan =
$_GET['idPemohonan'];
If you want use $_GET link should look like:
prosesstatus.php?idPemohon=value&idkursus=value&idPemohonan=value
Or you should add these parameters in
Input type hidden and change in php to $_POST
I agree with others than you should use MVC for example some kind of framework (Laravel, CodeIgniter)
Or separate as much as possible php and html because your code looks unprofessional.
Your MySQL queries are vulnerable to SQL injection and most likely file
include/connect.php
is not outside public folder which is not good practice to do.
I have already referred this post How to get row ID in button click? but still can't make it load values and I do not know why.I'm pretty sure the id has been properly captured at profile.php. Below is my code:
hanj.php
style type="text/css">
.buttonize {
text-decoration: none;
border: 1px solid #ccc;
background-color: #efefef;
padding: 10px 15px;
-moz-border-radius: 11px;
-webkit-border-radius: 11px;
border-radius: 11px;
text-shadow: 0 1px 0 #FFFFFF;
}
<?php require_once('Connections/conn.php');?>
</style>
<table border="1" cellpadding="5" cellspacing="2" width="600">
<tr>
<th>Vendor Id</th>
<th>Kategori</th>
<th>Nama Vendor</th>
<th>Alamat</th>
<th>Poskod</th>
<th>Bandar</th>
<th>Negeri</th>
<th>No</th>
<th>Email</th>
</tr>
<?php
session_start();
$_SESSION['profile']= $row ['v_id'];
mysql_select_db ($database_conn,$conn);
$query="SELECT v_id,type,companyName,address,code,city,state,contact,email FROM vendor";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "</td><td>";
echo $row['v_id'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>";
echo $row['companyName'];
echo "</td><td>";
echo $row['address'];
echo "</td><td>";
echo $row['code'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
echo $row['state'];
echo "</td><td>";
echo $row['contact'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
print '<center>View</center>';
echo "</td></tr>";
}
?>
profile.php
<?php require_once('Connections/conn.php'); ?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Casado</title>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="http://pingendo.github.io/pingendo-bootstrap/themes/default/bootstrap.css" rel="stylesheet" type="text/css">
<div class="container">
<div class="col-md-12">
<div class="col-md-2"></div>
<div class="col-md-8">
<table class="table table-bordered">
<thead>
<tr>
<th>Pakej</th>
<th>Image</th>
<th>Harga</th>
<th>Vendor Id</th>
</tr>
</thead>
<tbody>
<?php
session_start();
mysql_select_db ($database_conn,$conn);
$vid = $_SESSION['profile'];
$sql = mysql_query("SELECT * from item where v_id = '$vid' ") or die (mysql_error());
while($res = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $res['pakej'] ?></td>
<td><?php echo '<img src="data:image;base64,'.$res['image'].'" class="img-thumbnail">' ?></td>
<td><?php echo $res['harga'] ?></td>
<td><?php echo $res['v_id'] ?></td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
<div class="col-md-2"></div>
</div>
</div>
</body>
</html>
btw there is no syntax error
Instead of $_SESSION['profile'] use $_REQUEST['id'] in your profile.php and it should work.
In profile.php use $_GET to reference the passed in 'id'. i.e
Replace the following line
$vid = $_SESSION['profile'];
with this one.
$vid = $_GET['id'];
Hope this helps.
this question is answered by Bikash Paul above where he suggest to change $_SESSION['profile'] to $_REQUEST['id']
Instead of $vid = $_SESSION['profile']; try $vid = $_GET['profile'];, if not works, then print_r($_GET) or print_r($_REQUEST) & show us the details
I need to insert a timestamp into table visitors column out after the user clicks on the sign-out button. The sign-out button already deletes rows in the table liveroster but its not updating rows in table visitors at column out with the timestamp.
mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);
<?php
/*
Connection Stuff
*/
if (isset($_POST['ID'])) {
$ID = $_POST['ID'];
if (isset($_POST['delete_id'])) {
mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);
mysql_query("DELETE FROM liveroster WHERE ID = " . $ID);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<tr>
<td></td>
<td><strong>Time</strong></td>
<td><strong>Name</strong></td>
<td><strong>Teacher</strong></td>
<td><strong>Reason</strong></td>
</tr>
<?php
$data = mysql_query("SELECT * FROM liveroster") or die(mysql_error());
while($info = mysql_fetch_array($data)){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="ID" value="<?php echo $info['ID']; ?>" />
<input type="submit" name="delete_id" value="Sign-Out" />
</form>
</th>
<td>
<?php echo $info['timestamp']; ?>
</td>
<td>
<?php echo $info['name']; ?>
</td>
<td>
<?php echo $info['teacher']; ?>
</td>
<td>
<?php echo $info['reason']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
Use PDO:
<?php
$dsn = "mysql:host=localhost;port=$port;dbname=$dbname";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
/*
Connection Stuff
*/
if (isset($_POST['ID'])) {
$ID = $_POST['ID'];
if (isset($_POST['delete_id'])) {
/* Update */
$count = $dbh->exec("UPDATE `visitors` SET `out` = 'test' WHERE `ID` = '" . $ID ."'");
/* Delete */
$count = $dbh->exec("DELETE FROM `liveroster` WHERE `ID` = '" . $ID ."'");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<tr>
<td></td>
<td><strong>Time</strong></td>
<td><strong>Name</strong></td>
<td><strong>Teacher</strong></td>
<td><strong>Reason</strong></td>
</tr>
<?php
$sql = "SELECT * FROM liveroster";
$sth = $dbh->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
foreach($rows as $value){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="ID" value="<?php echo $value['ID']; ?>" />
<input type="submit" name="delete_id" value="Sign-Out" />
</form>
</th>
<td>
<?php echo $value['timestamp']; ?>
</td>
<td>
<?php echo $value['name']; ?>
</td>
<td>
<?php echo $value['teacher']; ?>
</td>
<td>
<?php echo $value['reason']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
You could create a column and update it using the "NOW()" function every time you click submit.
UPDATE visitors
SET out = test, getdate = NOW()
WHERE ID = $ID