Back to top

Autore Topic: [risolto] importazione utenti da csv funzione explode  (Letto 4859 volte)

pippo30

  • Visitatore
Re:importazione utenti da csv funzione explode
« Risposta #20 il: 23 Apr 2013, 11:24:52 »
Codice: [Seleziona]

<?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 "e:/xampp/htdocs/users2import.csv")
        {
            
/* Check the user file exists */
            /* Open the file */

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

            
$handle fopen($source,"r");
            
$content fread ($handle,filesize $source ));  
             
            
/* Get the file, line by line */
            
$lines explode("\n"$content);

            
fclose($handle); 
            
/* Counter of users added */
            
$added 0;
             
            
/* Iterate through each line of the data file */
            
for ($i 0$i count($lines); $i++){// as $key => $line){
                /* Split the line by comma */
                
$properties explode(",",$lines[$i]);

                
/* Build a new user object */
                
$user = new JUser();
                
$data = array();
                
$data['name'] = $properties[0];
                
$data['username'] = $properties[1];
                
$data['email'] = $properties[2];
                
$data['password'] = $properties[3];

                
// Load the users plugin group.
                
JPluginHelper::importPlugin('user');
                 
                
/* Set the values */
                
$user->groups = array(2);

                if (!
$user->bind($data))
                {
                    
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_BIND_FAILED'$user->getError()));
                    return 
false;
                }
                 
                
$user->registerDate date('Y-m-d H:i:s');
                
                if (!
$user->save())
                {
                    
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED'$user->getError()));
                    return 
false;
                }
                else 
$added++;
                
$user null;
            }

            if(
$added) return $added;
 
            
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED'"Ci sono stati degli errori nella importazione"));
            return 
false
        }
}



ho usato lo stesso metodo di joomla

Codice: [Seleziona]


/* Iterate through each line of the data file */
            for ($i = 0; $i < count($lines); $i++){// as $key => $line){
                /* Split the line by comma */
                $properties = explode(",",$lines[$i]


alla fine riga del csv una , e un carattere e funziona
« Ultima modifica: 23 Apr 2013, 11:33:02 da Marco31 »

 



Web Design Bolzano Kreatif