多步搜索在codeigniter中不起作用

时间:2016-06-23 06:02:25

标签: php mysql codeigniter

我使用codigniter创建了一个多级搜索我使用了像活动记录这样做但我不知道为什么它不起作用让我想如果我搜索一些东西并回显关键字并将其与数据库匹配是他们匹配,但我不知道为什么sql返回0记录从数据库中找到

Php代码

$position = $this->session->set_userdata('position', $this->input->post('position'));
                $adress_all = explode(",", $this->session->userdata('address'));
                $keyword = array(
                    'address'   => $adress_all[0],
                    'game'      => $this->session->userdata('skills'),
                    'gender'    => $this->session->userdata('coach'),
                    'Positions' => $this->session->userdata('position')
                );

                print_r($keyword);
                $sql = $this->db->like($keyword);
                $sql = $this->db->get_where('coach', array('type' => 'private'));

Html表格

<!--Step 1 -->
<form action="<?php echo base_url() . "home/main?st=step1" ; ?>" method="POST">
                <ul>
                    <li><input type="text" placeholder="Categoría" name="skills" id="skills" onkeyup="autocomp();" />
                        <ul id="resultkeyword"></ul>
                    </li>   
                    <li><input id="geocomplete" type="text" name="address" placeholder="Código Postal" size="90" /></li>
                    <li><button type="submit" class="cutter-btn" name="process">Buscar<span><i class="fa fa-angle-right" aria-hidden="true"></i></span></button></li>
                </ul>
            </form>

         <!--Step 2-->
                <form action="<?php echo base_url() . "home/main?st=step2" ; ?>" method="POST" class="step-form">
                    <h3>Preferred gender of your coach:</h3>
                    <div class="form-group">
                    <div>
                      <input id="radio1" type="radio" name="coach" value="" checked="checked"><label for="radio1">No Preference</label>
                    </div>
                    <div>
                      <input id="radio2" type="radio" name="coach" value="Male"><label for="radio2">Only Show Male</label>
                    </div>
                    <div>
                      <input id="radio3" type="radio" name="coach" value="Female"><label for="radio3">Only Show Female</label>
                    </div>
                    </div>

                    <a href="<?php echo base_url() . "home/main" ; ?>" class="nxt-step"><i class="fa fa-angle-double-left" aria-hidden="true"></i> Previous</a>
                    <button type="submit" class="nxt-step">Next <i class="fa fa-angle-double-right" aria-hidden="true"></i></button>

                </form>

<?php 
    $session_data = array(
        'country' => $this->input->post('country'),
        'coach'   => $this->input->post('coach')
    );
    $this->session->set_userdata($session_data);
?>
    <!-- STEP 3 -->
                <form action="<?php echo base_url() . "home/listings" ; ?>" method="POST" class="step-form">
                    <h3>What position?</h3>
                    <div class="form-group">
                    <div>
                      <input id="posotion1" type="radio" name="position" value="Not Sure" checked="checked"><label for="posotion1">Not Sure</label>
                    </div>
                    <div>
                      <input id="posotion2" type="radio" name="position" value="Guard"><label for="posotion2">Guard</label>
                    </div>
                    <div>
                      <input id="posotion3" type="radio" name="position" value="Forward"><label for="posotion3">Forward</label>
                    </div>
                    <div>
                      <input id="posotion4" type="radio" name="position" value="Center"><label for="posotion4">Center</label>
                    </div>
                    </div>

                    <a href="<?php echo base_url() . "home/main?st=step2" ; ?>" class="nxt-step"><i class="fa fa-angle-double-left" aria-hidden="true"></i> Previous</a>
                    <button type="submit" class="nxt-step">Next <i class="fa fa-angle-double-right" aria-hidden="true"></i></button>

                </form>
    <!-- END STEP 3 -->    

这是print_r的结果 数组([地址] =&gt; 2707百老汇[游戏] =&gt;篮球[性别] =&gt;男[职位] =&gt;警卫队)

数据库值 地址:百老汇2707号 游戏:篮球 性别:男 职位:后卫,前锋,后卫

如果我评论除了其中任何一个之外的所有3个然后我得到了结果但是如果所有4个数组都被取消注释那么我没有得到任何结果意味着如果我只传递1个值然后sql返回行但是如果还有更多那么1然后sql返回0行 我的意思是说关键值是匹配的,然后为什么它给我0结果返回?

1 个答案:

答案 0 :(得分:0)

您可以使用or_like方法搜索多列

$this->db->like('address', $adress_all[0]);
$this->db->or_like('game', $this->session->userdata('skills'));
$this->db->or_like('gender', $this->session->userdata('coach'));
$this->db->or_like('Positions', $this->session->userdata('position'));
$sql = $this->db->get_where('coach', array('type' => 'private'));