Part of the EllisLab Network
   
1 of 2
1
display from database
Posted: 09 February 2010 06:38 AM   [ Ignore ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

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

Profile
 
 
Posted: 09 February 2010 02:35 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  346
Joined  09-28-2002

Controller (controllers/students.php):

<?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):

<?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):

<h1><?php echo $page_title?></h1>

<
ul>
<?php foreach($students as $student): ?>
    
<li><?php echo $student->first_name?> <?php echo $student->last_name?></li>
<?php endforeach; ?>
</ul
Profile
 
 
Posted: 10 February 2010 12:14 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

THANKS A LOT BROTHER

wish u the best

Profile
 
 
Posted: 10 February 2010 12:44 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

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

Profile
 
 
Posted: 10 February 2010 01:23 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  06-05-2008
$data = array(
                
'page_title' => 'Students',
                
'students' => $students->result(),
                
'role_id' => $this->session->user_data('role_id');
            );
            
$this->load->view('students/list',$data); 
 Signature 

cool smile Saidai Jagan

Profile
 
 
Posted: 10 February 2010 02:11 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

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

Profile
 
 
Posted: 10 February 2010 02:19 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  06-05-2008
echo $role_id

in u r view file

 Signature 

cool smile Saidai Jagan

Profile
 
 
Posted: 10 February 2010 02:29 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

iam not getting the value

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

Profile
 
 
Posted: 10 February 2010 02:59 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  06-05-2008
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
 Signature 

cool smile Saidai Jagan

Profile
 
 
Posted: 10 February 2010 04:17 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

still not getting

wat is this list

students/list

Profile
 
 
Posted: 10 February 2010 04:29 AM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  06-05-2008

u assigned the role id to session

echo $this->session->userdata('role_id'); 
 Signature 

cool smile Saidai Jagan

Profile
 
 
Posted: 10 February 2010 04:37 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

yes i did

how did u store the value of $role in session


and wat is session/list

(sorry 4 asking such stupid questions ,but please help)

Profile
 
 
Posted: 10 February 2010 05:27 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  06-05-2008
ceig - 10 February 2010 07:11 AM

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

By this code alter it to

$role_ids = array('role_id' => $role);
$this->session->set_userdata($role_ids);
$data = array(
                
'page_title' => 'Students',
                
'students' => $students->result(),
                
'role_id' => $this->session->userdata('role_id');
            );
echo 
$data['role_id']

Note : students/list is already there. I just copied the code.

 Signature 

cool smile Saidai Jagan

Profile
 
 
Posted: 10 February 2010 06:34 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

thank you brother grin thank u

its working


one more doubt

how to select data from a table from db using WHERE condition in codeigniter .

example list of students whose marks=90

Profile
 
 
Posted: 10 February 2010 06:49 AM   [ Ignore ]   [ # 14 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  178
Joined  06-05-2008

check here

 Signature 

cool smile Saidai Jagan

Profile
 
 
Posted: 10 February 2010 07:05 AM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  16
Joined  02-01-2010

thanks

Profile
 
 
   
1 of 2
1