Fetching Specific Orders - php

This is the query I have written to fetch the records from 3 different tables and display them in one simple table.
$order_id=$_REQUEST['a'];
$query="select DISTINCT product.productId,orders.id,retailerName, total, status, delivery,shopName,ownerName,address,email,contact,notes,order_item.quantity,order_item.date,productName,price,product.detail,product.price,product.expiry from orders,checkout,order_item,product where product.productId=order_item.productId and orders.id ='".$order_id."' and orders.id=order_item.orderId and orders.id=checkout.orderId";
$res=mysqli_query($conn,$query);
$row=mysqli_fetch_array($res);
The below code is written for display of record and detail.
<?PHP
while($row=mysqli_fetch_array($res)){
?>
<tr>
<?php
$subtotal=0;
$subtotal = $row['quantity'] * $row['price'];
$total += $subtotal;
?>
<td><?php echo $row['productName'];?></td>
<td><?php echo $row['quantity'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $subtotal;?></td>
</tr>
<?PHP
}
?>
It displays all of the orders from database whereas I want the orders to be displayed for specific company.

If you have button for show only one result you should link it with some id.
For example:
Delete
Make SQL query show only one result: use WHERE clause.
if(isset($_GET['id'])) {
$id = $_GET['id'];
$q = "DELETE FROM table WHERE id = '$id'";
}
$db->query($q);

Related

How do I display item title from 1 category (tbl_category) in a table that represents another category?

I have 2 tables tbl_category and tbl_food.
I sorted the items from tbl_food to tbl_category by adding category_id to tbl_food, where category_id in tbl_food is identical to id in tbl_category.
But how do I take title from tbl_category and display it in a table that represents items from tbl_food?
<?php
//query to get all admin from dtb
$sql = "SELECT * FROM tbl_food";
//Exectue the query
$res = mysqli_query($conn, $sql);
//check if executed
if($res==TRUE)
{
//count rows to check wether we have data
$count = mysqli_num_rows($res); //get all rows in dtb
$sn=1; //create a variable and assign the value
//check numb of rows
if($count>0)
{
//there is data
while($rows=mysqli_fetch_assoc($res))
{
//using while loop to get data from dtb
//get individual data
$id=$rows['id'];
$title=$rows['title'];
$description=$rows['description'];
$price=$rows['price'];
$active=$rows['active'];
$category_id=$rows['category_id'];
//display values in table
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $title; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $price; ?> kn</td>
<td><?php echo $active; ?></td>
<td><?php echo $category_id; ?></td>
</tr>
<?php
}}
else{
?>
<tr colspan="4">
<td class="error">No categories added</td>
</tr>
<?php
}
}
?>
I think you need to edit your query by adding JOIN to join tbl_category to tbl_food via category_id, at first, like this:
$sql = "SELECT food.*, category.title FROM tbl_food AS food INNER JOIN tbl_category AS category ON category.id=food.category_id";
Secondly it seems you have mistake in iterating query results. mysqli_fetch_assoc($res) returns an associated array, so it must be like this:
$rows = mysqli_fetch_assoc($res)
foreach($rows as $row){
$id=$row['id'];
$title=$row['title'];
$description=$row['description'];
$price=$row['price'];
$active=$row['active'];
$category_id=$row['category_id'];
//Doing stuff
}
You have to method for get title from tbl_category
1- JOIN Method: it is already mentioned above
2- Condition Method:
$sql = "SELECT tbl_food.*, tbl_category.title FROM tbl_food,tbl_category WHERE tbl_category.id=tbl_food.category_id";
foreach($sql as $row){
$id=$row['id'];
$title=$row['title'];
$description=$row['description'];
$price=$row['price'];
$active=$row['active'];
$category_id=$row['category_id'];
}

left join query of mysql php

I'm working on a project where I have two tables 1 is the Users table and another one in the Department table. here I have given a foreign key connection to the department table to the user's table and checked that is working fine. (getting data of particular foreign key). now my requirement is in a single table I want to display all users along with that the foreign key value also. here.
require_once "database.php";
$result = "SELECT * FROM Users";
// , Department WHERE Department.Department_ID = Users.Dpt_id
$output = mysqli_query($conn, $result);
<?php
while ($single = $output->fetch_assoc()):?>
<tr>
<td><?php echo $single['firstname']; ?></td>
<td><?php echo $single['lastname']; ?></td>
<td><?php echo $single['email']; ?></td>
<td><?php echo $single['phnumber']; ?></td>
<td><?php echo $single['provider']; ?></td>
<td><?php echo $single['location']; ?></td>
<td><?php echo $single['Dpt_id']; ?>
</td>
<td><?php if ($single['Dadmin'] == 1){
echo '<p>Department admin</p>';
}
elseif($single['Superuser'] == 1){
echo '<p>SUPER</p>';
}
else{
echo '<p>USER</p>';
}
?></td>
<?php endwhile ?>
tried code:
$result = "SELECT * FROM Users, Department WHERE
Department.Department_ID = Users.Dpt_id";
$output = mysqli_query($conn, $result);
so here is my code. please help me to get the value from the foreign key.
I think you want this result but i don't know your column name:
SELECT u.*, d.department_name
FROM USERS u
LEFT JOIN Department d
ON u.dpt_id = d.department_id
Try this query:
SELECT u.*, d.department_id
FROM USERS u
INNER JOIN Department d
ON u.dpt_id = d.department_id

Display most recent logs for every user

How to display most recent logs for every user?
My database tables are user_log_id, username, login_date, logout_date, user_id. Can you please help me with the query? Thank you
Heres my code:
<?php
$user_query = $conn->query("select * from user_log order by user_log_id DESC ")or die(mysql_error());
while($row = $user_query->fetch()){
$id = $row['user_log_id'];
?>
<tr>
<td><?php echo $row['login_date']; ?></td>
<td><?php echo $row['logout_date']; ?></td>
<td><?php echo $row['username']; ?></td>
</tr>
<?php } ?>
The MySql query you used is correct for getting recent logs. But if you need to get only for specific users or only for logged in users, you have to add a where clause in the query.
select * from user_log where username=".$row['username']." order by user_log_id DESC

How to run two statements in mysqli_query

I need to run two statements to return the data I need. The below code will return all needed except the count column which can be retrieved from a different table.
How can I run these two statements in the same code to retrieve the count as well?
Here is the code I have:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
The other statement that the count data I need is this:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
Thanks for your time and any help you can provide.
Join the two queries.
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";

How to disable SELECT QUERY from auto-sorting the results

<?php
$db = db_connect();
$SQLSELECT = "SELECT * FROM `order` INNER JOIN tb ON order.pc = tb.pc";
$result_set = mysqli_query($db, $SQLSELECT);
foreach($result_set as $row) {
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['add1']; ?></td>
<td><?php echo $row['add2']; ?></td>
<td><?php echo $row['prov']; ?></td>
<td><?php echo $row['pc']; ?></td>
<td><?php echo $row['tier']; ?></td>
</tr>
<?php
I have this code and it gets data from the database by comparing the 2 tables. So for example the one in order table is 5,6,2,1,4,3. So when the query compares the 2 tables it checks for 5 then 6 then 2 and so on. When the results come out, the results become sorted and the output becomes 1,2,3,4,5,6 but I want to output it in the order how I input it. It is somehow auto sorting. Is it possible to disable that?
Unless you explicitly specify a sort order with the user of ORDER BY. Mysql and other databases does not provide any guarantee about the order in what the data is returned.
If you have a table that hasn't had many deletes and updates, the order is likely the order that you inserted. But no guarantee. So use ORDER BY

Categories