Show results from mysql query with php 5 - php

<table class="table table-bordered table-hover table-responsive bgwhite clgray">
<thead>
<tr>
<th class="text-center text-capitalize" width="20">
No
</th>
<th class="text-center text-capitalize">
idbarang
</th>
<th class="text-center text-capitalize">
Kategori
</th>
<th class="text-center text-capitalize">
Merk
</th>
<th class="text-center text-capitalize">
Foto
</th>
<th class="text-center text-capitalize">
Stok
</th>
</tr>
</thead>
<?php
$queryStok = "SELECT SUM(stok) AS jumlahbarang FROM tabelstok WHERE idjenisusaha=1 GROUP BY idbarang";
$resultStok = mysqli_query($link,$queryStok);
$queryStok2 = "SELECT * FROM tabelbarang,tabelmerk WHERE tabelbarang.idmerk=tabelmerk.idmerk";
$resultStok2= mysqli_query($link,$queryStok2);
while ($datastok=mysqli_fetch_assoc($resultStok,$resultStok2)) {
?>
<tbody>
<tr>
<td class="text-center text-capitalize">
<?php echo $datastok['merk'] ?>
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
No
</td>
<td class="text-center text-capitalize">
<?php echo $datastok['jumlahbarang'];?>
</td>
</tbody>
<?php } ?>
</table>
How can I show the result of 2 tables with 2 queries, in A SINGLE loop.

If you want to show the result of two queries without writting two while loops, you should use mysqli_multi_query() function to execute multiple queries at once. Each statement should have a semicollon at the end (the last statement should not contain the semicollon). You can add as many queries as you want.
$query = "YOUR FIRST STATEMENT;";
$query .= "YOUR SECOND STATEMENT";
$result = mysqli_multi_query($link, $query);
do {
$result = mysqli_store_result($link);
while ($row = mysqli_fetch_row($result)) {
// DO YOUR STUFF HERE WITH $row
}
mysqli_free_result($result);
} while (mysqli_next_result($link));
If you want to merge the queries into one, then if there is a column which connects/associates both tables, then you should use JOIN when you build the query. Example:
SELECT customers.id, customers.name FROM customers
INNER JOIN details ON customers.id=details.user_id;
This merges the elements from details table with elements from customers table, linked by a common value (the customers ids).
As you didn't show the structure of the database, and tables I can't show an example based on your code, just a general one.

Related

Adding pagination to a table using Datatables

I am updating a plugin for WordPress using .php files. I have an update that requires a table and pagination. I'm trying to use jQuery for this.
I followed the datatable instructions but I am not seeing any changes applied. What am I doing wrong? I have added the jQuery to use the correct table id. Nothing seems to work doing this.
I am attempting to run through the data and allow the page to paginate. I do not care about having the option for user to select number of items on page at given time.
I am looking for an easy way to paginate a HTML table. Nothing I've tried seems to work
jQuery(document).ready(function() {
jQuery('#engine-search-table').DataTable();
});
<div class="parts-table">
<table id="engine-search-table" class="display">
<thead>
<tr id="header-row">
<th class="table-header">
<h4>Manufacturer</h4>
</th>
<th class="table-header">
<h4>Model</h4>
</th>
<th class="table-header">
<h4>Description</h4>
</th>
<th class="table-header">
<h4>Series</h4>
</th>
<th class="table-header">
<h4>Engine</h4>
</th>
<th class="table-header">
<h4>Years</h4>
</th>
<th class="table-header">
<h4>Begin Serial</h4>
</th>
<th class="table-header">
<h4>Engine Serial</h4>
</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($parts)) {
foreach ($parts as $part) {
$meta = get_post_meta($part->ID); ?>
<tr id="data" class="parts-row">
<td class="part-container-title">
<?php printCFTVal($meta, 'manufacturer');?>
</td>
<td class="part-container-manufacturer">
<a href='<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'model'); ?>
</a>
</td>
<td class="part-container-category">
<?php printCFTVal($meta, 'description'); ?>
</td>
<td class="part-container-excerpt">
<?php echo str_replace('OVERVIEW', '', get_the_excerpt($part)); ?>
</td>
<td class='part-container-button'>
<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'enginemodel'); ?>
</td>
<td>
<?php printCFTVal($meta, 'years');?>
</td>
<td>
<?php printCFTVal($meta, 'beginSerial');?>
</td>
<td>
<?php printCFTVal($meta, 'engineserial');?>
</td>
</tr>
<?php }
} else {
echo '<p>No parts found.</p>';
} ?>
</tbody>
</table>
</div>

How can I display values from a table that correspond to their matching id

