Thanks to many of you here I have the script below wich lists out the contents of a mysql table. It also allows me to sort both ascending and descending. I have also made update and delete links in the far right column.
Next step I would like to do is to let me delete several rows at a time with checkboxes. More spesifically to delete all the checked rows.
Im thinking I will need the script to check wich rows are marked/checked for deletion and have an global delete button/link ? Problem is I don't know where to put it or how to run a check for this.
Any ideas are greatly apreciated.
Here is the script so far.
<?php echo "<a href=./>Home</a> | <a href=insert.php>Insert</a><br>";
// connect to db
$connect = mysqli_connect('localhost','user','pass','db');
// set the default sort
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id';
if(isset($_GET['sort_by'])) {
if($_GET['sort_by'] == 'asc') {
$sort_order = 'desc';
} else {
$sort_order = 'asc';
}
}
// html table
echo "<table border='1'>";
// query mysql table column keys
$query_keys = mysqli_query($connect,("SELECT * FROM pdb"));
$fetch_keys = mysqli_fetch_array($query_keys, MYSQLI_ASSOC);
// print out table columns keys
echo "<tr>";
foreach($fetch_keys as $key => $value) {
switch($sort) {
case $key :
$order_by = $key;
break;
}
if($sort==$key) {
echo "<td bgcolor=lightgreen><a href='?sort=$key&sort_by=" . $sort_order . "'>$key</a></td>";
} else {
echo "<td bgcolor=lightblue><a href='?sort=$key&sort_by=" . $sort_order . "'>$key</a></td>";
}
}
echo "<td>Update | Delete</tr>";
echo "</tr>";
// free query keys
mysqli_free_result($query_keys);
// pagination
$rows_per_page = 20;
$query_rows = mysqli_query($connect, "SELECT id FROM pdb");
$num_rows = mysqli_num_rows($query_rows);
$num_of_pages = ceil($num_rows / $rows_per_page);
$current_page = 1;
if (isset($_GET['current_page']) && $_GET['current_page'] >= 1 && $_GET['current_page'] <= $num_of_pages) {
$current_page = (int)$_GET['current_page'];
}
$offset = ($current_page -1) * $rows_per_page;
mysqli_free_result($query_rows);
// query mysql table content
$query_content = mysqli_query($connect,("SELECT * FROM pdb ORDER BY $sort $sort_order LIMIT $offset, $rows_per_page"));
// print out table contents
while($row = mysqli_fetch_array($query_content, MYSQLI_NUM)) {
echo "<tr>";
foreach($row as $rows) {
echo "<td>" . $rows . "</td>";
}
echo "<td><a href=update.php?id=$row[0]>Update</a> | <input type='checkbox' name='list[]' value='$row[0]'><a href=shure.php?id=$row[0]>Delete</a></td>";
echo "</tr>";
}
// free query content
mysqli_free_result($query_content);
echo "</table>";
// echo pagination
for ($i = 1; $i <= $num_of_pages; ++$i) {
if($i==$current_page) {
echo "<a href='?current_page=$i'><font color=red>$i</font></a> ";
}
else {
echo "<a href='?current_page=$i'>$i</a> ";
}
}
// close connection
mysqli_close($connect);
?>