Ejemplo n.º 1
0
 public function search($smarty, $tpl, $params, $DB)
 {
     $this->Test_session();
     $res = new Home($DB);
     if (isset($_POST['search'])) {
         if (empty($_POST['search'])) {
             $smarty->assign('errRecherche', "Votre recherche est vide !");
             $this->Test_admin($smarty, $tpl, $params, $DB);
             $this->init($smarty, $tpl, $params, $DB);
             $smarty->display($tpl);
         } else {
             $recherche = $_POST['search'];
             $tableau = explode(" ", $recherche);
             $sql1 = 'SELECT * FROM posts';
             $sql2 = 'SELECT * FROM posts';
             $i = 0;
             foreach ($tableau as $mot) {
                 if (strlen($mot) > 1) {
                     if ($i == 0) {
                         $sql1 .= ' WHERE ';
                         $sql2 .= ' WHERE ';
                     } else {
                         $sql1 .= ' OR ';
                         $sql2 .= ' OR ';
                     }
                     $sql1 .= ' textePosts LIKE "%$mot%"';
                     $sql2 .= ' titrePosts LIKE "%$mot%"';
                     $i++;
                 }
             }
             $sql1 .= ' ORDER BY idPosts DESC';
             $sql2 .= ' ORDER BY idPosts DESC';
             $r1 = $res->search($sql1);
             $r2 = $res->search($sql2);
             if ($r1) {
                 foreach ($r1 as $key => $value) {
                     if (!isset($tabP[$r1[$key]['idPosts']])) {
                         $tabP[$r1[$key]['idPosts']] = array();
                     }
                     array_push($tabP[$r1[$key]['idPosts']], html_entity_decode($r1[$key]['titrePosts']), $r1[$key]['datePosts'], $r1[$key]['membres_pseudoMbr'], $r1[$key]['catPosts_nomCatPosts']);
                 }
             }
             if ($r2) {
                 foreach ($r2 as $key => $value) {
                     if (!isset($tabP[$r2[$key]['idPosts']])) {
                         $tabP[$r2[$key]['idPosts']] = array();
                     }
                     array_push($tabP[$r2[$key]['idPosts']], html_entity_decode($r2[$key]['titrePosts']), $r2[$key]['datePosts'], $r2[$key]['membres_pseudoMbr'], $r2[$key]['catPosts_nomCatPosts']);
                 }
             }
             if (!$r1 && !$r2) {
                 $smarty->assign('errRecherche2', "Aucun résultat trouvé.");
                 $this->Test_admin($smarty, $tpl, $params, $DB);
                 $this->init($smarty, $tpl, $params, $DB);
                 $smarty->display($tpl);
             } else {
                 $smarty->assign('postSearch', $tabP);
                 $this->Test_admin($smarty, $tpl, $params, $DB);
                 $this->init($smarty, $tpl, $params, $DB);
                 $smarty->display('home.tpl');
             }
         }
     }
 }