|
|
 |
|
 |
|
x10 Lieutenant
Join Date: Oct 2007
Posts: 438
Credits:
4,200
|
|
09-24-2008, 06:57 PM
|
|
Re: Insert records from a local csv file
Quote:
Originally Posted by freecrm
Thanks Guys..
My code now looks like this...
PHP Code:
//connect to db or show error
mysql_select_db($database_freecrm, $freecrm)
or die(mysql_error());
//if form hidden field returns a value, execute the following script
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
//allocate posted file path to variable
$file=$_POST['filepath'];
//load file and split into array
$arrfile = explode(',',file_get_contents($file));
//loop through each line
foreach($arrfile as $value){
//define column values
$firstname=value[1];
$lastname=value[2];
$company=value[3];
//insert into db
$insert="INSERT INTO TEST (FIRSTNAME, LASTNAME, COMPANY) VALUES ('$firstname','$lastname','$company')";
mysql_query($insert) OR die(mysql_error());
}
}
I have tried several versions of $something=$value[1] but I can't get it right. The square brackets are returning an error.
Code:
Parse error: syntax error, unexpected '[' in /home/freecrm/public_html/crmimexport/contactimport.php
I have tried with (), and without brackets but no difference.
Just one thing to bear in mind, field (column) 1 is an autoincrement Integer ID and not specified in the csv file.
I checked out the page on the php site but it's all gobbledegook to me!!! I would prefer to understand what I'm doing rather than just copy lines and lines of strange code... 
|
Try just above '//define column values':
$value=explode(",",$value);
and changing $arrfile=explode(",", ...
to $arrfile=explode("\n", ...
and it's worth trying to understand at least some of the php docs, at least after a little experience. It can take you far!
Good luck!
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com
|
|
|
|
x10 Lieutenant
Join Date: May 2008
Posts: 272
Credits:
956
Location: UK
|
|
09-27-2008, 02:25 PM
|
|
Re: Insert records from a local csv file
Quote:
Originally Posted by Scoochi2
It should work for any file. Just make sure you use an absolute rather than a relative path.
That being said, it depends on how the sever the script is hosted on whether it will for for files hosted elsewhere.
I think on X10 you need the intermediate level PHP (at least).
You do not need to change the function at all. You specify the filename when you call the function. For example:
PHP Code:
$data = explode_by_lines('http://arandomdomain.com/directory/testdata.csv');
|
OK - have changed the code to the following:
PHP Code:
<?php //connect to db or show error mysql_select_db($database_freecrm, $freecrm) or die(mysql_error()); //if form hidden field returns a value, execute the following script if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $filepath = $_POST['filepath']; //allocate form path to variable function explode_by_lines($filename) { $data_1 = array(); foreach (file($filename) as $array) { $data_1[] = explode(",",str_replace('"','',$array)); } return $data_1; } $data = explode_by_lines('$filepath'); print_r($data);
//insert into db //$insert="INSERT INTO TEST (FIRSTNAME, LASTNAME, COMPANY) VALUES ('$firstname','$lastname','$company')"; //mysql_query($insert) OR die(mysql_error()); } ?>
but still no joy... :(
The file path I used was
http://www.freecrm.x10hosting.com/testdata.csv
but I still get the same error, where it is looking for a file local to the script...
I do have intermediate level as I use putenv() functions.
not sure where to go with this now...
Edit:
Quote:
Originally Posted by mattura
Try just above '//define column values':
$value=explode(",",$value);
and changing $arrfile=explode(",", ...
to $arrfile=explode("\n", ...
and it's worth trying to understand at least some of the php docs, at least after a little experience. It can take you far!
Good luck!
|
Thanks Matt
Still getting the same error
Code:
Parse error: syntax error, unexpected '[' in /home/freecrm/public_html/crmimexport/contactimport.php on line 31
Line 31 is
PHP Code:
$firstname=value[1];
So still no joy although I see where you were going with that... 
__________________
Rich
If you liked this post, please give me +rep or donate credits.
My Site:
Last edited by freecrm; 09-27-2008 at 02:25 PM.
Reason: Automerged Doublepost
|
|
|
|
x10 Sophmore
Join Date: Sep 2008
Posts: 110
Credits:
1,305
|
|
09-27-2008, 08:53 PM
|
|
Re: Insert records from a local csv file
Quote:
|
Originally Posted by freecrm
Still getting the same error
Code:
Parse error: syntax error, unexpected '[' in /home/freecrm/public_html/crmimexport/contactimport.php on line 31
Line 31 is
PHP Code:
$firstname=value[1];
So still no joy although I see where you were going with that... 
|
I think what is causing the error is value[1] instead of $value[1] you simply forgot the $ for variable.
I tried to check your code and added some stuffs so I could check it. And I put stuffs in functions so I could use the code again in the future.
But your code is still unchanged except for a few that I added. You can test it here if you like:
testing using your csv just input it in the box http://www.freecrm.x10hosting.com/testdata.csv (the last line is a newline with no data that's why you'll get a row with empty strings.
index.html - form I just made up blah
test.php - the one called by "post" method of form
functions.php - some stuffs
PHP Code:
<?php /** * Some functions I just made up just for testing^^^ * Put them on separate file and just use require. * file: functions.php * - natsuki^^ */
// this function simply creates the table test needed function create_table($host, $user, $pass, $db, $table) { $link = mysql_connect_db($host, $user, $pass); if (!$link) { die('error!'); } mysql_select_db($db, $link) or die(mysql_error()); $sql = 'CREATE TABLE ' . $table . ' ( id INT NOT NULL AUTO_INCREMENT, FIRSTNAME VARCHAR(40) NULL, LASTNAME VARCHAR(40) NULL, COMPANY VARCHAR(40) NULL, PRIMARY KEY(`id`) )'; mysql_query($sql) or die(mysql_error()); return true; }
// simply puts every line in an array function get_data_per_line($string) { $string = explode("\n", $string); return $string; }
// make string db-safe function format_db_string($string) { $string = trim($string); if (!get_magic_quotes_gpc()) { $string = addslashes($string); } return $string; }
// to connect to MySQL db function mysql_connect_db($hostname = 'localhost', $user = '', $pass = '') { $link = mysql_connect($hostname, $user, $pass); if (!$link) { die('Could not connect to database: ' . mysql_error()); } return $link; }
?>
PHP Code:
<?php # ------------------------------------------- start addition ----------- // file: test.php
// require functions.php require 'functions.php';
// just setting the test variables: define('_USER_', 'natsuki'); // constant
$_host = 'localhost'; $_db = _USER_ . '_test'; $_user = _USER_ . '_test'; $_pass = 'test';
$database_freecrm = $_db;
// connect to db to get link $freecrm = mysql_connect_db($_host, $_user, $_pass); # ------------------------------------------- end addition -------------------- //connect to db or show error mysql_select_db($database_freecrm, $freecrm) or die(mysql_error()); //if form hidden field returns a value, execute the following script if (isset($_POST['MM_insert']) && ($_POST['MM_insert'] == 'form1')) { //allocate posted file path to variable $file = $_POST['filepath']; // load file and split into array per newline "\n" $temp = file_get_contents($file); $temp = get_data_per_line($temp); // or just use $arrfile from the start. I used temp because it's shorter xD $arrfile = array(); // explicit declaration as array $arrfile = $temp; //$arrfile = explode(',',file_get_contents($file)); //
//loop through each line foreach ($arrfile as $value) { // explicit array, to prevent $value from being an array itself $data = array(); $data = explode(',', $value); // separate data per ',' //define column value $firstname = str_replace('"', '', $data[0]); // just making safe $lastname = str_replace('"', '', $data[1]); $company = str_replace('"', '', $data[2]); $firstname = format_db_string($firstname); // format string for db use $lastname = format_db_string($lastname); $company = format_db_string($company); //insert into db $insert = "INSERT INTO TEST (FIRSTNAME, LASTNAME, COMPANY) VALUES ('$firstname', '$lastname', '$company');"; mysql_query($insert) OR die(mysql_error()); } echo "Success putting data from csv $file to database $database_freecrm and table TEST!"; } ?>
Just remember that the table I made is this:
PHP Code:
$sql = 'CREATE TABLE ' . $table . ' ( id INT NOT NULL AUTO_INCREMENT, FIRSTNAME VARCHAR(40) NULL, LASTNAME VARCHAR(40) NULL, COMPANY VARCHAR(40) NULL, PRIMARY KEY(`id`) )';
It's very important to have the primary key set to auto increment so you won't get duplicate key errors
^^such a long post^^
|
|
|
|
x10 Lieutenant
Join Date: May 2008
Posts: 272
Credits:
956
Location: UK
|
|
09-28-2008, 04:23 PM
|
|
Re: Insert records from a local csv file
Natsuki - you are an absolute star!!!!!!!!! (And I'm very thankful for your time and am sending you 200 creds - not much I know but I don't have many!)
It works a treat and no problems. I tidied up the data file to get rid of the extra line...
Yay!!!!!!!!!!!!!!!
Just one quick question... how do I ignore the first line??
__________________
Rich
If you liked this post, please give me +rep or donate credits.
My Site:
Last edited by freecrm; 09-28-2008 at 04:26 PM.
|
|
|
|
x10 Sophmore
Join Date: Sep 2008
Posts: 110
Credits:
1,305
|
|
09-28-2008, 04:52 PM
|
|
Re: Insert records from a local csv file
you can do it with the unset() function then reindex the array with array_values() just change the function get_data_per_line to this:
PHP Code:
// simply puts every line in an array and deliberately ignores 1st line of $string function get_data_per_line($string) { $string = explode("\n", $string); unset($string[0]); $string = array_values($string); return $string; }
or you can add it in the main script, so you can still use get_data_per_line() somewhere else just change the line to this
PHP Code:
// load file and split into array per newline "\n" $temp = file_get_contents($file); $temp = get_data_per_line($temp); unset($temp[0]); $temp = array_values($temp);
^^
Last edited by natsuki; 09-28-2008 at 05:06 PM.
|
|
|
|
x10 Lieutenant
Join Date: May 2008
Posts: 272
Credits:
956
Location: UK
|
|
09-28-2008, 06:06 PM
|
|
Re: Insert records from a local csv file
Perfect - many thanks...
Edit:
Please close this thread.
__________________
Rich
If you liked this post, please give me +rep or donate credits.
My Site:
Last edited by freecrm; 09-28-2008 at 06:06 PM.
Reason: Automerged Doublepost
|
|
|
|
x10 Lieutenant
Join Date: Oct 2007
Posts: 438
Credits:
4,200
|
|
09-29-2008, 05:21 AM
|
|
Re: Insert records from a local csv file
Yep, well done natsuki for spotting that missing $.
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com
|
|
|
|
x10 Lieutenant
Join Date: May 2008
Posts: 272
Credits:
956
Location: UK
|
|
09-29-2008, 07:49 AM
|
|
Re: Insert records from a local csv file
Quote:
Originally Posted by mattura
Yep, well done natsuki for spotting that missing $.
|
Lol - I detect a note of sarcasm here!
I am also thankful for your efforts and have given you 100 creds. He did write the entire script that worked though including some additional validation...
__________________
Rich
If you liked this post, please give me +rep or donate credits.
My Site:
|
|
|
|
x10 Sophmore
Join Date: Sep 2008
Posts: 110
Credits:
1,305
|
|
09-29-2008, 01:34 PM
|
|
Re: Insert records from a local csv file
even if you put $ value wasn't an array anyway so you get no values for index xp
I plan on making a csv thingy class but it won't be as simple as this ^^
|
|
|
|
x10 Lieutenant
Join Date: Oct 2007
Posts: 438
Credits:
4,200
|
|
09-30-2008, 08:24 AM
|
|
Re: Insert records from a local csv file
Quote:
Originally Posted by freecrm
Lol - I detect a note of sarcasm here! 
|
haha! No, it's just I was away for a while and natsuki posted the reply!
It would be an array after making the suggested change:
PHP Code:
$value=explode(",",$value);
Well I'm very grateful for the credits, not sure I deserve them...
__________________
----
Please donate credits if you had a really good reply from me! Rep is also appreciated. Thanks
matt.elementfx.com
|
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
It works! ...
|
bpakidz |
Programming Help |
4 |
09-08-2008 12:12 AM |
|
Internal Serve Error
|
xaakx |
Free Hosting |
6 |
02-03-2008 09:02 AM |
|
MySQL Issues Here
|
Corey |
Server Alerts |
304 |
01-06-2008 10:10 PM |
|
help!!
|
retro-bliss |
Free Hosting |
25 |
12-07-2007 02:12 PM |
|
php version
|
loveispoison |
Free Hosting |
10 |
11-21-2007 11:53 AM |
All times are GMT -5. The time now is 06:39 PM.
Powered by vBulletin® Version 3.7.3 Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
Actress | Loans | Remortgages | Loans | Turbo Tax
| |