使用codeigniter在搜索表单中搜索后,无法从数据库中获取行数据

时间:2013-03-12 09:36:48

标签: php mysql codeigniter

嘿伙计们我是codeigniter的新手,我正在开发一个应用程序,其中我有数据库和数据库中的许多记录...我做了一个搜索表单,在其中我创建一个文本字段,用户可以填写任何字段值和搜索记录。但这是一个小问题..当我在文本框中填写一个名称并单击搜索按钮时,视图上没有记录显示。 。请帮助我 。 。谢谢..

我的控制器是:

function search()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('inputsearch', 'Search Your Text Here','required');
    if ($this->form_validation->run() == TRUE)
    {
        $this->load->helper('form');
        $this->load->helper('html');
        $search_this=$this->input->post('inputsearch');
        $this->load->model('mod_user');
        $searchmember=$this->mod_user->search_member($search_this);
        $data['searchmember'] = $searchmember;
        var_dump($searchmember);
        $this->load->view('view_home',$data);
        $this->load->view('view_homemember',$data);
    }
    else
    {
        redirect('ctl_home');
    }
}

视图是:

<form class="userinfo" action="" method="post">
        <table border="1" cellpadding="2" cellspacing="0">
            <tr>
                <td width="5%;">&nbsp;</td>
                <td width="5%;">&nbsp;</td>
                <td width="15%;">Name</td>
                <td width="15%;">Moderator Name</td>
                <td width="20%;">KCC Branch</td>
                <td width="15%;">Father/Husband Name</td>
                <td width="15%;">Address</td>
                <td width="10%;">Date</td>
            </tr>
             <?php foreach ($rows as $row):?>
            <tr>
                <td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
                <td><a href="<?php echo site_url("ctl_dbcont/deleteinput/" . $row->member_id);?>" onclick="return confirm('Delete content?');">Delete</a></td>
                <td><?php echo $row->member_name;  ?></td>
                <td><?php echo $row->moderator_name;  ?></td>
                <td><?php echo $row->branch_name;  ?></td>
                <td><?php echo $row->father_name;  ?></td>
                <td><?php echo $row->address;  ?></td>
                <td><?php echo $row->date;  ?></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </form>

我的模特是:

function search_member($search_this)
{
    $query=$this->db->query("SELECT tbl_members.* , tbl_moderators.member_id AS moderator_id, tbl_moderators.member_name AS moderator_name, tbl_moderators.member_no AS moderator_no, tbl_branches.branch_name FROM tbl_members, tbl_members AS tbl_moderators, tbl_branches  WHERE tbl_members.moderator_id = tbl_moderators.member_id AND tbl_branches.branch_id = tbl_members.kcc_branch 
                                    AND  CONCAT(tbl_members.member_name, ' ', tbl_moderators.member_name, ' ',tbl_members.moderator_id, ' ', tbl_moderators.member_id, ' ',tbl_branches.branch_name, ' ',tbl_members.father_name, ' ',tbl_members.address, ' ',tbl_moderators.address, ' ',tbl_members.date, ' ',tbl_moderators.date)  like '%".$search_this."%'");
    if ($query->num_rows >= 0)
        {
            foreach($query->result_array() as $row)
            {
                $data[]=$row;
            }
            return $data;
        }
}

2 个答案:

答案 0 :(得分:1)

你的循环应该是这样的

<?php foreach ($searchmember as $row):?>
    <tr>
        <td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
        <td><a href="<?php echo site_url("ctl_dbcont/deleteinput/" . $row->member_id);?>" onclick="return confirm('Delete content?');">Delete</a></td>
        <td><?php echo $row->member_name;  ?></td>
        <td><?php echo $row->moderator_name;  ?></td>
        <td><?php echo $row->branch_name;  ?></td>
        <td><?php echo $row->father_name;  ?></td>
        <td><?php echo $row->address;  ?></td>
        <td><?php echo $row->date;  ?></td>
    </tr>
<?php endforeach; ?>

$ search是$ row而不是$ rows作为foreach中的$ row

答案 1 :(得分:0)

如果$ data ['searchmember']包含控制器中的值, 您可以在$ searchmember

中访问视图中的varibale