php - Assigning autonumber to HTML form entry -
i have searched everywhere, nothing find seems solve this. have html web form (in php document) writes data csv file, , below form table filters csv data in based on key word. have no problems existing code part. however, need have auto-number function assigns number each form. need on start. i'm still relatively new coding, great. edit: here code use write data csv file.
if($_post['formsubmit'] == "submit") { $fs = fopen("fixturerequests.csv","a"); fwrite($fs,$varfixnum . ", " . $varrequester . ", " . $vardept . ", " . $varsupervisor . ", " . $vardesc . ", " . $varparts . ", " . $varwc . ", " . $varaddinfo . ", " . $vardatereq . ", " . $vardateneed . ", " .$varstatus . "\n"); fclose($fs); header("location: successfullysubmitted.php"); exit; }
any guidance excellent. thank you.
you can use function
function next_available_form_id(){ $rows = file('fixturerequests.csv'); //put our csv file array if(empty($rows)) return 1; //if our csv empty start 1 $data = str_getcsv(array_pop($rows)); //array_pop gets last row return $data[0]+1; //we first field , add 1 //just use field store form number //e.g if store form number in //4th field replace $data[0] $data[3] }
based on code provided can use function provided next form_id before storing in csv file.just make modification code after opening csv file :
$fs = fopen("fixturerequests.csv","a"); $form_id=next_available_form_id(); //add next available id //and insert $form_id first field in csv file fwrite($form_id,$fs,$varfixnum . ", " . $varrequester . ", " . $vardept . ", " . $varsupervisor . ", " . $vardesc . ", " . $varparts . ", " . $varwc . ", " . $varaddinfo . ", " . $vardatereq . ", " . $vardateneed . ", " .$varstatus . "\n");
notice: of course since csv have not have form_id first field should either create csv file scratch or add form numbers in existing records.in example use awk that:
awk '{printf "%d,%s\n", nr, $0}' < fixturerequests.csv
Comments
Post a Comment