I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
I have 3 tables users alerts and articles a user can setup an alert so when a new article is added that match his profile he will get an email
I am trying to write a small code to do this task
and this is what I came up with so far
$query_users = "SELECT * FROM `users`";
$result_users = $conn->query($query_users);
while ($users = mysqli_fetch_row($result_users)) {
$user_id = $users[0];
$user_email = $users[1];
$query_alerts = "SELECT * FROM `alerts` where user_id='$user_id' and active= '1' ";
$result_alerts = $conn->query($query_alerts);
while ($alerts = mysqli_fetch_row($result_alerts)) {
$category = mysqli_real_escape_string($conn,$alerts[2]);
$keyword = mysqli_real_escape_string($conn,$alerts[3]);
$query_search = "SELECT * FROM `articles` WHERE `category_id` = '$category' `title` LIKE '%$keyword%' ORDER BY `articles`.`id` ASC ";
$result_search = $conn->query($query_search);
$count = $result_search->num_rows;
}
}
Now what I want to do is to store the result this way
Put each alert category + keyword + count in an array then put it in an array that contain the user id and email
So at the bottom I can loop through the users and send to the user with id 1 and email example#website.com
a table like this
Category | Keyword | articles count
Health | Diet | 5
Buisness | Banks | 2
I hope you understood my idea, I am very confused and dizzy I didn't even know what to type in the search box
Take one array outside of while and add 3 variable in array when loop is executed.
i added 2 lines in your code which are.
$response = array();
and
$response[] = array
(
'category' => $category,
'keyword' => $keyword,
'count' => $count
);
Complete example
<?php
$query_users = "SELECT * FROM `users`";
$result_users = $conn->query($query_users);
$response = array();
while ($users = mysqli_fetch_row($result_users))
{
$user_id = $users[0];
$user_email = $users[1];
$query_alerts = "SELECT * FROM `alerts` where user_id='$user_id' and active= '1' ";
$result_alerts = $conn->query($query_alerts);
while ($alerts = mysqli_fetch_row($result_alerts))
{
$category = mysqli_real_escape_string($conn,$alerts[2]);
$keyword = mysqli_real_escape_string($conn,$alerts[3]);
$query_search = "SELECT * FROM `articles` WHERE `category_id` = '$category' `title` LIKE '%$keyword%' ORDER BY `articles`.`id` ASC ";
$result_search = $conn->query($query_search);
$count = $result_search->num_rows;
$response[] = array
(
'category' => $category,
'keyword' => $keyword,
'count' => $count
);
}
}
echo "<pre>";
print_r($response);
I have this code how can I combine multiple queries and their result into one ?
<?php
$obj = new stdClass();
include('pdoConfig.php');
$response = array();
$sql1 = $dbh->prepare("select * from orders_assigned where delivery_status != 'Cancelled' && order_status='Picked'");
$sql1->execute();
$count1 = $sql1->rowCount();
if ($count1 >= 1) {
while ($row1 = $sql1->fetch()) {
$delivery_status = $row1['delivery_status'];
$deliveryboy_id = $row1['username'];
$order_id = $row1['order_id'];
$sql2 = $dbh->prepare("select * from delboy_login where id = ?");
$sql2->bindParam(1, $deliveryboy_id);
$sql2->execute();
$row2 = $sql2->fetch();
$del_name = $row2['name'];//name
$del_lat = $row2['lat'];//lat
$del_longi = $row2['longi'];//long
$del_icon = $row2['icon'];//icon
$sql3 = $dbh->prepare("select * from `order` where `order_id` = ?");
$sql3->bindParam(1, $order_id);
$sql3->execute();
$row3 = $sql3->fetch();
$address_id = $row3['address_id'];
$user_id = $row3['user_id'];
$sql4 = $dbh->prepare("select * from customer_login where cust_id = ?");
$sql4->bindParam(1, $user_id);
$sql4->execute();
$row4 = $sql4->fetch();
$cus_name = $row4['name'];//name
$sql5 = $dbh->prepare("select * from address where a_id = ?");
$sql5->bindParam(1, $address_id);
$sql5->execute();
$row5 = $sql5->fetch();
$cus_lat = $row5['lat'];//lat
$cus_longi = $row5['longi'];//long
$cus_icon = $row5['icon'];//icon
$tmp = array();
$tmp['lat'] = $del_lat;//i want use $cus_lat here too
$tmp['content'] = $del_name;//i want use $cus_name here too
$tmp['lng'] = $del_longi;//i want use $cus_longi here too
$tmp['icon'] = $del_icon;//i want use $cus_icon here too
array_push($response, $tmp);
}
}
echo json_encode($response, JSON_NUMERIC_CHECK);
Using this I can only get either $del_ data or $cus_ data I want use both. I have multiple tables for same data but I can not combine tables because both are different kind of users. As I am not good at joins so I don't know how to use joins here.
By using one of query provided below i am getting this
{
"delivery_status" : Delivered,
"username" : 1,
"order_id" : 5,
"del_name" : Boy One,
"del_lat" : 26.8808383,
"del_longi" : 75.7503407,
"address_id" : 31,
"user_id" : 1,
"cus_name" : Roylee Wheels,
"cus_lat" : 20.593684,
"cus_longi" : 78.96288
},
{
"delivery_status" : Processing,
"username" : 1,
"order_id" : 6,
"del_name" : Boy One,
"del_lat" : 26.8808383,
"del_longi" : 75.7503407,
"address_id" : 30,
"user_id" : 1,
"cus_name" : Roylee Wheels,
"cus_lat" : 20.594725,
"cus_longi" : 78.963407
},
and so on...
I want final output like this
{
"icon" : "icon path here",
"del_name" : "Boy One",
"del_lat" : 26.8808383,
"del_longi" : 75.7503407,
},
{
"icon" : "icon path here",
"cus_name" : "Roylee Wheels",
"cus_lat" : 20.594725,
"cus_longi" : 78.963407
},
and so on...
select orders_assigned.*, order.*, address.*
from orders_assigned
inner join delboy_login on delboy_login.id = orders_assigned.delboy_login
inner join `order` on `order`.`id` = orders_assigned.order_id
inner join customer_login on customer_login.cust_id = order.user_id
inner join address on address.a_id = order.address_id
where delivery_status != 'Cancelled' && order_status='Picked'
You need to join your tables. this is untested, so something like this.
It's bad practice to select * from tables, you should really be defining only the fields you need.
Also note that order is a reserved word in mysql so not a good name for a table - hence the backticks.
Try this : It will work !
SELECT oa.delivery_status, oa.username oa.order_id, dl.name, dl.lat, dl.longi, o.address_id, o,user_id, cl.name, a.lat, a.longi
FROM orders_assigned as oa
JOIN delboy_login as dl ON oa.username = dl.id
JOIN order as o ON oa.order_id = o.order_id
JOIN customer_login as cl ON = o.user_id = cl.cust_id
JOIN address as a ON = o.address_id = a.a_id
WHERE oa.delivery_status != 'Cancelled' && oa.order_status='Picked'
SELECT
orders_assigned.delivery_status AS delivery_status,
orders_assigned.username AS username,
orders_assigned.order_id AS order_id,
delboy_login.name AS del_name,
delboy_login.lat AS del_lat,
delboy_login.longi AS del_longi,
order.address_id AS address_id,
order.user_id AS user_id,
customer_login.name AS cus_name,
address.lat AS cus_lat,
address.longi AS cus_longi
FROM
orders_assigned
LEFT JOIN
delboy_login ON delboy_login.id = orders_assigned.username
LEFT JOIN
orders ON order.order_id = orders_assigned.order_id
LEFT JOIN
customer_login ON customer_login.cust_id = order.user_id
LEFT JOIN
address ON address.a_id = order.address_id
Here is the SQL you want.
Take note that you can rename the columns that the query returns as you can see from below.
delboy_login.name AS del_name,
delboy_login.lat AS del_lat,
delboy_login.longi AS del_longi,
customer_login.name AS cus_name,
address.lat AS cus_lat,
address.longi AS cus_longi
Joining table is very important, I hope that you will be able to pick up the query pattern in the future.
Best of luck to you.
EDIT:
The output you desire can be achieved by simply creating 2 different temp_arrays, and pushing them into the main array one after the other.
if ($count1 >= 1) {
while ($row = $sql->fetch()) {
$delivery_status = $row['delivery_status'];
$deliveryboy_id = $row['username'];
$order_id = $row['order_id'];
$del_name = $row['del_name'];//name
$del_lat = $row['del_lat'];//lat
$del_longi = $row['del_longi'];//long
$del_icon = $row['del_icon'];//icon
$address_id = $row['address_id'];
$user_id = $row['user_id'];
$cus_name = $row['cus_name'];//name
$cus_lat = $row['cus_lat'];//lat
$cus_longi = $row['cus_longi'];//long
$cus_icon = $row['cus_icon'];//icon
$tmp_del = array();
$tmp_del['del_lat'] = $del_lat;
$tmp_del['del_name'] = $del_name;
$tmp_del['del_longi'] = $del_longi;
$tmp_del['del_icon'] = $del_icon;
$tmp_cus = array();
$tmp_cus['cus_lat'] = $cus_lat;
$tmp_cus['cus_name'] = $cus_name;
$tmp_cus['cus_longi'] = $cus_longi;
$tmp_cus['cus_icon'] = $cus_icon;
array_push($response, $tmp_del);
array_push($response, $tmp_cus);
}
I was working on a post system..
So, I have to show posts by friends of the user and the groups in which user has participated..
Here is my code to show posts..
<?php
$sql = "SELECT * FROM posts WHERE uploader_id=:friend_id ORDER BY id DESC";
$query = $db->prepare($sql);
$query->execute(array(
":friend_id" => $friend_id
));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$name = $row['name'];
echo "POST BY $name";
}
$sql = "SELECT * FROM group_posts WHERE id=:member_group ORDER BY id DESC";
$query = $db->prepare($sql);
$query->execute(array(
":member_group" => $group_id
));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$name = $row['name'];
echo "POST BY $name";
}
?>
Now, I want all these posts to be shuffled in a way that all the posts of the post table and group_posts table are shown in the descending order.
UPDATE
I edited my code to this..
I figured out that first I'll have to code this before coding my post system..
<?php
$sql = "SELECT * FROM friends WHERE user_one=:me OR user_two=:me2 UNION SELECT * FROM group_members WHERE member_id=:me3";
$query = $db->prepare($sql);
$query->execute(array(
":me" => $my_id,
":me2" => $my_id,
":me3" => $my_id
));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$user_one = $row['user_one'];
$user_two = $row['user_two'];
$group_id = $row['group_id'];
if ($user_one == $my_id) {
$friend_id = $user_two;
} else {
$friend_id = $user_one;
}
echo $friend_id . "<BR>" . $group_id;
}
?>
Now, here's the problem..
This is successfully printing the $friend_id but, it shows an undefined index 'group_id' while printing $group_id.
I have checked all the fields are correct.
Try using just one query with UNION
SELECT *
FROM (
SELECT name, id FROM posts WHERE uploader_id=:friend_id
UNION
SELECT name, id FROM group_posts WHERE id=:member_group
) p
ORDER BY p.id DESC
Note, your inner queries must return the same number of columns in the same order (and I think with the same name/alias, too).
When I run the second query to select departments it is returning my list of departments which is like 20 records, and then return thousands of blank enteries to the page freezes in a browser what did I do wrong?
mysql_select_db("ita", $con);
$results = mysql_query("SELECT * FROM `Colleges`");
$colleges = array();
while($row = mysql_fetch_assoc($results))
{
$colleges[] = array("Name" => $row['Name'], "Value" => $row['Value'], "ID" => $row['CollegeID']);
}
$collegecount = count($colleges);
$depts = array();
$result1 = mysql_query("SELECT * FROM `Departments` WHERE uid = `$uid`");
while($row = mysql_fetch_assoc($result1))
{
$depts = array("Name" => $row['Name'], "Value" => $row['Value'], "ID" => $row['CollegeID']);
}
$result = mysql_query("SELECT * FROM `users` WHERE wmuid = '$uid'");
while($row = mysql_fetch_assoc($result)){
//POPULATE FORM FIELDS FROM DB
You have backticks around your $uid. I'm not sure what exactly that will do since I doubt you have a column with the same name as whatevers in $uid, but it's probably going to cause some weird undefined behavior. Change your ` to ' Like:
SELECT * FROM `Departments` WHERE `uid` = '$uid'
Edit
Or if the column uid stores a numerical type remove the quotes altogether like:
SELECT * FROM `Departments` WHERE `uid` = $uid