Insert unique code to a field while import csv file
Insert unique code to a field while import csv file
Hi,
I have two php functions which generate a unique code, so I use these function with the datatables editor and it work fine, but when import data as csv file the field of unique code get the same code and when add manually the code would be unique.
Note: I added the two functions at the begining of the server site file and use "setValue( generateKey() )" to call function for unique code and save into db.
Thanks in advance
This discussion has been closed.
Replies
Could you post your code, please, so we can take a look.
Colin
Hi colin,
Here my full code:
client side:
server side:
I’m sorry, it is probably too early in the morning here, but I’m not understanding that. Could you explain a bit more please?
Allan
Hi allan,
I meant when insert several rows into table via "import csv" the field "student_code" take the same code and it must be defferent at each row.
Example:
First row: student_name= Mark, student_code= RR12TYUI
Second row: student_name= Jane, student_code= RR12TYUI
And so on
As you see the "student_code" take the same code.
thanks
Right - I think I understand what is going on now - thanks for the clarification. The way the CSV import example works is that it uses Editor's multi-row editing ability. That means it will submit all of the imported rows in a single request.
You are currently only calling
generateKey()
once and setting the value to be the same for all rows. Hence why it is the same for all rows from an import.What I think you will need to do here is use a
preCreate
event listener in the PHP code to modify the value for each row being submitted - e.g.:This will work as
preCreate
is called once per row submitted.Allan
thanks allan
Hi allan
it worked perfectly after adding "use ($connwdb)" because this variable is out of function's scope.
thanks again
Hi allan,
I have another problem with "import csv" I link the "students" table with "gender" table when import csv to "students" table all text and number fields such as name, email, mobile number become number, so my question here does import csv work with linked tables or not?
if yes, could you please help me to solve this problem.
Thanks
I always forget about the variable scoping in PHP! Good to hear that (almost) worked).
The example import to CSV doesn’t take into account linked tables. If your CSV had the linked primary key in it, then it would just work as is. But if you need to lookup the linked primary key, that is something that would need to be added into the import processing code at the server-side before Editor sees it to insert into the database - in
preCreate
for example.Allan
Thanks alot
allan