1. Save data from .CSV file to database using php
public function savefile(){
die('Remove die to action');
error_reporting(E_ALL ^ E_NOTICE);
if(!defined('DS')) {
define('DS',DIRECTORY_SEPARATOR);
}
mysql_connect('localhost', 'dbuser', 'db_password');
mysql_select_db('db_name');
$db = JFactory::getDBO(); // connect to databese
$date =& JFactory::getDate();
$csvfile = JPATH_SITE.DS.'media'.DS.'batch_6_7.csv'; // File name with path
$getfile = fopen($csvfile, "r"); // open file in read mode
// get all column of row with row by row
while (($slice = fgetcsv($getfile, 1000, ",")) !== FALSE) {
$schoolcode = intval(trim($slice[0]));
$schoolname = trim($slice[1]);
$telephone = $db->quote(trim($slice[2]));
$fax = $db->quote(trim($slice[3]));
$emailaddress = $db->quote(trim($slice[4]));
$website = $db->quote(trim($slice[5]));
$schoolstate = $db->quote(trim($slice[6]));
$province = $db->quote(trim($slice[7]));
$country = $db->quote(trim($slice[8]));
$city = $db->quote(trim($slice[9]));
$address = $db->quote(trim($slice[10]));
$postalcode = $db->quote(trim($slice[11]));
$nature = $db->quote(trim($slice[12]));
$concerted = $db->quote(trim($slice[13]));
$schooltype = $db->quote(trim($slice[14]));
$genericdomination = $db->quote(trim($slice[15]));
$codens = $db->quote(trim($slice[16]));
$codprovincia = $db->quote(trim($slice[17]));
$codcen = $db->quote(trim($slice[18]));
$denomination = $db->quote(trim($slice[19]));
$address2 = $db->quote(trim($slice[20]));
$latitude = $db->quote(trim($slice[21]));
$longitude = $db->quote(trim($slice[22]));
$precision = $db->quote(trim($slice[23]));
$binf2 = $db->quote(trim($slice[24]));
$ini = $db->quote(trim($slice[25]));
if(empty($schoolname)){
$schoolname = 'Untitled';
}
$title = $db->quote($schoolname);
$alias = $db->quote(strtolower(str_replace(' ','-',trim($schoolname))));
$content = $db->quote($article);
// image File uploading code
$uploadedFile = glob('media/schoolreviews/'.$schoolcode.'.*');
$addFile = $uploadedFile[0];
$imagesText = array(
'image_intro' => $addFile,
'float_intro' => '',
'image_intro_alt' => '',
'image_intro_caption' => '',
'image_fulltext' => '',
'float_fulltext' => $addFile,
'image_fulltext_alt' => '',
'image_fulltext_caption' => ''
);
$query = sprintf("INSERT INTO mhr0y_content (title,catid,introtext,state,alias,images,publish_up,access,language,created_by) VALUES (%s,%d,%s,%d,%s,%s,%s,%d,%s,%d);",
$title,
78,
$content,
1,
$alias,
$db->quote(json_encode($imagesText)),
$db->quote($date->Format('Y-m-d H:i:s')),
1,
$db->quote('*'),
958
);
mysql_query($query);
$articleId = mysql_insert_id();
$query = sprintf("SELECT * From #__assets ORDER BY id DESC LIMIT 0,1");
$db->setQuery($query);
$record = $db->loadObject();
$id = $record->id + 1;
$parentId = 163;
$lft = $record->lft + 1;
$rgt = $record->rgt + 1;
$level = 3;
$name = $db->quote('com_content.article.'.$articleId);
$rules = $db->quote('{"core.delete":{"6":1},"core.edit":{"6":1,"4":1},"core.edit.state":{"6":1,"5":1}}');
$query = sprintf("INSERT INTO mhr0y_assets (id,parent_id,lft,rgt,level,name,title,rules) VALUES (%d,%d,%d,%d,%d,%s,%s,%s);",
$id, $parentId, $lft, $rgt, $level, $name, $title, $rules
);
mysql_query($query);
$assetId = mysql_insert_id();
$query = sprintf("UPDATE mhr0y_content SET asset_id = %d WHERE id = %d;", $assetId, $articleId);
mysql_query($query);
$query = sprintf("INSERT INTO mhr0y_schoolreviews_schoolreview (`schoolcode`, `schoolname`, `telephone`, `fax`, `emailaddress`, `website`, `schoolstate`, `province`, `country`, `city`, `address`, `postalcode`, `nature`, `concerted`, `schooltype`, `genericdenomination`, `codens`, `codprovincia`, `codcen`, `denomination`, `address2`, `latitude`, `longitude`, `precision`, `binf2`, `ini`, `articleId`)
VALUES(%d,%s,%s,%s,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s ,%s)
",
$schoolcode, $title, $telephone, $fax, $emailaddress, $website, $schoolstate, $province, $country, $city, $address, $postalcode, $nature, $concerted,
$schooltype, $genericdomination, $codens, $codprovincia, $codcen, $denomination, $address2, $latitude, $longitude, $precision, $binf2, $ini, $articleId
);
mysql_query($query);
}
die('Endof function');
}
// end coding here you can customize according to your choice image uploading optional
No comments:
Post a Comment