duanche4578 2018-04-05 15:02
浏览 183

如何上传csv文件,然后用Ajax解析其内容?

I already have a PHP script to upload a CSV file: it's a collection of tweets associated to a Twitter account (aka a brand). BTW, Thanks T.A.G.S :)

I also have a script to parse this CSV file: I need to extract emojis, hashtags, links, retweets, mentions, and many more details I need to compute for each tweet (it's for my research project: digital affectiveness. I've already stored 280k tweets, with 170k emojis inside).

Then each tweet and its metrics are saved in a database (table TWEETS), as well as emojis (table EMOJIS), as well as account stats (table BRANDS).

I use a class quite similar to this one: CsvImporter > https://gist.github.com/Tazeg/b1db2c634651c574e0f8. I made a loop to parse each line 1 by 1.

$importer = new CsvImporter($uploadfile,true); 
        while($content = $importer->get(1)) {
            $pack = $content[0];
            $data = array();
            foreach($pack as $key=>$value) {
                $data[]= $value;
            }
            $id_str = $data[0];
            $from_user = $data[1];

...

After all my computs, I "INSERT INTO TWEETS VALUES(...)", same with EMOJIS. The after, I have to make some other operations

  1. update reach for each id_str, if a tweet I saved is a reply to a previous tweet)
  2. save stats to table BRAND

All these operations are scripted in a single file, insert.php, and triggered when I submit my upload form. But everything falls down if there is too many tweets. My server cannot handle so long operations.

So I wonder if I can ajaxify parts of the process, especially the loop

  1. upload the file
  2. parse 1 CSV line and save it in SQL and display a 'OK' message each time a tweet is saved
  3. compute all other things (reach and brand stats)

I'm not enough aware of $.ajax() but I guess there is something to do with beforeSend, success, complete and all the Ajax Events. Or maybe am I completely wrong!?

Is there anybody who can help me?

  • 写回答

2条回答 默认 最新

  • dqnrk44682 2018-04-05 15:23
    关注

    As far as I can tell, you can lighten the load on your server substantially because $pack is an array of values already, and there is no need to do the key value loop.

    You can also write the mapping of values from the CSV row more idiomatically. Unless you know the CSV file is likely to be huge, you should also do multiple lines

    $importer = new CsvImporter($uploadfile, true);
    // get as many lines as possible at once...
    while ($content = $importer->get()) {
        // this loop works whether you get 1 row or many...
        foreach ($content as $pack) {
            list($id_str, $from_user, ...) = $pack;
            // rest of your line processing and SQL inserts here....
        }
    }
    

    You could also go on from this and insert multiple lines into your database in a single INSERT statement, which is supported by most SQL databases.

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