我有一张订单表。我希望用总天数填充另一张表。对于一年中的每一天,我想要总共一行。
我写了这个,我认为最大的问题是日期格式。
我问语法是否正确
我需要在这个例子中做一些事情,因为在完整版本中我需要更改一些变量:$somma_paid
如果货币发生变化..
我使用“if”并从一年的第一天开始,然后使用strtotime
我需要帮助来完成它。
最终结果将填充一个包含365行的新表格(data
,totale
,merce
,servizi
,numero
)。每行总共有一天。
$date1 = 2013-01-01;
$somma_paid = 0;
$somma_products = 0;
$somma_shipping = 0;
$query55 = "SELECT id_order, total_paid, payment, total_products, id_currency, total_shipping, shipping_number, delivery_date FROM ps_orders WHERE delivery_date LIKE '%2013%'";
$result55 = mysql_query($query55) or die(mysql_error());
while ($row55 = mysql_fetch_array($result55))
{
$currency = $row55['id_currency'];
$id = $row55['id_order'];
$date = $row55['delivery_date'];
if ($date == $date1) {
echo '
<tr>
<td style="width: 120px;">' . $id . '</td>
<td style="width: 120px;">' . $row55['delivery_date'] . '</td>
<td style="width: 170px;">' . $row55['payment'] . '</td>
<td style="width: 100px;">' . $row55['total_paid'] . '</td>
<td style="width: 100px;">' . $row55['total_products'] . '</td>
<td style="width: 100px;">' . $row55['total_shipping'] . '</td>
<td style="width: 158px;">' . $row55['shipping_number'] . '</td>
</tr>
';
$somma_paid += $row55['total_paid'];
$somma_products += $row55['total_products'];
$somma_shipping += $row55['total_shipping'];
}
else
{
$query1122 = "REPLACE INTO calendar (data, totale, merce, servizi, numero) VALUES('$date', '$somma_paid', '$somma_products', '$somma_shipping')";
$result1122 = mysql_query($query1122) or die(mysql_error());
$somma_paid = 0;
$somma_products = 0;
somma_shipping = 0;
$date1 = strtotime($date."+ 1 day");
}
}
答案 0 :(得分:1)
您应该更改查询以获取已格式化的数据,每天使用一组来为一年中的每一天添加一行,将您的选择替换为:
SELECT date(delivery_date) as data, SUM(total_paid) as totale, SUM(total_products) as merce, SUM(total_shipping) as servizi, COUNT(DISTINCT id_order) as numero
FROM ps_orders
WHERE YEAR(delivery_date) = '2013'
GROUP BY DAY(delivery_date) ORDER BY data
这样您就可以获得只需要导出的所有数据。 (您可以使用INSERT INTO日历(数据,totale,merce,servizi,numero)SELECT ...)或在您的行上执行foreach并使用php插入来制作它,就像您已经在做的那样,处理“无效”日你没有任何数据的地方。
<强>更新强>
如果您需要在插入新表之前执行转换,您可以执行按日期选择,然后循环遍历行以在需要时转换信息。转换后将所有数据存储在使用给定数据作为键的数组中(同样,如果您想要一年中的所有日期,您可以使用for()初始化数组并创建所有365个条目,即使某些条目仍为空
这是一个基于您如何获取和订购数据的代码的示例:
$query55 = "SELECT date(delivery_date) as data, total_paid as totale, total_products as merce, total_shipping as servizi, id_order as numero, id_currency as valuta
FROM ps_orders
WHERE YEAR(delivery_date) = '2013'
ORDER BY data ASC";
$result55 = mysql_query($query55) or die(mysql_error());
$calendario_array = array();
while ($row = mysql_fetch_array($result55))
{
if(!isset($calendario_array[$row['data']]))
$calendario_array[$row['data']] = array(
'data' => $row['data'], //redundant but useful for fast insert
'totale' => 0,
'merce' => 0,
'servizi' => 0,
'numero' => 0
);
switch($row['valuta'])
{
case(1): //convert value based on your id_currency
$row['merce'] = convertValue($row['merce']);
$row['totale'] = convertValue($row['totale']);
$row['servizi'] = convertValue($row['servizi']);
break;
case(2): //convert value based on your id_currency
$row['merce'] = convertValue($row['merce']);
$row['totale'] = convertValue($row['totale']);
$row['servizi'] = convertValue($row['servizi']);
break;
}
$calendario_array[$row['data']]['numero'] = $calendario_array $row['data']]['numero'] + 1;
$calendario_array[$row['data']]['merce'] = $calendario_array[$row['data']]['merce'] + $row['merce'];
$calendario_array[$row['data']]['totale'] = $calendario_array[$row['data']]['totale'] + $row['totale'];
$calendario_array[$row['data']]['servizi'] = $calendario_array[$row['data']]['merce'] + $row['servizi'];
}
//$calendario_array now contains all your data, you can cycle on it to perform the insert
foreach($calendario_array as $row)
//insert or update data in calendario table
更新v2
至于最后一部分,要在新表中插入数据,我建议执行从$calendar_array
到查询字符串的转换,它将立即执行所有插入。这样我们就不会遇到性能问题。请记住,我不打算截断表格以清除以前的数据我会留给您选择。至于数据字段,我想你将它存储为日期时间,如果没有,你将不得不编辑数据以获得所需的格式。
$insert_string = array(); //lets create a clear array that will contain a string for each entry. this will represent our insert value row
foreach($calendario_array as $row){ //cycle through the calendario array, store the data as a VALUES ( ) string
$insert_string[] = '("'.$row['data'].' 00:00:00",'.$row['merce'].','.$row['totale'].','.$row['servizi'].','.$row['numero'].')';
}
$insert_query = 'INSERT INTO ps_calendar (data, merce, totale, servizi, numero) VALUES '.implode(',', $insert_string).';'; //implode the array containing all the data.
$result = mysql_query($insert_query )or die(mysql_error());