Table from SQL query with header - php

I have created a table from a SQL query and displayed it in the same order as they appear in the table. (Table A in image).
This is working okay.
However it would be great if the data could be clubbed under the member category. As in Table B in image.
SQL Query ...
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)){
$category[] = trim($result['category']);
$name[] = trim($result['f_name']).' '.trim($result['l_name']);
$memid[] = trim($result1['memid']);
$addr[] = trim($result['addr']);
$phone[] = trim($result['phone']);
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Mem ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
for ($i=0; $i<=$row-1; $i++){ ?>
<tr>
<td><?php echo $category[$i]; ?></td>
<td><?php echo $memid[$i]; ?></td>
<td><?php echo $name[$i]; ?> </td>
<td><?php echo $addr[$i]; ?> </td>
<td><?php echo $phone[$i]; ?> </td>
</tr>
<?php }
} ?>
</table>

A little update of your code souhld do it:
$categories = [];
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)) {
$result_category = trim($result['category']);
if (!isset($categories[$result_category])) {
$categories[$result_category] = [];
}
$new = [];
$new['category'] = $result_category;
$new['name'] = trim($result['f_name']).' '.trim($result['l_name']);
$new['memid'] = trim($result['memid']);
$new['addr'] = trim($result['addr']);
$new['phone'] = trim($result['phone']);
$categories[$result_category][] = $new;
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Name</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
foreach ($categories as $category_name => $data){ ?>
<tr>
<td><?php echo $category_name; ?></td>
<td></td>
<td></td>
</tr>
<?php foreach ($data as $row) {?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['name']; ?> </td>
<td><?php echo $row['phone']; ?> </td>
</tr>
<?php }
}
} ?>
</table>

