Forum Moderators: coopster
this is my script
<?php
function display_menu() {?>
<form enctype ="multipart/form-data">
CSV Import: <input type=file name=csvfile>
<input type=submit name=action value= " CSV Import ">
</form>
<? }
function action_csv_import(){
global $csvfile;
$csvfile = stripslashes($csvfile);
if ($csvfile!= ""){
$nr = csv_import($csvfile);
if ($nr > 0) {
echo "import successful - $nr entries imported.<p>";
}
else if ($nr == 0) {
echo "Import did not find any entries.<p>";
}
else {
echo "import failed.<p>";
}
} else {
echo "Please select a file and try again.<p>";
}
}
function csv_import($path){
global $dbpath, $dbtype, $vars;
$db = dba_open($dbpath, "c", $dbtype, "0644");
if (!$db){
echo "db open failed.<p>";
return -1;
}
$fp = fopen($path, "r");
if (!$fp) {
dba_close($db);
echo "Cannot open CSV file ($path);
return -1;
}
$id = get_next_id($db);
$nr_entries = 0;
while(!feof($fp)){
$data = fgetcsv($fp, 4096);
if (is_array($data) && count($data) > 0){
$new = array();
for ($i = 0; $i < count($data); $i++){
$new['$vars[$i]'] = $data[$i];
}
$new["id"] = $id;
dba_replace($id, serialize($new), $db);
$id++;
$nr_entries++;
}
}
fclose($fp);
dba_sync($db);
dba_close($db);
return $nr_entries;
}
$dbpath = "upproduct.db";
$dbtype = "db2";
$data = array();
$vars = array("uppID", "userid", "prodNames", "prodPrices");
for ($i = 0; $i < count($vars); $i++){
$data[$vars[$i]] = ${$vars[$i]};
}
display_menu();
$action = strtolower(trim($action));
switch($action){
case "search":
action_search($word);
break;
case "csv import":
action_csv_import();
break;
}
?>
but it returned error on this part $new['$vars[$i]'] = $data[$i];. the error message is Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in line 61.
anyone please help me out on this issue?
thank you very much.