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.
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:
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.