So in this query below im joining two tables through order_id and displaying the all the values from the user_orders table.
As per the image below I am trying to display only the order_Id rows that match the order_manager table.
public function getUserOrder(){
$sql = "SELECT user_orders.order_id,
user_orders.title, user_orders.price,
user_orders.quantity
FROM order_manager
JOIN user_orders ON order_manager.order_id = user_orders.order_id;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
while ($result = $stmt->fetchAll()){
return $result;
}
}
I have attempted to use an if statement that appears to do something however it gives me the values that don't match order id in reverse.
<div class="container mt-5">
<?php $artworks = new Artworks(); ?>
<div class="row">
<div class="col-lg-12">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Order ID</th>
<th scope="col">Full Name</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Orders</th>
</tr>
</thead>
<?php
$artworks->getOrder();
foreach ($artworks->getOrder() as $art) {
echo "<tbody>
<tr>
<td>$art[order_id]</td>
<td> $art[full_name]</td>
<td> $art[phone] </td>
<td>$art[address]</td>
<td>
<table class= 'tale text-center table-dark'>
<thead>
<tr>
<th scope='col'>Order ID</th>
<th scope='col'>title</th>
<th scope='col'>price</th>
<th scope='col'>Quantity</th>
</tr>
<thead>
<tbody>
<tr>";
$artworks->getUserOrder();
foreach ($artworks->getUserOrder() as $order) {
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
}
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>";
}
echo "
</tbody>
</table>
</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
Here is an image to help explain the desired output
Solved moving the td items into the if statement. Guess it was mostly correct.
$artworks->getUserOrder();
foreach($artworks->getUserOrder() as $order)
{
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>
";
}
}

Shop by size using table format in PHP

I want my site to search for any product that belonged to particular size when click any size option in the table cell for desktop users but I am having two challenges
My loop for horizontal cells suppose to be incrementing from left to right like this 28D, 28DD, 28E, 28F, 28FF, 28G ...28K and then move to second line 30D --- 30k but instead it was reapeating each value 12 times horizontally like this 28D, 28D, 28D 12times and move to second line 28DD repeating it 12 times again. Please what could be the problem in my four loop?
I don't know how to put anchor tag around the table cell and how to put name = size attribute in the table cell for it to link sizeresult.php. where it will be processed by the select query
here is my code:
<div class="table-responsive"><!-- table-responsive begin -->
<table class="table table-striped table-hover" border="0" width="100%" cellpadding="5" cellspacing="5">
<thead>
<tr>
<th class="success">
<h4 class="text-center white-text">D</h4>
</th>
<th class="info">
<h4 class="text-center white-text">DD</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">E</h4>
</th>
<th class="success">
<h4 class="text-center white-text">F</h4>
</th>
<th class="info">
<h4 class="text-center white-text">FF</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">G</h4>
</th>
<th class="success">
<h4 class="text-center white-text">GG</h4>
</th>
<th class="info">
<h4 class="text-center white-text">H</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">HH</h4>
</th>
<th class="success">
<h4 class="text-center white-text">J</h4>
</th>
<th class="info">
<h4 class="text-center white-text">JJ</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">K</h4>
</th>
</tr>
</thead>
<tbody>
<?php
$count = 12; // Number of possible cells to add at once:
$i=0;
$get_sizes = "select * from sizes";
$run_sizes = mysqli_query($dbc,$get_sizes);
while ($row_sizes=mysqli_fetch_array($run_sizes)){
$size_id = $row_sizes['size_id'];
$size_name = $row_sizes['size'];
$i++;
?>
<tr>
<?php for ($i = 1; $i <= $count; $i++) {
echo "
<td value='$size_name' align='center'>
$size_name
</td>";
} // End of FOR loop.
?>
</tr>
<?php } ?>
</tbody>
</table>
</div> <!-- table-responsive end -->
</form>
sizeresult.php code is this :
$size_name=$_POST['size'];
$run_products = mysqli_query($dbc,"SELECT * FROM products INNER JOIN SIZES USING (size_id) WHERE sizes.size ='%$size_name%'");
I made the following changes
$count variable and for loop completely
created block of code to format your list of items in the way you have explained in your question grouping them by 12 in each row.
based on your request of using anchor tag i added anchor tag so that when the user clicks on it it will be taken to the sizeresult.php page and the query will be processed.
I made sure that the post and get request doesn't conflict by putting the following code right above the query in sizeresult.php
if(isset($_GET['size'])){ $size_name=$_GET['size'] } else if(isset($_POST['size'])){ $size_name=$_POST['size']; }
the following in your main page
<div class="table-responsive"><!-- table-responsive begin -->
<table class="table table-striped table-hover" border="0" width="100%" cellpadding="5" cellspacing="5">
<thead>
<tr>
<th class="success">
<h4 class="text-center white-text">D</h4>
</th>
<th class="info">
<h4 class="text-center white-text">DD</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">E</h4>
</th>
<th class="success">
<h4 class="text-center white-text">F</h4>
</th>
<th class="info">
<h4 class="text-center white-text">FF</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">G</h4>
</th>
<th class="success">
<h4 class="text-center white-text">GG</h4>
</th>
<th class="info">
<h4 class="text-center white-text">H</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">HH</h4>
</th>
<th class="success">
<h4 class="text-center white-text">J</h4>
</th>
<th class="info">
<h4 class="text-center white-text">JJ</h4>
</th>
<th class="danger">
<h4 class="text-center white-text">K</h4>
</th>
</tr>
</thead>
<tbody>
<tr>
<?php
//$count = 12; // Number of possible cells to add at once://you don't need this too.
$i=1;
$get_sizes = "select * from sizes";
$run_sizes = mysqli_query($dbc,$get_sizes);
while ($row_sizes=mysqli_fetch_array($run_sizes)){
$size_id = $row_sizes['size_id'];
$size_name = $row_sizes['size'];
if($i==12){
echo "<td align='center'><a href='product-card.php?size=$size_name' type='button' style='text-decoration:none; color:black;' class='btn btn-block'>$size_name</a></td>";
$i=1;
echo "</tr><tr>";
}
else {
echo "<td align='center'><a href='product-card.php?size=$size_name' type='button' style='text-decoration:none; color:black;' class='btn btn-block'>$size_name</a></td>";
$i++;
}
} // End of while loop.
?>
</tr>
</tbody>
</table>
</div> <!-- table-responsive end -->
<!-- </form> you can remove this FORM tag if you want -->

