web-dev-qa-db-fra.com

Insertion de plusieurs colonnes

J'ai plusieurs entrées de fichier personnalisées dans mon module, id aiment enregistrer toutes ces entrées dans ma table de base de données ("Table personnalisée, pas les tables Joomla"), ci-dessous est mon code, qui enregistre une seule entrée, il y a 4 entrées:

helper.php

class moduploaddocumentsHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */
    public static function getdata()
    {
       //Retrieve file details from uploaded file, sent from upload form
        $file = JRequest::getVar('highestacademic', null, 'files', 'array');

        //Import filesystem libraries. Perhaps not necessary, but does not hurt
        jimport('joomla.filesystem.file');

        //Clean up filename to get rid of strange characters like spaces etc
        $filename =  uniqid().JFile::makeSafe($file['name']);
        //Set up the source and destination of the file
        $src = $file['tmp_name'];
        $dest = JPATH_BASE . DS . "media" . DS ."documents" . DS . $filename;


        // $name=$post['highestacademic']['name'] ;
        //First check if the file has the right extension, we need jpg only
        if ( strtolower(JFile::getExt($filename) ) == 'jpg' || strtolower(JFile::getExt($filename) ) == 'png') {
           if ( JFile::upload($src, $dest) ) {
             $user = JFactory::getUser();
             $useid = $user->id ;
             // Obtain a database connection
            $db = JFactory::getDbo();
            // Create a new query object.
            $query = $db->getQuery(true);
            // Insert columns.
            $columns = array('user_id', 'document_name');

            // Insert values.
            $values = array($useid, $db->quote($filename) );
            // Retrieve the shout
            $query
                    ->insert($db->quoteName('#__documents'))
                    ->columns($db->quoteName($columns))
                    ->values(implode(',', $values));
            $db->setQuery($query);
            $db->execute();
            JFactory::getApplication()->enqueueMessage(JText::_('DOCUMENT SUCCESFULLY UPLOADED'), 'Message');

           } else {
             echo "error !"; //Redirect and throw an error message
           }
        } else {
           echo "Wrong extension !"; //Redirect and notify user file is not right extension
        }
         return $dest ;

    }
 }

/ tmpl/default.php

<form  method="post" enctype="multipart/form-data" >
      <div class="form-group">
        <label for="highestacademic">Attachment of Highest Academic Qualifications</label>
        <input name="highestacademic" type="file" id="highestacademic">
        <p class="help-block">Degree, Diploma, Pre-University, GCE O-Levels, etc.</p>
      </div>
      <div class="form-group">
        <label for="language">Attachment of Language Qualifications</label>
        <input name="language" type="file" id="language">
        <p class="help-block">TOEFL, IELTS , etc.</p>
      </div>
      <div class="form-group">
        <label for="passport">Attachment of Passport Copy</label>
        <input name="passport" type="file" id="passport">
        <p class="help-block">ID or Passport copies both sides.</p>
      </div>
      <div class="form-group">
        <label for="supporting">Other Supporting Documents</label>
        <input name="supporting" type="file" id="supporting">
        <p class="help-block">Other supporting documents ie photos ,etc .</p>
      </div>
       <button type="submit" class="btn btn-default">Submit</button>
    </form>
1
Jimmy Obonyo Abor

J'ai finalement résolu ceci en utilisant $jinput->files->get() comme ci-dessous:

Notez le doc[1] Qui place tous les fichiers de téléchargement dans un tableau dans l'objet Jinput ici $files = $jinput->files->get('docs');

helper.php

class moddocumentsHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */
    public static function getdata()
    {
        $jinput = JFactory::getApplication()->input;
       //Retrieve file details from uploaded file, sent from upload form
        //$file = JRequest::getVar('highestacademic', null, 'files', 'array');
         $files = $jinput->files->get('docs');
        //Import filesystem libraries. Perhaps not necessary, but does not hurt
         jimport('joomla.filesystem.file');
        foreach($files as $key => $file ){
          if($file['name'] !== " "){
             // //Clean up filename to get rid of strange characters like spaces etc
               $filename =  uniqid().$file['name'];
              // //Set up the source and destination of the file
                $src = $file['tmp_name'];
                $dest = JPATH_BASE . DS . "media" . DS ."documents" . DS . $filename;



              // //First check if the file has the right extension, we need jpg only
               if ( strtolower(JFile::getExt($filename) ) == 'jpg' || strtolower(JFile::getExt($filename) ) == 'png' || strtolower(JFile::getExt($filename) ) == 'pdf' || strtolower(JFile::getExt($filename) ) == 'doc') {
                   if ( JFile::upload($src, $dest) ) {
                    $user = JFactory::getUser();
                    $useid = $user->id ;
              //     // Obtain a database connection
                    $db = JFactory::getDbo();
              //     // Create a new query object.
                    $query = $db->getQuery(true);
              //     // Insert columns.
                   $columns = array('user_id', 'document_name' ,'type');

              //     // Insert values.
                   $values = array($useid, $db->quote($filename) , $db->quote($key));
              //     // Retrieve the shout
                   $query
                           ->insert($db->quoteName('#__documents'))
                           ->columns($db->quoteName($columns))
                           ->values(implode(',', $values));
                   $db->setQuery($query);
                   $db->execute();

              //     // Redirect to a page of your choice
              //     // header("Location: http://".$_SERVER["HTTP_Host"]."/administrator");
                 } else {
                  echo "error !"; //Redirect and throw an error message
                 }
               } else {
                 echo "Wrong extension !"; //Redirect and notify user file is not right extension
              }
          }

        }


         return $files ;

    }



}

tmpl/default.php

 <form  method="post" enctype="multipart/form-data" >
      <div class="form-group">
        <label for="highestacademic">Attachment of Highest Academic Qualifications</label>
        <input name="docs[1]" type="file" id="highestacademic">
        <p class="help-block">Degree, Diploma, Pre-University, GCE O-Levels, etc.</p>
      </div>
      <div class="form-group">
        <label for="language">Attachment of Language Qualifications</label>
        <input name="docs[2]" type="file" id="language">
        <p class="help-block">TOEFL, IELTS , etc.</p>
      </div>
      <div class="form-group">
        <label for="passport">Attachment of Passport Copy</label>
        <input name="docs[3]" type="file" id="passport">
        <p class="help-block">ID or Passport copies both sides.</p>
      </div>
      <div class="form-group">
        <label for="supporting">Other Supporting Documents</label>
        <input name="docs[4]" type="file" id="supporting">
        <p class="help-block">Other supporting documents ie photos ,etc .</p>
      </div>
       <button type="submit" class="btn btn-default">Submit</button>
    </form>
2
Jimmy Obonyo Abor