I'm totally a begginer trying to get this done. I get a list of ids from a query as 1,2,3,4... then I need to SUM the values of a column named "total" where the id includes one of the ids listed.
//Total quoted
//$array_quos = $row3['quo_list'];
$array_quos = explode(',', $row3['quo_list']);
foreach ($array_quos as $qtid => $qt)
{
$qt = $db->query("SELECT total FROM exp.quo_main WHERE quo_id = '.$qtid.' ");
$total_quoted2 += $qt;
}
Related
Im pulling data from 2 different WordPress tables and looping through to get the appropreate data into a table row.
$tid = $_POST['pqrnum']; //get estimate number
$results = $wpdb->get_results('SELECT * FROM eqnum WHERE id='.$tid); //getting estimate data
$pArray = explode(',', $results[0]->pnum); //creating array of product numbers in estimate
$ptable = "<table>"; //create table
$presults = $wpdb->get_results('SELECT * FROM eparts'); //getting product data
$pres= count($presults); //products array count
for($l = 0; $l <=count($pArray); $l++){ //loop through each product in the array
$item = $pArray[$l]; //getting item number start 0
$titem = $item + 1; //adding 1 to $item without disturbing it because product array starts at 1
for ($x = 1; $x <= $pres; $x++) { //looping through product array
if($titem == $presults[$x-1]->id){ // if estimate item = product number
$ptable .= "<tr><td>item num ". $titem ." presults id ".$presults[$x-1]->id." presults name ".$presults[$x-1]->name."</td></tr>";
}
}
}
$ptable .= "</table>";
not sure why, but it seems to be printing an extra row, in this particular example there should be 3 rows, however this is the response:
item num 1 presults id 1 presults name Back up Alarm
item num 10 presults id 10 presults name Oil Filter
item num 11 presults id 11 presults name Spark Plug
item num 1 presults id 1 presults name Back up Alarm
as you can see the first and last lines are the same, it should be just the top 3, not 4.
just trying to determine why the extra row, any help appreciated.
Alex had the answer using < rather than <=, I thought I had tried that before but prefect thank you.
I have a query that requests an ID (the PK) and an order number and throws them into an array. I then loop through the returned data in the array and run two more queries to find the number of times the order number shows up in the database and to get the invoice numbers that belong to that order number. The problem I'm seeing with this setup is that it is taking a while (around 9 seconds) to return the compiled data array. Is there a faster way to get the returned results I'm looking for?
I've tried to find some articles online and came across mysqli_multi_query. Is this the better route to make multiple queries to gather the type of data I am trying to get?
<?php
require 'config.php';
$sql = "SELECT id,internal_order_number FROM orders GROUP BY internal_order_number ORDER BY created_date desc LIMIT 0 ,50";
$query=mysqli_query($mysqli, $sql);
if (!$query) {
throw new Exception(mysqli_error($mysqli)."[ $sql]");
}
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData['line_id'] = $row["id"];
$nestedData['internal_order_number'] = $row["internal_order_number"];
$data[] = $nestedData;
}
$compiled_data = array();
// Loop through data array with additional queries
foreach($data as $line){
$new_data = array();
// Get item counts
$item_counts = array();
$get_count = " SELECT internal_order_number FROM orders WHERE internal_order_number = '".$line['internal_order_number']."' ";
$count_query=mysqli_query($mysqli, $get_count);
while ($counts=mysqli_fetch_array($count_query)){
if (isset($item_counts[$counts['internal_order_number']])) {
$item_counts[$counts['internal_order_number']]++;
} else {
$item_counts[$counts['internal_order_number']] = 1;
}
}
$product_count = $item_counts[$line['internal_order_number']];
// Get invoice numbers
$invoice_array = array();
$get_invoices = " SELECT invoice_number FROM orders WHERE internal_order_number = '".$line['internal_order_number']."'";
$invoice_query=mysqli_query($mysqli, $get_invoices);
while ($invoice=mysqli_fetch_array($invoice_query)){
if(!in_array($invoice['invoice_number'], $invoice_array)){
$invoice_array[] = $invoice['invoice_number'];
}
}
$invoices = implode(", ",$invoice_array);
$new_data['order_number'] = $line['internal_order_number'];
$new_data['count'] = $product_count;
$new_data['invoices'] = $invoices;
$compiled_data[] = $new_data;
}
mysqli_close($mysqli);
print_r($compiled_data);
?>
What, why are you doing basically the same query 3 times. You first one selects them all, you second query requires the same table making sure the first tables order number == the tables order number and the last just grabs the invoice number...?
Just do one query:
SELECT internal_order_number, invoice_number FROM table WHERE ...
Then loop through it and do what you need. You don't need 3 queries...
I want to add the product quantity based on customer name.Here I fetch the values based on customer name and store into array ,now I want to add the quantity values.In select query ,the condition customer having 2+2= 4 qty in separate of two rows How can I add the qty values.
$selectproduct = "SELECT * FROM purchase_item WHERE custname = '$customername'";
$resultselectproduct = $conn->query($selectproduct);
if ( $resultselectproduct ->num_rows >0 )
{
while($rowselectproduct = $resultselectproduct->fetch_assoc())
{
$array[] = $rowselectproduct;
}
}
My database structure:
custname product qty
A ProA 2
A ProB 2
When I run the query based on 'A' customer I got the value as qty value 4
Just change your query and use the SUM() function. Like so:
$query = "SELECT
SUM(qty) AS total
FROM `purchase_item`
WHERE `custname` = '$customername'";
$stmt = $conn->query($query);
$result = $stmt->fetch_assoc();
echo $result['total'];
I had array values as cause
Ex: $cause = $_REQUEST['cause'];
ie., $cause = 2,3,4
How to get that array value cause name from query
My table name is 'cp_cause'
How to get the 2,3,4 cause name from the above table.
My sample query model in thinkphp is
$cause_name = $GLOBALS['db']->getAll("select category from ".DB_PREFIX."category where id = '".$cause."'");
i want the name of labour, health, women
If I get it right: you get comma separated Ids and want to query this?
SELECT * FROM cp_cause WHERE id IN (2, 3, 4)
PHP:
$cpCauses = $GLOBALS['db']->getAll("select * from cp_cause where id in('".$cause."')");
The result should be a list, containing the matching rows. But we do not know, what your getAll-Method returns!
Example: if result is an array, you can iterate:
foreach($cpCauses as $cause) {
echo $cause['cause_name'];
}
You need to create string like '2','3','4' for checking with MySql in clause.
For e.g.
<?php
$cause = array();
$cause[] = '2';
$cause[] = '3';
$cause[] = '4';
$sql_where = array();
foreach($cause as $values){
$sql_where[] = "'".$values."'";
}
$sql_where = implode(",",$sql_where);
$cause_name = $GLOBALS['db']->getAll("select category from ".DB_PREFIX."category where id in '".$sql_where."'");
?>
In my project I am fetching data from the database using a while loop where each data is fetched by a customer id as $cid. Now all the data is calculated and finally put into a variable $cashflow. Now suppose there are 20 persons so the table will display 20 records where each data is counted for each person / customer. Now what I want to do is count the total of all 20 records and echo it seperately.
For eg.
Cust.1 : 500 (calculated from $cashflow)
Cust. 2 : 1000
Cust.3 : 800 ..... and so on.
Now these data are well echoed in my table. Now what I want is to add 500+1000+800 and echo 2300 as total separately. But I am not being able to figure out how to do it in my code. Should I take a variable in loop as a ++ counter? or something else? Please help.
Code:
$cash = "SELECT SUM(paid) FROM hbil WHERE cid = '".$cid."' AND method = 'cash'";
$cash_qur = mysql_query($cash) or die(mysql_error());
$cash_neg = "SELECT SUM(price) FROM hbil WHERE cid = '".$cid."' AND method = 'cash' AND price < 0";
$cash_qur_neg = mysql_query($cash_neg) or die(mysql_error());
while($cash_fetch = mysql_fetch_array($cash_qur))
{
$cash_fet = mysql_fetch_array($cash_qur_neg);
$cashpay = $cash_fetch['SUM(paid)'];
$cashneg = $cash_fet['SUM(price)'];
$cash_total = $cashpay-$cashneg;
$cashflow = number_format($cash_total,2,'.',',');
}
$total = 0;
while (...) {
//...
$total += $cash_total;
}
echo $total;
Will do the trick