1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Read and Sort before to save records

Discussion in 'PHP' started by piropeator, May 20, 2016.

  1. #1
    Hi everyone!!
    I have this:
    if(file_exists("filename.csv")) {
    
        $registro = fopen("filename.csv", "r");
        $BD = new ConexionDB();
        while (($data = fgetcsv($registro, 200, ",")) !== FALSE) {
            $sth = $BD->prepare("INSERT INTO table_temp (codigo, name) VALUES (:codigo, :name)");
            $codigo = $data[0];
            $name   = $data[1];
            $sth->bindParam(':codigo', $codigo);
            $sth->bindParam(':name', $name);
            $sth->execute();
        }
    }
    PHP:
    The source data (csv) is not sorted. I want to sort from "codigo" before to insert into the table.
    How I do that??
    Thanks.
     
    piropeator, May 20, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Why...? The database doesn't care, just sort it when you pull data instead?
     
    PoPSiCLe, May 20, 2016 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    Well, This is a temporal table and the data should be sorted. For that.
     
    piropeator, May 20, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Well, yes, but it still doesn't really matter - if you need a sort, again, just sort it when you use the table (although, unless there are thousands of records, all being affected, again, it doesn't really matter that much).

    However, if you really wanna sort it before you input it, you can do this (it works, however it might be a bit cumbersome - I'm tired :) )
    
    <?php
    if(file_exists("filename.csv")) {
    
      $BD = new ConexionDB();
      $data = file("filename.csv");
      foreach ($data as $key => $value) {
      $content = explode(",",$value);
      $newdata[$content[0]] = str_replace('"','',$content[1]);
      }
      ksort($newdata);
    
      $sth = $BD->prepare("INSERT INTO table_temp (codigo, name) VALUES (:codigo, :name)");
      foreach ($newdata as $key => $value) {
      $codigo = $key;
      $name  = $value;
      $sth->bindParam(':codigo', $codigo);
      $sth->bindParam(':name', $name);
      $sth->execute();
      }
    }
    
    ?>
    
    PHP:
     
    Last edited: May 20, 2016
    PoPSiCLe, May 20, 2016 IP
  5. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #5
    before anything else, can you just sort the data in a spreadsheet?
     
    Blizzardofozz, May 21, 2016 IP
  6. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #6
    The requirement is to load the data from csv file and sort it before save. I solved making an "order by code asc" at the end of the sentence.
    :)
     
    piropeator, May 23, 2016 IP