php结合数组操作csv文件

<?php
$host = "localhost";
$username = "root";
$password = "";
mysql_connect ( $host, $username, $password );//连接mysql数据库
mysql_select_db ( "mydatabase" );//数据库名
$file = fopen ( "test.csv", "r" );//读取csv文件
$array1 = array (
        "T",
        "G",
        "A",
        "E"
);
// ID 区域 2.25-3.03     3.4-3.10    3.11-3.17
$dates = array (
        array (
                "startDate" => "2013-3-3",
                "endDate" => "2013-2-25"
        ),
        array (
                "startDate" => "2013-3-10",
                "endDate" => "2013-3-4"
        ),
        array (
                "startDate" => "2013-3-17",
                "endDate" => "2013-3-11"
        )        
);

while ( $content = fgetcsv ( $file ) ) {//取得csv文件的一行
    if (! empty ( $content ) && ! empty ( $content [0] ) ) {//如果第一个单元格的内容不为空
        $id = trim ( $content [0] );
        foreach ( $array1 as $arrayone ) {//一个$array1元素
            $content =  $id . "," .$arrayone. "," ;
            foreach ( $dates as $date ) {//一个时间段
                $selectSql = "select count(l.id) from table1 l,table2 s
                where s.user_id = l.user
                        and  l.createdOn < '".$date[startDate]."'
                        and l.endTime > '".$date[endDate]."'
                        and s.type='".$arrayone."'
                        and l.id like'" . $id . "%';";
                $result = mysql_query ( $selectSql );
                $rowNum = mysql_num_rows ( $result );
                if ($rowNum >= 1) {
                    while ( $row = mysql_fetch_array ( $result, MYSQL_NUM ) ) {
                        $content .= $row [0] . ",";
                    }
                } else {
                    // 没有记录,则为0
                    $content .= "0" . ",";
                }
            }
            $content .= "*\t\n";
            file_put_contents("countSkus.csv", $content, FILE_APPEND);
        }        
    }
}
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值