Error grouping rows with similar ids together

I am trying to group rows with similar ids (order_id in this case) together. I want each group of similar ids in a separate table. My presence query displays all rows in one table. I don't know how to go about it.
This is my code:
<div class='panel panel-body'>
<table class='table table-condensed'>
<thead>
<tr class='cart_menu'>
<td class='price'>Order ID</td>
<td class='price'>Item(s)</td>
<td class='price'>Quantity</td>
</tr>
</thead>
<tbody>
";
$select = "SELECT DISTINCT a.*, b.* FROM shipping_order a JOIN shipping_details b ON b.order_id = a.order_id WHERE user_id = :user_id AND a.order_id = b.order_id";
foreach ($db->query($select, array('user_id' => $id)) AS $items){
echo"
<tr>
<td class='cart_price'>
<h4>{$items['order_id']}</h4>
</td>
<td class='cart_price'>
<h4><a href=''>{$items['item']}</a></h4>
<!-- <p>Web ID: </p> -->
</td>
<td class='cart_price'>
<p>{$items['quantity']}</p>
</td>
</tr>
";
}
echo"
{$items['total']}
</tbody>
</table>
</div>

Displaying data in table format from mysql database with php loop

Need to display retrieved data from mysql table in table format, but not getting the right display i planned for. This is how i want it to look like
Target display
But this is what am getting based on my code
Current display
This is the html code
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php
echo $staff["first_name"];
}
?>
</td>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff last name
?>
<td>
<?php
echo $staff["last_name"];
}
?>
</td>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff department
?>
<td>
<?php
echo $staff["department"];
}
?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
&nbsp
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
</tbody>
</table>
</div>
Here is the function that am using to find the list of all employee from the my staff table
function find_all_employee() {
global $connection;
$query = "select * from staff";
$staff_set = mysqli_query($connection, $query);
confirm_query($staff_set);
return $staff_set;
}
Is there a better way to write the loop and display my data in the right way? I have looked up similar threads but still not able to grasp.
I dont understand why you calling so much tie function and also why making so much loops. Let me try fi your code:
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php echo $staff["first_name"]; ?>
</td>
<td>
<?php echo $staff["last_name"]; ?>
</td>
<td>
<?php echo $staff["department"]; ?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
&nbsp
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Since OP asked for a varying technique in a comment:
<?php
// Select all staff first name, last name, and department info
$staff_set = find_all_employee();
$staff_results = array();
while ($staff = mysqli_fetch_assoc($staff_set)) {
$staff_results[] = $staff;
}
?>
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (!empty($staff_results)): ?>
<?php foreach($staff_results as $staff_member): ?>
<tr>
<td>
<?= $staff_member["first_name"]; ?>
</td>
<td>
<?= $staff_member["last_name"]; ?>
</td>
<td>
<?= $staff_member["department"]; ?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
&nbsp
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr><td colspan="4">No Staff Members Found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
This also introduces the short tag syntax (<?=) for replacing calls to echo. Read this thread if your PHP version is < 5.4. This also introduces control structure alternate syntax, for better readability with HTML. The above approach separates the concern of selecting data from the DB from the presentation of the HTML. This allows for easier debugging, as well as future adaptation and modularization of your application.

Categories