Visualizzazione dei risultati da 1 a 10 su 17

Hybrid View

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2013
    Messaggi
    56
    una cosa del genere può andare bene?


    Codice PHP:
    class DB{
        private static 
    $instance;
        private 
    $dbh;

       private function 
    __construct()
        {
            
    $this->dbh = new PDO('mysql:host=localhost;dbname=ps''root''123456');
        }

       public function 
    __destruct()
        {
            
    $this->dbh NULL;
        }

       public static function 
    getInstance()
        {
            if(
    self::$instance == NULL)
                
    self::$instance = new DB();
            return 
    self::$instance;
        }
       public function 
    insert($obj$table$types)
        {
            foreach (
    $types as $k => $v)
                
    $columns[$k] = ($obj->__get($k)) ?: NULL;
            
    $columns_str implode(', 'array_keys($columns));
            
    $placeholders_str ':'.implode(', :'array_keys($columns));
            
    $stmt $this->dbh->prepare("INSERT INTO $table ($columns_str) VALUES ($placeholders_str)");
            
    $stmt->execute($columns);
            
    $obj->__set('id'$this->dbh->lastInsertId());
        }

    Ultima modifica di iJoker; 29-01-2014 a 02:07

  2. #2
    Quote Originariamente inviata da iJoker Visualizza il messaggio
    una cosa del genere può andare bene?
    Questo è un mapper...

    Un controller è molto piu semplice:

    Codice PHP:
    class filmController {

      protected 
    $mapper;
      protected 
    $view;

      public function 
    __constructPDO $pdoview $view ) {
        
    $this->mapper = new Mapper$pdo );
        
    $this->view $view;
      }

      public function 
    index() {
        
    $films $this->mapper->fetchAll();

        
    $content = new view'index-content' );
        
    $content->films $films;

        
    $this->view->title 'Index';
        
    $this->view->addContent'content'$content );
      }

      public function 
    add() {
        
    $content = new view'film-add-form' );

        
    // un metodo che usa il controller da controller
        
    $fv = new formValidator();
        
    $fv->validateEmpty'name''You must insert a value for the field [name]' );
        
    $fv->validateEmpty'description''You must insert a value for the field [description]' );

         if ( 
    $fv->checkErrors() ) {
            
    $message = new view'message'$fv->displayErrors(), array( 'class'=>'error-message' ) );
            
    $content->addView'message'$message );
            } else {
            
    $this->mapper->insert();

            
    $message = new view'message'$this->mapper->getMsg(), array( 'class'=>'service-message' ) );
            
    $content->addView'message'$message );
        }

        
    $this->view->title 'Add';
        
    $this->view->addContent'content'$content );
      }

    un ipotetico controller per i film del tuo database.. ma secondo me devi studiare un po' se hai problemi chiedi pure
    Questa volta, più che un voto.. è favoreggiamento.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.