How to sort in while loop - php

Hello here is my current code:
$sqlpack = "Select * from package_in_plan where plan_id='$package_id' order by plan_id";
$planres = mysqli_query($conn,$sqlpack);
$plan = array();
while ($row1 = mysqli_fetch_assoc($planres)){
$plan[] = $row1['package_id'];
}
$plan_1 = implode(',', $plan);
for ($x = 0; $x < count($plan); $x++) {
$sql_service = "Select * from service_in_package where package_id='".$plan[$x]."'";
$chid = mysqli_query($conn,$sql_service);
while ($row = mysqli_fetch_array($chid)){
$ch_id = $row['service_id'];
$sql = "Select * from itv where status='1' and id='$ch_id' order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
}
}
What i need is the output to be sorted by number($chnr), right now my code is not sorting because it's receiving specific id from previous select ($ch_id).
How can i let the output of $results to be sorted "order by number".
Number is INT in itv table.

You could make use of subqueries which results in a single query that can be sorted.
$sql = "Select * from itv where status='1' and id IN
(Select service_id from service_in_package where package_id IN
(Select package_id from package_in_plan where plan_id='$package_id' order by plan_id))
order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}

Related

Display the total of the SUM (price * quantity) from SELECT query [duplicate]

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.
while ($row = mysql_fetch_assoc($result)){
$sum += $row['Value'];
}
echo $sum;
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['ColumnName'];
}
echo $qty;
Try this:
$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database:
Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'
For this condition, the following would be the sql query and code:
$sql_qry = "SELECT SUM(duration_sec) AS count
FROM tbl_npt
WHERE date='09-10-2018' AND status='off'";
$duration = $connection->query($sql_qry);
$record = $duration->fetch_array();
$total = $record['count'];
echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.
Method #1
$qry = "SELECT column_value AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
while ($rec = $db->fetchAssoc($res)) {
$total += $rec['count'];
}
echo "Total: " . $total . "\n";
Method #2
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #3 -SQLi
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $conn->query($sql);
$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #4: Depreciated (don't use)
$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name');
$row = mysql_fetch_assoc($res);
$sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']).
Also, make sure you set $sum = 0 before the loop.
Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL
"SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
sum = $row['SUM(price)'];
}
echo sum;

How to fetch single row data from php?

for ($i=$start; $i<$start+$scale && $i < $total_record; $i++)
{
$sql = "select * from memo where num = ?";
$stmh = $pdo->prepare($sql);
//mysql_data_seek($result, $i);
$row = mysql_fetch_array($result);
$sql2 = "select * from phptest.memo order by num desc";
$stmh2 = $pdo->query($sql2);
$stmh2->execute();
//$row = $stmh2->fetch(PDO::FETCH_ASSOC);
$row = $stmh->fetchColumn($i-1);
$memo_id = $row['id'];
$memo_num = $row['num'];
$memo_date = $row['regist_day'];
$memo_nick = $row['nick'];
$memo_content = $row['content'];
Hi guys i want fetch single row data by using PDO method instead of $row = $stmh2->fetch(PDO::FETCH_ASSOC); like mysqli_data_seek($result,$i);. What should i do?
$sql2 = "select * from phptest.memo order by num desc";
$stmh2 = $pdo->query($sql2);
$stmh2->execute();
while($r = $stmh2->fetch()) {$row[] = $r;}
Now $row[$i] should be getting you the same results as mysqli_data_seek($result,$i) would have.
IE: $row[0] would return the first row.

While loop showing only one record when i use nested while loop for fetch data from another table

I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}

PHP Why If / Else statement not working in While loop?

This is my code and I don't know why IF/ELSE statement is not working
$user_id = $_POST['user_id'];
$strSQL = "SELECT chat.user_id,max(date) max_date ,user.user_id,user.firstname,user.lastname,user.picture
FROM ( select from_user_id as user_id,date,isread from chat
WHERE to_user_id = '$user_id'
Union select to_user_id as user_id,date,isread from chat WHERE from_user_id = '$user_id' ) as chat join user on user.user_id = chat.user_id
where user.status != 'block' group by chat.user_id order by max_date DESC";
$chatdata = array();
$objQuery = mysql_query($strSQL);
while($row = mysql_fetch_assoc($objQuery)){
$frduser_id = $row['user_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$picture = $row['picture'];
$strSQL2 = "SELECT isread from chat WHERE to_user_id = '$user_id' and from_user_id = '$frduser_id'
ORDER BY date DESC LIMIT 1";
//array_push($chatdata, $row);
$objQuery2 = mysql_query($strSQL2);
if (empty($objQuery2)) {
$row2 = 1;
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}else{
$row2 = mysql_fetch_assoc($objQuery2);
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}
}
You can use mysql_num_rows to get count of rows inside results.
$objQuery2 = mysql_query($strSQL2);
$objQuery2Rows = mysql_num_rows($strSQL2);
if ( $objQuery2Rows < 1) {
$row2 = 1;
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}else{
$row2 = mysql_fetch_assoc($objQuery2);
$result = array_merge($row, $row2);
array_push($chatdata,$result);
}

Get sum of MySQL column in PHP

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.
while ($row = mysql_fetch_assoc($result)){
$sum += $row['Value'];
}
echo $sum;
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['ColumnName'];
}
echo $qty;
Try this:
$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database:
Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'
For this condition, the following would be the sql query and code:
$sql_qry = "SELECT SUM(duration_sec) AS count
FROM tbl_npt
WHERE date='09-10-2018' AND status='off'";
$duration = $connection->query($sql_qry);
$record = $duration->fetch_array();
$total = $record['count'];
echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.
Method #1
$qry = "SELECT column_value AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
while ($rec = $db->fetchAssoc($res)) {
$total += $rec['count'];
}
echo "Total: " . $total . "\n";
Method #2
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #3 -SQLi
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $conn->query($sql);
$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #4: Depreciated (don't use)
$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name');
$row = mysql_fetch_assoc($res);
$sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']).
Also, make sure you set $sum = 0 before the loop.
Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL
"SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
sum = $row['SUM(price)'];
}
echo sum;

Categories