douxianwu2221 2018-10-19 20:04
浏览 32
已采纳

使用CakePHP在另一个视图上显示搜索结果

I'm working with CakePHP now and I need to do a search with one view and then send the data to another action on the same controller, that will trigger another view with the results of that search. The thing is, on the search screen, there's also a table showing some data of the same model, and I believe that's one big problem. So right now, it's here what I got:

public function busca() {
    $emergency = TableRegistry::get('EmergencySheets');
    $manufacturers = TableRegistry::get('Manufacturers');
    $data = $this->request->is('get') ? $this->request->query : $this->request->getData();
    $query = $emergency->find()
    ->select(['id', 'data_atualizacao_fabricante', 'tarja', 'manufacturer_id', 'nome_comercial'])
    ->where('EmergencySheets.data_atualizacao_fabricante')
    ->order(['data_atualizacao_fabricante'=>'DESC'])
    ->limit(7);
    $manufacturer_query = $manufacturers->find()
    ->select(['id','nome'])
    ->where($query->manufacturer_id = 'id');
    $manufacturer = $manufacturer_query->toArray();
    $sheets = $query->toArray();
    $this->set('manufacturers', $manufacturer);
    $this->set('sheets', $sheets);
    if($data){
        return $this->redirect(['action' => 'ficha' , $data]);
    }else{
        return $this->redirect(['action' => 'busca404']);
    }
}

How can I handle this? Thank you all!

Edit: Forgot to mention, but the $data variable always come empty on the form, even when I type something on the form input. Here's the view code, too!

<section class="search-section">
<div class="container px-0">
    <div class="search-wrapper">
        <div class="search-title">
            <h2><span>Quais produtos</span>você vai transportar?</h2>
            <p><span>Pesquise pelos produtos no campo de busca</span>
                ou clique nas letras ao lado.
            </p>
        </div>
        <div class="search-bar">
            <?=$this->Form->create()?>
                <div class="ml-5 bar">                        
                        <input type="text" placeholder="Procure várias fichas de uma só vez" class="formcontrol tip"
                            data-toggle="tooltip" data-placement="top">
                        <span class="removeClick"><i class="fas fa-times-circle fa-2x"></i></span>
                        <button type="submit" class="btn"><i class="fa fa-search fa-2x"></i></button>                        
                </div>
            <?=$this->Form->end()?>
            <div class="ml-5 alfabeto text-center">
                <button href="#A">A</button> <button href="#B">B</button> <button href="#C">C</button> <button href="#D">D</button>
                <button href="#E">E</button> <button href="#F">F</button> <button href="#G">G</button> <button href="#H">H</button>
                <button href="#I">I</button> <button href="#J">J</button> <button href="#K">K</button> <button href="#L">L</button>
                <button href="#M">M</button> <button href="#N">N</button> <button href="#O">O</button> <button href="#P">P</button>
                <button href="#Q">Q</button> <button href="#R">R</button> <button href="#S">S</button> <button href="#T">T</button>
                <button href="#U">U</button> <button href="#V">V</button> <button href="#W">W</button> <button href="#X">X</button>
                <button href="#Y">Y</button> <button href="#Z">Z</button><button href="#0-9">0-9</button>
            </div>
        </div>
    </div>
</div>

  • 写回答

2条回答 默认 最新

  • duanbei3747 2018-10-23 01:09
    关注

    I would use the same function and template for the search form and results. Eliminate the whole if section with redirects, only do the search if there is data to search on, and change your view to check whether there are results to display. Something like this:

    $data = $this->request->is('get') ? $this->request->getQueryParams() : $this->request->getData();
    if (!empty($data)) {
        // Do your searches using $data here, set the results for the view
        $this->set('results', $results);
    }
    

    Then in your template, you have it like you've shown, but add a section with

    if (isset($results)):
        // Display your search results here
    endif;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?