While loop though the data mark on array with group of category name and then print the result
$row = mysqli_num_rows($sql);
if($row > 0) {
$mainArray = [];
while ($result = mysqli_fetch_assoc($sql)){
$category = $result['category'];
if(isset($mainArray[$category])){
$mainArray[$category][] = $result;
} else {
$mainArray[$category] = $result;
}
}
}
foreach($mainArray as $cateName => $data){ ?>
<tr>
<td style="text-align:left"><?php echo $cateName; ?></td>
<td></td>
<td></td>
</tr>
<?php
foreach($data as $row){ ?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['f_name'].' '.$row['l_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
<?php } ?>
}
?>

Related

PHP how to delete a specific row from database with html table button

I am trying to delete a specific entry from the database with a button. I know this has already been asked several times, unfortunately the solutions don't really work for me. The goal would be, if I click on the button in the 3rd row, that this line is deleted. I have the problem that I always only delete the last ids or all ids at once.
Maybe someone can help me, thank you.
admin.php
<?php
include('connection.php');
include('read.php');
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
if (isset($_POST['delete'])) {
echo $row['ID'];
$deleteQuery = "DELETE FROM card WHERE id = " . $row['ID'];
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
?>
</table>
</div>
</div>
</form>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>
You can archive this by using get request without posting whole data to server.
if( !empty($_GET['id']) ){
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
header('location:youfilename.php');
exit;
}
?>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>Delete
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
You can modify your code like that :
<?php
include('connection.php');
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
Your error was you don't used the POST value to delete the row but the last ID store in the row variable that come from your query reading.
Becarfull too you make a wrong usage of the prepare function in PDO.

PHP & SQL WHERE (changing variable)

Good Day, i'm struggling with this code..
The result "10" is set $fod but this needs to be replaced to display the called row's id so that the SQL WHERE can list the correct data inside the relevant row. See Image for the Result i'm getting.
See Current Result and Desired Result
As you can see i need ROW ID 10 to display 10's items and ROW ID 11 to display 11's items. Please assist me, how can i call these rows to display correctly?
Calling function:
Controller.php
$data['order_list'] = $this->product->data_ordershalf();
$fod = 10;
$data['order_listfull'] = $this->product->data_ordersfull($fod);
Functions.php:
function data_ordershalf(){
$this->db->select('*');
$this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
$this->db->join('customers', 'customers.id=orders.customerid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('orders');
$this->db->group_by('orderid');
$rs = $this->db->get();
return $rs->result_array();
}
function data_ordersfull($fod){
$this->db->select('*');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('order_detail');
$this->db->where('orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();
View.php:
<?php if(!$order_list){ ?>
<tbody>
<tr>
<th colspan="7"><center>No orders placed</center></th>
</tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
<?php foreach( $order_list as $row) { ?>
<tr>
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<table>
<?php foreach( $order_listfull as $row2) { ?>
<tr>
<th scope="row"><?php echo $row2['id']; ?></th>
<td><?php echo $row2['product_name']; ?></td>
<td><?php echo $row2['quantity']; ?></td>
<td></td>
<td><?php echo $row2['price']; ?></td>
</tr>
<?php } ?>
</table>
</td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
You can create helper and put following function it.
function data_ordersfull($fod){
$this->db->select('*');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->from('order_detail');
$this->db->where('orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();
}
====================View.php======================================
<?php if(!$order_list){ ?>
<tbody>
<tr>
<th colspan="7"><center>No orders placed</center></th>
</tr>
</tbody>
<?php } else { $sr = 1; ?>
<tbody>
<?php foreach( $order_list as $row) {
$order_listfull=data_ordersfull($row['id']);
?>
<tr>
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><table>
<?php foreach( $order_listfull as $row2) { ?>
<tr>
<th scope="row"><?php echo $row2['id']; ?></th>
<td><?php echo $row2['product_name']; ?></td>
<td><?php echo $row2['quantity']; ?></td>
<td></td>
<td><?php echo $row2['price']; ?></td>
</tr>
<?php } ?></table></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
function data_ordershalf(){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_detail', 'order_detail.orderid=orders.id', 'left');
$this->db->join('customers', 'customers.id=orders.customerid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->group_by('order_detail.orderid');
$rs = $this->db->get();
return $rs->result_array();
}
function data_ordersfull($fod){
$this->db->select('*');
$this->db->from('order_detail');
$this->db->join('orders', 'orders.id=order_detail.orderid', 'left');
$this->db->join('testshop_products', 'testshop_products.product_id=order_detail.productid', 'left');
$this->db->where('order_detail.orderid',$fod);
$rs = $this->db->get();
return $rs->result_array();

Pagination in while loop with php

I'm newbee about php,trying to learn.
I've search other topic about while loop pagination but not satisfied.
I have 70 user records in my database,wants to list them with html table. I'm showing all records with this code. How can i make simple pagination with these codes? Please help me.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users ORDER BY uid ASC";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){ ?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
The follow provide very simple pagination as a starting point. You will need to supply formatting for the pagination and such.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
$c = mysqli_query($dbc,$q);
if($c) {
if($t = mysqli_fetch_assoc($c)) {
$numrows = $t['numrows'];
}
}
$numrows = 0;
$rowsperpage = 10;
$currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
$numpages = ceil($numrows / $rowsperpage);
$startrow = ($currpage - 1) * $rowsperpage;
if($startrow > $numrows) {
$startrow = $numrows - $rowsperpage;
}
if($startrow < 0) {
$startrow = 0;
}
$q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){
?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div id='pagination'>
<?php
for($pgno = 1;$pgno <= $numpages;$pgno++) {
echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
}
?>
</div>

Query always returns recently added data only

I'm trying to show all my data from the database I made in mysql.
I am using this code:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
}
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
</table>
My problem is that not all the data show up, only the data I recently added. I believe that SELECT * means selecting all the data.
But I don't know what's the problem why it does not show all the data, anyone would happen to know?
You need to add your td inside your while loop
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
?>
<tr>
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name'];
echo " <td>" . $id . "</td>";
echo " <td>" . $name . "</td>";
}
?>
</tr>
Note:- mysql is deprecated instead use mysqli and PDO
Try using this:
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$id = $row['game_id'];
$name = $row['game_name'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
</table>
add this inside while,
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
Final Code
while($row = mysql_fetch_array($result)) {
$id = $row['game_id'];
$name = $row['game_name']; ?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
</tr>
<?php } ?>
Switch to mysqli_* or PDO instead of mysql_* which is deprecated.
If you are not connected with database then follow this code it will help you.
<table border= "3">
<tr>
<th>ID</th>
<th>Game Name</th>
</tr>
<?php
$link=mysql_connect("localhost", "root","");
mysql_select_db('dbname', $link);
$query = "SELECT * FROM `test_game_name`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $row['game_id']; ?></td>
<td><?php echo $row['game_name']; ?></td>
</tr>
<?php }
?>
</table>

Two loops in one table

I need to insert two loops in one table, but I have a problem.
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
}
?>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
</table>
But the result is:
http://prntscr.com/6m9v25
One name is in the wrong position.
Just put while loop into for loop.
// Code goes here
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
<?php
}
?>
</table>
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
$x = 1;
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<tr>
<td><?php echo $x ?></td>
<td><?php echo $name ?></td>
</tr>
<?php
$x++
}
?>
</table>
Try This Code!! (Edited Again!)

Categories