doujiushi9007 2015-07-15 14:15
浏览 56
已采纳

使用mysql和php创建多个csv文件

i'm trying to generate multiple progressive csv file (with name DOC_n°.csv) using mysql & php while.

//query 1
$query2 ="SELECT id_order FROM ps_orders";
$result2= mysql_query($query2); 

$i=1;

while ($riga2 = mysql_fetch_array($result2)){
//query 2
$query = "SELECT ps_order_detail.product_name,ps_orders.id_order,ps_orders.date_add,ps_order_detail.unit_price_tax_incl,ps_order_detail.id_order_detail,ps_order_detail.product_quantity,ps_order_detail.product_reference,ps_order_detail.product_weight,ps_order_detail.unit_price_tax_incl,ps_address.id_customer,ps_address.firstname,ps_address.lastname,ps_address.address1,ps_address.address2,ps_address.postcode,ps_address.city
FROM ps_orders
JOIN ps_order_detail ON  ps_orders.id_order = ps_order_detail.id_order
JOIN ps_address ON ps_orders.id_customer = ps_address.id_customer
WHERE ps_orders.id_order=$i;";

$result = mysql_query($query);  
    //second while to write document and values inside of it
    while ($riga = mysql_fetch_array($result)){
//useful variable to write files
        $path ="TMPIN/";
        $doc=$path."DOC_".$i.".csv";
        $myfile = fopen($doc, "w");
...
...
information inside the csv
...
..          
fwrite($myfile, $testo."
");
fclose($myfile);
};
$i++;
};

My code works, but in the csv file generated i find all the loops generate before. There is a way to see only the LAST loop generat for cycle?

  • 写回答

3条回答 默认 最新

  • drl37530 2015-07-15 14:21
    关注

    You have 2 while loop. You change file name only in the master loop, so in the child loop, you always write in the same csv (and massively open/close this file by the way !)

    So you should try something like this :

    $query = "SELECT ps_order_detail.product_name,ps_orders.id_order,ps_orders.date_add,ps_order_detail.unit_price_tax_incl,ps_order_detail.id_order_detail,ps_order_detail.product_quantity,ps_order_detail.product_reference,ps_order_detail.product_weight,ps_order_detail.unit_price_tax_incl,ps_address.id_customer,ps_address.firstname,ps_address.lastname,ps_address.address1,ps_address.address2,ps_address.postcode,ps_address.city
        FROM ps_orders
        JOIN ps_order_detail ON  ps_orders.id_order = ps_order_detail.id_order
        JOIN ps_address ON ps_orders.id_customer = ps_address.id_customer
        WHERE 1=1;";
    
    $result = mysql_query($query);  
    //second while to write document and values inside of it 
    while ($riga = mysql_fetch_array($result)){
        $testo = "";
        $testo .= "stuff to write
    ";
        ...
        ...
        information inside the csv
        ...
        ...
    
        // once you know what to write, open file.
        //useful variable to write files
        $path ="TMPIN/";
        $doc=$path."DOC_".$riga['id_order'].".csv";
        $myfile = fopen($doc, "w");
        fwrite($myfile, $testo."
    ");
        fclose($myfile);
    }
    

    EDIT : (Thanks to @Kickstart)

    Deleted the first loop which is useless... You can do 1 only query

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私