explode

marzar

Utente Attivo
8 Ott 2010
53
0
6
ho fatto un componente di jooma per importatre utenti temite csv il problema che ho e che in questo momwnto per cone vedete i campi son separati semplicemente da una ,
Codice:
if (function_exists('str_getcsv'))
$line = str_getcsv($line);
else
$line = preg_split('/,/', $line);

io vorrei fare così

Codice:
$user = new JUser();
$line = “nome cognome, nomeutente, [email protected], password, bla bla”; //supponiamo che questa è la riga da importare
$properties = explode(“,”, $line);
$user->name = $properties[0];
$user->username = $properties[1];
$user->email = $properties[2];
$user->password = $properties[3];
$user->…. = $properties[n];

Codice:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
/**
 * General Controller of JUserImporter component
 */
class JUserImporterController extends JControllerLegacy
{
        /**
         * display task
         *
         * @return void
         */
        function display($cachable = false, $urlparams = array()) 
        {
                // set default view if not set
                $input = JFactory::getApplication()->input;
                $input->set('view', $input->getCmd('view', 'JUserImporter'));
 
                // call parent behavior
                parent::display($cachable, $urlparams);
        }
		
		/**
		 * Import task
		 */
		function importUsers($source = "c:/xampp/htdocs/users2import.csv")
		{
			/* Check the user file exists */
if (file_exists( $source ) && !isset($_REQUEST['srcfile'])) {
/* Open the file */

if(!file_exists($source)) $source = $_REQUEST['srcfile'];

$handle = fopen($userFile,"r");
$content = fread ($handle,filesize ( $source ));
fclose($handle);   
 
/* Get the file, line by line */
$lines = explode("\n", $content);
 
/* Remove the first line (headers only) */
unset($lines[0]);
 
/* Counter of users added */
$added = 0;
 
/* Iterate through each line of the data file */
foreach ($lines as $key => $line){
/* Split the line by comma */
if (function_exists('str_getcsv'))
$line = str_getcsv($line);
else
$line = preg_split('/,/', $line);//come in serisco la funzione explode al posto di questa riga
 
/* Build a new user object */
$user = new JUser();
$user->id = null;
 
/* Set the values */
$user->name = $line[2];
$user->username = $line[12];
$user->email = $line[11];
$user->groups = array(2);
 
/* Build the password */
$salt       = JUserHelper::genRandomPassword(32);
$crypted    = JUserHelper::getCryptedPassword( preg_replace("/\s/", "", strtolower($line[13])) , $salt);
$newpassword= $crypted . ':' . $salt;
 
$user->password = $newpassword;
 
$user->usertype = "deprecated";
$user->registerDate = date('Y-m-d H:i:s');
 
if(!$user->save()) JError::raiseError(500,$user->getError() );   
else $added++;
}

return $added;
}else{
	JError::raiseError(403, JText::_('File CSV da importare mancante.'));
	return false;
}
 
		JError::raiseError(403, JText::_('Ci sono stati deli errori.'));
		return false; 
		}
}
 

Discussioni simili