Advertisement
kdelemme

Génération de données BDD

May 9th, 2012
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1. <?php
  2.  
  3. class Generator {
  4.     public $prenoms = array('kevin', 'mathieu', 'leslie', 'florent', 'fanny', 'antoine', 'axel', 'guillaume');
  5.     public $noms = array('delemme', 'brisebarre', 'martinez', 'moreau', 'lagarde', 'chekroun', 'legua', 'cozic');
  6.  
  7.     public $mots = array('pellentesque', 'ligula', 'tortor', 'pulvinar', 'vel', 'varius', 'at', 'adipiscing', 'in', 'sapien', 'In', 'hac', 'habitasse', 'proin', 'at', 'mi', 'in', 'lectus', 'cursus', 'aliquam', 'eu', 'et', 'metus', 'vestibulum', 'ante', 'ipsum', 'primis', 'in', 'faucibus', 'orci', 'luctus');
  8.  
  9.     public $mails = array('@gmail.com', '@yahoo.fr', '@facebook.com', '@isen.fr');
  10.  
  11.  
  12.     public function Generator() { }
  13.  
  14.  
  15.     /**
  16.     * $maxsize = taille maximum de la chaine générée. Prendre -20 que la valeur maximum possible reelle.
  17.     * $nbelt = nombre de mots dans la chaine.
  18.     * la génération s'arrete quand $nbelt est attend ou quand $maxsize est attend.
  19.     */
  20.     public function gen_str($nbelt, $maxsize)
  21.     {
  22.         $str = '';
  23.         for ($i = 0; $i<$nbelt || strlen($str) < $maxsize; $i++)
  24.             $str .= $this->mots[rand(0,count($this->mots)-1)]. ' ';
  25.  
  26.         return '\''.$str.'\'';
  27.     }
  28.  
  29.     public function gen_prenom()
  30.     {
  31.         return '\''.$this->prenoms[rand(0, count($this->prenoms)-1)].'\'';
  32.     }
  33.  
  34.     public function gen_nom()
  35.     {
  36.         return '\''.$this->noms[rand(0, count($this->noms)-1)].'\'';
  37.     }
  38.  
  39.     public function gen_villeid() {
  40.         return '\''.rand(1, 30000).'\'';
  41.     }
  42.  
  43.     public function gen_date_futur() {
  44.         return '\''.date('Y-m-d', time() + rand(1814400, 5443200)).'\'';
  45.     }
  46.  
  47.     public function gen_date_passee() {
  48.         return '\''.date('Y-m-d', time() - rand(200*1814400, 300*5443200)).'\'';
  49.     }
  50.  
  51.     public function gen_mail() {
  52.         return '\''.$this->prenoms[rand(0,count($this->prenoms)-1)]. '.' .$this->noms[rand(0,count($this->noms)-1)] . $this->mails[rand(0,count($this->mails)-1)].'\'';
  53.     }
  54.  
  55.     public function gen_phone() {
  56.         $number = '06';
  57.         for ($i=0; $i<8; $i++)
  58.             $number .= rand(0,9);
  59.  
  60.         return '\''.$number.'\'';
  61.     }
  62.  
  63.     public function gen_url() {
  64.         return '\'www.' . $this->mots[rand(0,count($this->mots)-1)] . '.com\'';
  65.     }
  66.  
  67.     /**
  68.     *
  69.     * Generation des INSERT
  70.     * Penser a générer moins de prestataires que d'utilisateurs par exemple ^^
  71.     */
  72.  
  73.     /**
  74.     * TODO:
  75.     * Créer une fonction par table pour générer N * INSERT INTO table(champs1, .., champs12) VALUES($gen->gen_phrase(nb, size champs1), .., $gen->gen_phrase(nb, size champs12));
  76.     */
  77.     public function gen_utilisateurs($nb) {
  78.         for ($i = 1; $i <= $nb; $i++)
  79.         {
  80.             echo 'INSERT INTO utilisateurs(id, ut_nom, ut_prenom, ut_adresse, ut_telephone_fixe, ut_telephone_mobile, ut_adresse_mail, ut_site_internet, ut_blog, ut_date_naissance, ut_sexe, ut_est_actif, ut_secteur_activite, ut_cat_sociopro, ut_situation_famille, ut_centre_interet, ut_est_professionnel, ut_compte_actif) VALUE';
  81.             echo '('.$i.', '.$this->gen_nom().', '.$this->gen_prenom().', '.$this->gen_str(10, 80).', '.$this->gen_phone().', '.$this->gen_phone().', '.$this->gen_mail().',';
  82.             echo $this->gen_url().', '.$this->gen_url().', '.$this->gen_date_passee().', 1, \'true\', '.$this->gen_str(4, 20).', '.$this->gen_str(4, 20).', '.$this->gen_str(4, 20).',';
  83.             echo $this->gen_str(4, 20).', \'true\', \'true\');';
  84.             echo '<br />';
  85.         }
  86.     }
  87.  
  88. }
  89.  
  90.  
  91. $gen = new Generator();
  92.  
  93. $gen->gen_utilisateurs(20);
  94.  
  95.  
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement