Welcome Guest, Not a member yet? Register   Sign In
search is not working
#1

(This post was last modified: 04-01-2020, 04:40 AM by wdeda.)

Hi!
I'm stuck on something that is driving me really crazy!
I have a local website, music and film catalogs, and it is possible to search for various items, such as names, songs, records, movies, etc.
I'm updating from ci3 to ci4, but when I do a search I get 404 error, so I decided to do an experiment. In the CI3 search form I pointed to CI4 and ... voilà! , it worked, indicating that both model, controller and view, in CI4, are correct. Obviously, the reverse, CI4 to CI3, works too.

Model:
PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
SearchModel extends Model
{
    public function getNames() {
        $request = \Config\Services::request();
        $db = \Config\Database::connect();
        $terms $request->getVar('search');
        $query $db->table('names')
        ->select('*')
        ->like('name'$terms)
 
    ->orLike('alias'$terms)
 
    ->orLike('alias2'$terms)
 
    ->orderBy('vip''name''alias''asc')
        ->get();
        
        
return $query->getResult();
    }

    public function numNames()
    {
        $request = \Config\Services::request();
        $terms $request->getVar('search');        
        $db 
= \Config\Database::connect();
        $builder $db->table('names');
        $builder->like('name'$terms);
        $builder->orLike('alias'$terms);
        $builder->orLike('alias2'$terms);
        $data $builder->countAllResults();
        
        
if($data != 0){
            return $data;
        }else{
            $data null;
            return $data;
        }
      
    
}

    public function getAlbuns()
    {
        $request = \Config\Services::request();
        $db = \Config\Database::connect();
        $terms $request->getVar('search');
        $builder $db->table('albuns');
        $builder->select('albuns.*, names.name, names.id AS artistid');
        $builder->join('names''albuns.player_id = names.id');
        $builder->like('albuns.title'$terms);
        $builder->orderBy('albuns.ano''asc');
        $query $builder->get();
        
        
return $query->getResult();
    }

    public function numAlbuns()
    {
        $request = \Config\Services::request();
        $terms $request->getVar('search');        
        $db 
= \Config\Database::connect();
        $builder $db->table('albuns');
        $builder->like('title'$terms);
        $data $builder->countAllResults();
        
        
if($data != 0){
            return $data;
        }else{
            $data null;
            return $data;
        }
      
    
}

    public function getSongs()
    {
        $request = \Config\Services::request();
        $db = \Config\Database::connect();        
        $terms 
$request->getVar('search');
        $builder $db->table('names');
        $builder->select('names.id, names.name, names.cover, songs.song, songs.player1, songs.link1, songs.player2, songs.link2, songs.player3, songs.link3, songs.player4, songs.link4, songs.feat1, songs.feat2, songs.feat3, songs.feat4, songs.id AS songid, albuns.id AS albunsid, albuns.title, albuns.capa AS albumcover, albuns.ano AS recordyear FROM names'); 
        $builder->join('albuns''names.id = albuns.player_id');
        $builder->join('songs''albuns.id = songs.album_id');
        $builder->like('song'$terms);
        $builder->orLike('feat1'$terms);
        $builder->orLike('feat2'$terms);
        $builder->orLike('feat3'$terms);
        $builder->orLike('feat4'$terms);
        $builder->orderBy('albuns.ano''albuns.id''asc');
        $query $builder->get();

        return $query->getResult();

    }

    public function numSongs()
    {
        $request = \Config\Services::request();
        $terms $request->getVar('search');
        $db = \Config\Database::connect();
        $builder $db->table('songs');
        $builder->like('song'$terms);
        $builder->orLike('feat1'$terms);
        $builder->orLike('feat2'$terms);
        $builder->orLike('feat3'$terms);
        $builder->orLike('feat4'$terms);
        $data $builder->countAllResults();
        
        
return $data;
    }
    
    
public function getMovies()
    {
        $request = \Config\Services::request();
        $terms $request->getVar('search');
        $db = \Config\Database::connect();
        $query $db->table('movies')
        ->select('*')
        ->like('title'$terms)
        ->orLike('origtitle'$terms)
        ->orderBy('ano''title''desc')
        ->get();

        return $query->getResult();
    }

    public function numMovies()
    {
        $request = \Config\Services::request();
        $terms $request->getVar('search');
        $db = \Config\Database::connect();
        $builder $db->table('movies');
        $builder->like('title'$terms);
        $builder->orLike('origtitle'$terms);
        $data $builder->countAllResults();

return 
$data;
    }



Controller:
PHP Code:
<?php namespace App\Controllers;
    
    
use App\Models\SearchModel;

    class Search extends BaseController {

 public function 
allmedia()
 {
 
 
$request = \Config\Services::request();
 
$terms $request->getVar('search');
 
$nodata "/ci3/errors/nodata";
 if (empty(
$terms)) {
 
header("location:$nodata");
 exit;
 } 

 
$model = new SearchModel();

 
$db = \Config\Database::connect();

 
$result1 $model->getNames();
 
$result2 $model->getAlbuns();
 
$result3 $model->getSongs();
 
$result4 $model->getMovies();
 
$resultA $model->numNames();
 
$resultB $model->numAlbuns();
 
$resultC $model->numSongs();
 
$resultD $model->numMovies();
 
$total $resultA $resultB $resultC $resultD;
 
$movA $db->table('names');
 
$movB $db->table('albuns');
 
$movC $db->table('songs');
 
$movD $db->table('movies');
 
$tnames $movA->countAll('names');
 
$talbuns $movB->countAll('albuns');
 
$tsongs $movC->countAll('songs');
 
$tvideos $movD->countAll('movies');
 
$tgeral $tnames $talbuns $tsongs $tvideos

 
$header = array ( 
 
'icon' => 'search',
 
'css' => 'search',
 
'title' => 'Resultado(s) da Pesquisa - wdeda'
 
'placeholder' => 'Pesquisar'
 
);

 echo 
view('templates/header'$header);
 echo 
view('content/search/search', array(
 
'result1' => $result1,
 
'result2' => $result2,
 
'result3' => $result3,
 
'result4' => $result4,
 
'resultA' => $resultA,
 
'resultB' => $resultB,
 
'resultC' => $resultC,
 
'resultD' => $resultD,
 
'tnames' => $tnames,
 
'talbuns' => $talbuns,
 
'tsongs' => $tsongs,
 
'tvideos' => $tvideos,
 
'total' => $total,
 
'tgeral' => $tgeral,
 
'terms' => $terms
 
));
 echo 
view('templates/footer');

      
 




View:
PHP Code:
    <!--
        by wdeda 20200331
    
-->
    
 
<div class="search-content-container">
 <
div class="search-sidebar">
 <?
php if ($total != null) { ?>
 <div class="filters">
 <h2>Filtros da Pesquisa</h2>
 <hr />
 <section class="filter-types">
 <h3>Resultados</h3>
 <ul>
 <li class="active">
 <span class="span-all">Todos<span>&nbsp;</span><span class="count">(<span class="span-result"><?php echo $total?></span>)</span></span>
 </li>
 <?php
 
if ($resultA != null) {
 echo 
"<li class='li-all'>";
 echo 
"<span class='span-all'>Pessoas/Bandas<span>&nbsp;</span><span class='count'>(<span class='span-result'>$resultA</span>)</span></a></li>";
 } 
 if (
$resultB != null) {
 echo 
"<li class='li-all'>";
 echo 
"<span class='span-all'>Álbums<span>&nbsp;</span><span class='count'>(<span class='span-result'>$resultB</span>)</span></a></li>";
 }
 if (
$resultC != null) {
 echo 
"<li class='li-all'>";
 echo 
"<span class='span-all'>Músicas<span>&nbsp;</span><span class='count'>(<span class='span-result'>$resultC</span>)</span></a></li>";
 }
 if (
$resultD != null) {
 echo 
"<li class='li-all'>";
 echo 
"<span class='span-all'>Filmes/Vídeos<span>&nbsp;</span><span class='count'>(<span class='span-result'>$resultD</span>)</span></a></li>";
 }
 
?>
 </ul>
 </section>
 <hr />
 </div>
 <?php ?> 
 <div class="filters">
 <h2>Filtros Gerais</h2>
 <hr />
 <section class="filter-types">
 <h3>Totais</h3>
 <ul>
 <li class="active">
 <span class="span-all">Todos<span>&nbsp;</span><span class="count">(<span class="span-result"><?php echo $tgeral?></span>)</span></span>
 </li>
 <li class="li-all">
 <span class="span-all">Pessoas/Bandas<span>&nbsp;</span><span class="count">(<span class="span-result"><?php echo $tnames?></span>)</span></a></li>
 <li class="li-all">
 <span class="span-all"> Álbums<span>&nbsp;</span><span class="count">(<span class="span-result"><?php echo $talbuns?></span>)</span></a></li>
 <li class="li-all">
 <span class="span-all">Músicas<span>&nbsp;</span><span class="count">(<span class="span-result"><?php echo $tsongs?></span>)</span></a></li>
 <li class="li-all">
 <span class="span-all">Filmes/Vídeos<span>&nbsp;</span><span class="count">(<span class="span-result"><?php echo $tvideos?></span>)</span></a></li>
 </ul>
 </section>
 <hr />
 </div>
 </div>
 <div class="search-content">
 <?php
 
if ($total != 0) {
 echo 
"<h1>encontrado(s) $total resultado(s) para a pesquisa <span class='span-result'>$terms</span></h1>";
 }
 else
 {
 echo 
"<h1>nenhum resultado encontrado para a pesquisa <span class='span-result'>$terms</span></h1>";
 }
 
?>
 <ul class="search-results">
 <?php
 
foreach ($result1 as $result){
 
$link $result->id;
 
$name $result->name;
 
$act1 $result->act1;
 
$act2 $result->act2;
 
$act3 $result->act3;
 
$act4 $result->act4;
 
$act5 $result->act5;
 
$genre1 $result->genre1;
 
$genre2 $result->genre2;
 
$genre3 $result->genre3;
 
$active $result->active;
 
$cover $result->cover;
 
?>
 <li class="search-artist">
 <?php if ($cover != 0) {
 echo 
'<div class="photo">';
 echo 
"<a href='/ci4/allmedia/names/$link'>";
 echo 
'<img height="100" src="/img/names/'.$cover.'.jpg" title="'.$name.'" />';
 echo 
'</a>';
 echo 
'</div>';
 }else{
 echo 
'<div class="photo">';
 echo 
"<a href='/ci4/allmedia/names/$link'>";
 echo 
"<img height='100' src='/img/names/0000000.jpg' title='$name' />";
 echo 
'</a>';
 echo 
'</div>';
 }
 
?>
 <div class="info">
 <h4>
 <?php
 
if ($act2 == null){
 echo 
$act1;
 }else{
 echo 
"$act1$act2";
 }
 if (
$act3 != null){
 echo 
",&nbsp;$act3";
 }
 if (
$act4 != null){
 echo 
",&nbsp;$act4";
 }
 if (
$act5 != null){
 echo 
",&nbsp;$act5";
 }
 
?>
 </h4>
 <div class="name">
 <?php
 
echo "<a href='/ci4/allmedia/names/$link'>$name</a>";
 echo 
"</div>";
 echo 
"<div class='genres'>";
 if (
$genre2 == null) {
 echo 
"<span>$genre1</span>";
 }
 else
 {
 echo 
"<span>$genre1$genre2</span>";
 }
 if (
$genre3 != null) {
 echo 
"<span>, $genre3</span>";
 } 
 
?>
 </div>
 <?php if ($active != null){ ?> 
 <div class="decades">Atividade: <?php echo $active?></div>
 </div>
 <?php ?>
 </li>
 
 <?php }
 foreach (
$result2 as $result){
 
$album $result->id;
 
$artist $result->artistid;
 
$year $result->ano;
 
$cover $result->capa;
 
$title $result->title;
 
$name $result->name;
 
$name1 $result->name1;
 
$plink1 $result->plink1;
 
$name2 $result->name2;
 
$plink2 $result->plink2;
 
$name3 $result->name3;
 
$plink3 $result->plink3;
 
$name4 $result->name4;
 
$plink4 $result->plink4;
 
$style1 $result->style1;
 
$style2 $result->style2;
 
$style3 $result->style3;
 
$style4 $result->style4;
 
$rectitle $result->title;
 
?>
 <li class="album">
 <div class="cover" style="float: right;">
 <?php
 
echo "<a class='album-cover-link-crop-image-borders' href='/ci4/music/album/$album' title='$title'>";
 if (
$cover != 0000000) {
 echo 
"<img class='album-lazy' title='$title' src='/img/albuns/$cover.jpg' width='101' /></a>";
 }
 else
 {
 echo 
"<img title='$title' class='album-lazy' height='101' src='/img/albuns/dhnc055x55.png' width='101' /></a>";
 }
 
?>
 </div>
 <div class="info">
 <h4>Álbum</h4>
 <div class="title">
 <?php
 
echo "<a href='/ci4/music/album/$album'>$title</a>";
 echo 
"</div>";
 echo 
'<div class="artist">';
 if (
$name1 == null) {
 echo 
"<a href='/ci4/allmedia/names/$artist'>";
 echo 
$name;
 echo 
"</a>";
 }
 else
 {
 if (
$plink1 == null){
 echo 
"<span style='color: rgb(0, 0, 0);'>$name1";
 }
 else
 {
 echo 
"<a href='/ci4/allmedia/names/$plink1'>$name1</a>";
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 }
 }
 if (
$name2 != null){
 if (
$plink2 == null){
 echo 
"<span style='color: rgb(0, 0, 0);'>$name2";
 }
 else
 {
 echo 
"<a href='/ci4/allmedia/names/$plink2'>$name2</a>";
 }
 }
 if (
$name3 != null){
 if (
$plink3 == null){
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<span style='color: rgb(0, 0, 0);'>$name3";
 }
 else
 {
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<a href='/ci4/allmedia/names/$plink3'>$name3</a>";
 } 
 }
 if (
$name4 != null) { 
 if (
$plink4 == null){
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<span style='color: rgb(0, 0, 0);'>$name4";
 }
 else
 {
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<a href='/ci4/allmedia/names/$plink4'>$name4</a>";
 }
 }
 
?> 
 
 </div>
 <div class="year"><?php echo $year?></div>
 <div class="genres">
 <?php
 
if ($style2 == null) {
 echo 
"<span>$style1</span";
 }
 else
 {
 echo 
"<span>$style1$style2</span>";
 }
 if (
$style3 != null) {
 echo 
"<span>, $style3</span>";
 }
 if (
$style4 != null) {
 echo 
"<span>, $style4</span>";
 }
 
?>
 </div>
 </div>
 </li>
 <?php }
 foreach (
$result3 as $result){
 
$artist $result->id;
 
$reclink $result->albunsid;
 
$name $result->name;
 
$player1 $result->player1;
 
$link1 $result->link1;
 
$player2 $result->player2;
 
$link2 $result->link2;
 
$player3 $result->player3;
 
$link3 $result->link3;
 
$player4 $result->player4;
 
$link4 $result->link4;
 
$title $result->title;
 
$song $result->song;
 
$cover $result->cover;
 
$capa $result->albumcover;
 
$year $result->recordyear;
 
$capa $result->albumcover;
 
 
?>
 <li class="song">
 <div class="cover" style="float: right;">
 <?php
 
echo "<a class='album-cover-link-crop-image-borders' href='/ci4/music/album/$reclink' title='$title'>";
 if (
$capa != null) {
 echo 
'<img class="album-lazy" title="'.$title.'" src="/img/albuns/'.$capa.'.jpg" width="101" /></a>';
 }
 else
 { 
 echo 
'<img class="album-lazy" title="'.$title.'" src="/img/albuns/dhnc055x55.png" width="101" /></a>';
 }
 
?>
 </div>
 <h4>Música</h4>
 <div class="song-title">
 <?php
 
echo "<a href='/ci4/music/album/$reclink'>$song</a>";
 echo 
"</div>";
 echo 
"<div class='performers'>";
 echo 
"por<span>&nbsp;</span>";
 if (
$player1 == null) { 
 if (
$artist == 0){
 echo 
"<span style=1color: rgb(0, 0, 0);1>>$name</span>";
 }
 else
 {
 echo 
"<a href='/ci4/allmedia/names/$artist'>$name</a>";
 } 
 }
 else
 {
 if (
$link1 == 0){
 echo 
"<span style='color: rgb(0, 0, 0);'>$player1</span>";
 echo 
"<span>&nbsp;</span>/<span class='Apple-converted-space'>&nbsp;</span>";
 }
 else
 { 
 echo 
"<a href='/ci4/allmedia/names/$link1'>$player1</a>";
 echo 
"<span>&nbsp;</span>/<span class='Apple-converted-space'>&nbsp;</span>";
 }
 }
 if (
$player2 != null){
 if (
$link2 == 0){
 echo 
"<span style='color: rgb(0, 0, 0);'>$player2</span>";
 }
 else
 {
 echo 
"<a href='/ci4/allmedia/names/$link2'>$player2</a>";
 }
 }
 if (
$player3 != null){
 if (
$link3 == 0){
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<span style='color: rgb(0, 0, 0)'>$player3</span>";
 }
 else
 {
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<a href='/ci4/allmedia/names/$link3'>$player3</a>";
 }
 }
 if (
$player4 != null) {
 if (
$link4 == 0){
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<span style='color: rgb(0, 0, 0)'>$player4</span>";
 }
 else
 { 
 echo 
"<span>&nbsp;</span>/<span>&nbsp;</span>";
 echo 
"<a href='/ci4/allmedia/names/$link4'>$player4</a>";
 }
 }
 
?> 
 </div>
 </li>
 <?php }
 foreach (
$result4 as $result){
 
$link $result->id;
 
$cover $result->capa;
 
$title $result->title;
 
$genre1 $result->genre1;
 
$genre2 $result->genre2;
 
$genre3 $result->genre3;
 
$year $result->ano;
 
$origtitle $result->origtitle
 
?>
 <li class="video">
 <div class="cover">
 <a href="/ci4/movies/movie/<?php echo $link?>">
 <?php
 
if ($cover != null) {
 echo 
'<img title="'.$title.'" height="149" src="/img/titles/'.$cover.'.jpg" /></a>';
 }
 else
 {
 echo 
"</a>";
 }
 
?>
 </div>
 <div class="video-info">
 <h4>Vídeo</h4>
 <div class="video-title">
 <a href="/ci4/movies/movie/<?php echo $link?>"><?php echo $title ?></a>
 </div>
 <div class="movie-year"><?php echo $year?></div>
 <div class="movie-style">
 <?php
 
if ($genre2 == null){
 echo 
$genre1;
 } else {
 echo 
$genre1.',&nbsp;'.$genre2;
 }
 if (
$genre3 != null){
 echo 
',&nbsp;'.$genre3;
 }
 
?>
 </div>
 </div>
 </li>
 <?php ?>
 </ul>
 </div>
 </div> 

Header:
PHP Code:
<!DOCTYPE html>
<
html lang="PT_br">
<
head>
    <
meta charset="UTF-8">
    <
meta name="viewport" content="width=device-width, initial-scale=1.0">
    <
meta name="description" content="Catálogo privado de filmes e músicas">
    <
meta http-equiv="X-UA-Compatible" content="ie=edge">
    <
link rel="shortcut icon" href="/ci4/public/img/<?php echo $icon; ?>.ico" type="image/x-icon" />
    <
link rel="stylesheet" href="/ci4/public/css/global.css">
    <
link href="/ci4/public/css/header.css" rel="stylesheet">
    <
link href="/ci4/public/css/<?php echo $css; ?>.css" rel="stylesheet">
    <
title><?php echo $title?></title>
</head>
<body>
<div class="overflow-container">
    <div class="header-main-nav">
    <div class="max-header">
    <div class="main-nav">
    <div class="logo-container">
                    <a class="logo" href="/ci4/">
                    <h1 class="logo-img">wdeda</h1>
                    </a>
    </div>
    <div class="search-container">
        <form action="/ci4/search/allmedia/" class="site-search" method="post" name="site-search">
            <input class="site-search-button" name="wdsub" type="submit" />
            <input class="site-search-input" name="search" placeholder="<?php echo $placeholder?>" tabindex="1" type="search" />
        </form>
    </div>
        <nav class="site-nav">
            <ul>
                <li class="blog">
                    <a href="/blog/">Blog</a>
                </li>
                <li class="backups">
                    <a href="/ci4//backups">Backups</a>
                </li>
                <li class="tools">
                    <a href="/ci4//tools">Tools</a>
                </li>
                <!--
                <li class="test">
                    <a href="/wd3">wd3</a>
                </li>
                <li class="empty1">
                    <a class="empty1">empty1</a>
                </li>
                <li class="empty2">
                    <a class="empty2" href="">empty2</a>
                </li -->
            </ul>
        </nav>
    <div class="user-social-nav">
        <nav class="social-nav">
            <ul>
                <li class="facebook">
                    <a alt="Facebook" href="https://www.facebook.com/" title="Facebook">Facebook</a>
                </li>
                <li class="twitter">
                    <a alt="Twitter" href="https://twitter.com/" title="Twitter">Twitter</a>
                </li>
                <li class="tumblr">
                    <a alt="Tumblr" href="https://allmusic.tumblr.com/" title="Tumblr">Tumblr</a>
                </li>
                <li class="rss">
                    <a alt="RSS feed" href="https://www.allmusic.com/rss" target="_blank" title="RSS feed">RSS</a>
                </li>
            </ul>
        </nav>
        <nav class="user-notifications">
            <ul>
                <li class="user-notification-icon">
    <div class="flag">
    <div class="notifications-flag-inner">
    </div>
    </div><div class="user-noticiation-inner-container">
    </div>
                </li>
            </ul>
        </nav>
        <nav class="user-nav">
            <ul>
                <li class="user-menu">
                    <span class="display-name">    Walter Déda</span>
                    <span class="gear" title="Account Settings"></span>
                </li>
            </ul>
        </nav>
    </div>
    </div>
    </div>
    </div> 

If anyone can help, I appreciate it.
Reply
#2

You need to add a route for the post method in app/Config/Routes.php
You can look at the example in this tutorial I wrote about a search form in CI4: https://includebeer.com/en/blog/how-to-b...r-4-part-5
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB