Jay
Jay

Reputation: 402

cant fetch row data from database after search in search form using codeigniter

hey guys i am new in codeigniter and i am working on an application in which i have database and many records in database..i make a search form in which i make a text field in which user can fill any field value and search records.but here is a small problem..when i fill a name in textbox and click on search button no record display on view . . pls help me . . thanks ..

my controller is:

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');
    }
}

view is:

<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>

my model is:

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;
        }
}

Upvotes: 0

Views: 1312

Answers (2)

joHN
joHN

Reputation: 1795

If $data['searchmember'] contains value in controller, you can access the varibale in view as $searchmember

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

Your loop should have been like this

<?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; ?>

It is $searchmember as $row instead of $rows as $row in foreach

Upvotes: 1

Related Questions