R
R
Rinat2014-05-03 12:07:32
PHP
Rinat, 2014-05-03 12:07:32

Loop within a loop, how to implement?

Two requests

/*Выборка дат по закупкам*/
$procurement = mysql_query ("SELECT data_start_god 
FROM goods 
WHERE id_com_god='{$company['inde_com']}' GROUP BY data_start_god  ORDER BY `data_start_god` ASC LIMIT 7",$db);
while ($rowk = mysql_fetch_array($procurement))
echo "'{$rowk['data_start_god']}',";

/*Подсчет cуммы товара*/
$product = mysql_query ("SELECT SUM(start_col_god) 
FROM goods 
WHERE id_com_god='{$company['inde_com']}' AND data_start_god='{$rowk['data_start_god']}'  ORDER BY `start_col_god` ASC LIMIT 7",$db);
while ($pro = mysql_fetch_array($product))
echo "'$pro[0]',";

From the first query, you need to display the last 7 dates.
The second query is used to calculate the amount of goods for a given date.
Ie the second cycle depends on the first, but the sum on dates does not leave.
You need two separate requests, so they will be in different places in javascript
I don’t know if it was possible to explain correctly =(
Help as you can

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
svd71, 2014-05-03
@WaRstim

@WaRstim :

"you need two separate requests, so they will be in different places in javascript"

The code is in php and if it plays some role in javascript, then it is not clear what is needed.
/*Выборка дат по закупкам*/
$procurement = mysql_query ("SELECT data_start_god 
FROM goods 
WHERE id_com_god='{$company['inde_com']}' GROUP BY data_start_god  ORDER BY `data_start_god` ASC LIMIT 7",$db);
while ($rowk = mysql_fetch_array($procurement))
echo "'{$rowk['data_start_god']}',";

foreach($rowk as <b>$itm</b>) {
/*Подсчет cуммы товара*/
$product = mysql_query ("SELECT SUM(start_col_god) 
FROM goods 
WHERE id_com_god='{$company['inde_com']}' AND data_start_god='{<b>$itm</b>['data_start_god']}'  ORDER BY `start_col_god` ASC LIMIT 7",$db);
while ($pro = mysql_fetch_array($product))
echo "'$pro[0]',";
}

@metamorph wrote correctly, for any construction it is enough to get by with one request, but it can also be divided into two requests.
$procurement = mysql_query ("SELECT data_start_god<b>, sum(start_col_god) as sum_col_god </b>
FROM goods 
WHERE id_com_god='{$company['inde_com']}' GROUP BY data_start_god  ORDER BY `data_start_god` ASC LIMIT 7",$db);
while ($rowk = mysql_fetch_array($procurement))
echo "'{$rowk['data_start_god']}',";

Y
Yuri Morozov, 2014-05-03
@metamorph

Perhaps I did not understand the task, but what prevents it from being done in one request?
select ..., SUM(number of products)
where ...
group by [company_id,] date
order by date desc
limit 7?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question