...

View Full Version : <textarea> csv to DB



Yakisoba
04-25-2006, 06:30 AM
Hey Folks,

I have a fairly large table generated with JavaScript...users can enter/delete/change data at thier descretion. Once the user has completed any modifications I would like to give them the ability to save to a DB.
So far I have Included a "Export to CSV" button that converts all the table data into CSV format and sends the output to a <textarea>.

What I would like to do next is take the CSV data inside the <textarea> and send it to my DB...
(i.e. after the data is converted to CSV the user then has the option of clicking the "Save to DB" button)

I'm not sure where to start in terms of getting things into thier respective cols...

Anyone have a suggestion on how to accomplish this?


Thanks,

Yakisoba

felgall
04-25-2006, 07:55 AM
They need to submit the form with the CSV data in the text area to a server side script and that script then does the database update. This can be done either by reloading a whole new page or via Ajax.

Yakisoba
04-25-2006, 08:45 AM
Thanks for the quick response felgall.


They need to submit the form with the CSV data in the text area to a server side script and that script then does the database update. This can be done either by reloading a whole new page or via Ajax.

This much is already understood...(Sorry, I should have been clearer in my initial post).

Ideally I would like the "save" feature to function asynchronously...as does the rest of my page...

The idea I am working with now involves this order of operation:
1. Convert/display CSV data into <textarea> (this is complete)
2. Submit via <form> CSV data contained in <textarea> to .php script (no problem)
3. .php script to split the CSV data into an array (this is somewhat working, but I can't get it to play nice with step 4)
4. .php script to build a mySQL INSERT query
5. INSERT values in TABLE (this should not be a problem, must complete the above steps first though)
6. Refresh browser page to reflect changes (this is complete. if I manually enter data into my DB the changes are reflected when the page loads)

As you can see steps 3 and 4 are my biggest challenges at the moment.

Any suggestions?

Thanks,

Yakisoba

felgall
04-25-2006, 11:19 PM
Well to insert them into the database you need a comma separated list of values in the same order as the fields you are loading into so a single database call something like this should handle 3 and 4.

INSERT INTO tablename ($field1, $field2, $field3) VALUES ($csvlist);

Yakisoba
04-26-2006, 09:52 AM
Well to insert them into the database you need a comma separated list of values in the same order as the fields you are loading

Got that...



something like this should handle 3 and 4.

INSERT INTO tablename ($field1, $field2, $field3) VALUES ($csvlist);


I think the above is more what I was thinking for number 5 on my list...

from my last post

1. Convert/display CSV data into <textarea> (this is complete)

so I have a <textarea> that contains CSV output (actually SCSV ";" output, but at the moment that doesn't matter)

example data displayed in the <textarea name=csv> would look like this:
;1;2;3;4;5
;1;2;3;4;5
;1;2;3;4;5
;1;2;3;4;5

each col is seperated by ";" and each row is seperated by "\r\n"....

so $_POST["csv"] =;1;2;3;4;5 ;1;2;3;4;5 ;1;2;3;4;5 ;1;2;3;4;5

now I would like to take that and split them into thier respective rows/cols and put them into an array...is this possible?

Could I first split the data one row at a time, then place that row in an array, then insert the data into my db?


This is what I've been trying with no luck: (not the prettiest so go easy;)


<?php

include 'db_connect_item.php';

function data_split($buff)
{
$length = strlen($buff);
$i = 0;
while ($length >= 1)
{
$pos2 = strpos($buff, ';');
if (!$pos2)
{
$fields[$i] = $buff;
$length = - 1;
}
else
{
$fields[$i] = substr($buff, 0, $pos2);
$buff = substr($buff, ($pos2 + 1));
$length = strlen($buff);
}
$i++;
}
return $fields;
}


if ($_POST["csv"] == null)
{
echo "Export to CSV first.";
}
else
{

$csv = $_POST["csv"];
$pos1 = 0;
$pos2 = 1;
while ($pos2)
{
$pos2 = strpos($csv, "\r\n");
$buffer = substr($csv, $pos1, $pos2);
$csv = substr($csv, ($pos2 + 1));
if (!$buff) $buff = $csv;
$fields = data_split($buff);
$name = $fields[4];
$sql = "insert into item (player)
values('$name')";
$result = mysql_query($sql) or die ("CRASH!!!!!".mysql_error());
}
echo "<p>Data Stored! <br>";
}

?>



With the above code i was trying to INSERT $name into col player...the player name would be in the 4th position in the array...

So as it stands now ... I can send the data of $csv into all of that but, not getting anything useful in return...currently all my field values are null...

Any suggestions are appreciated.

Thanks

Yakisoba

GJay
04-26-2006, 12:23 PM
take a look at fgetcsv and explode for getting what you want.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum