Welcome Guest, Not a member yet? Register   Sign In
display from database
#1

[eluser]new kid[/eluser]
NEW TO CODEIGNITER .NEED HELP

hi i was creating a demo site for a school and i need to display(display is using echo table) all the details from a table called students .how to create model and controller and how to get the values in view .


please give some samples

i need to role id also to this view because i want to change the table display according to the
people(students/teachers) logging in
#2

[eluser]kirkaracha[/eluser]
Controller (controllers/students.php):

Code:
<?php
class Students extends Controller {

    public function index() {
        // load the model
        $this->load->model(array('Students_model'));

        // run the database query
        $students = $this->Students_model->get_students();

        if ($students->num_rows() > 0) {
        // show a list of students if there are any in the database
            $data = array(
                'page_title' => 'Students',
                'students' => $students->result(),
            );
            $this->load->view('students/list',$data);
        } else {
        // show a message if no students are listed in the database
            $data = array(
                'page_title' => 'No Students Listed',
                'message_text' => 'Sorry, no students are listed.'
            );
            $this->load->view('shared/display_messages',$data);
        }
    } // index

} // Students
?>

Model (models/students_model.php):

Code:
<?php

class Students_model extends Model{

    function Students_model() {
        parent::Model();
        $this->table = 'students';
    }

    function get_students() {
        $this->db->select('*');
        $this->db->from($this->table);
        return $this->db->get();
    } // get_students

} // Students_model

?>

View (views/students/list.php):

Code:
<h1>&lt;?php echo $page_title; ?&gt;</h1>

<ul>
&lt;?php foreach($students as $student): ?&gt;
    <li>&lt;?php echo $student->first_name; ?&gt; &lt;?php echo $student->last_name; ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>
#3

[eluser]new kid[/eluser]
THANKS A LOT BROTHER

wish u the best
#4

[eluser]new kid[/eluser]
PASSING VARIABLE tO VIEW

i need to pass role id variable also to this view because i want to change the table display according to the
people(students/teachers) logging in

when they log in a session is created and i want to pass this session variable to this same
view
please tell me how
#5

[eluser]saidai jagan[/eluser]
Code:
$data = array(
                'page_title' => 'Students',
                'students' => $students->result(),
                'role_id' => $this->session->user_data('role_id');
            );
            $this->load->view('students/list',$data);
#6

[eluser]new kid[/eluser]
i hav set session in login page by
$this->session->set_userdata('$role');

$role is from database

then i used ur code to send the values
$data = array(
'page_title' => 'Students',
'students' => $students->result(),
'role_id' => $this->session->user_data('role_id');
);
$this->load->view('students/list',$data);


how to get this value in view
i hav to store it in a variable
please help
#7

[eluser]saidai jagan[/eluser]
Code:
echo $role_id;
in u r view file
#8

[eluser]new kid[/eluser]
iam not getting the value

please check the code i hav used $role in setting userdata
#9

[eluser]saidai jagan[/eluser]
Code:
echo $this->session->userdata('role_id');
$data = array(
                'page_title' => 'Students',
                'students' => $students->result(),
                'role_id' => $this->session->userdata('role_id');
            );
            $this->load->view('students/list',$data)
#10

[eluser]new kid[/eluser]
still not getting

wat is this list

students/list




Theme © iAndrew 2016 - Forum software by © MyBB