Hello I am a beginner in codeigniter framework and I have a problem!
There is a table that needs to be populated with all sorts of data, and one of items will be select menu with options, and while I am trying to populate that select menu I'm stuck and now I have a problem.
When I call controller's method my_courses():
function my_courses(){
$username = $this->session->userdata('username'); //1 username from session
$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table
$result =$this->model_professor->professors_classes($username); //3 so we send username to that model
if($result == false){
echo "Professor has no courses ";
}else{ //4 model will return results collected from table
$query = $this->model_professor->professors_classes($username);
$data['records'] = $query;
//
//
//WE SEND DATA TO A TEST PAGE AND THIS IS OK!!! WHEN I DO THAT EVERYTHING WORKS FINE
//
//
$this->load->view('professor/test',$data);
}
}
MODEL professors_classes:
function professors_classes($username){
$this->db->where("username", $username);
$query = $this->db->get("table_teach_a_course");
//if we have results then we will return them
if($query->result()){
return $query->result();
}
//else we return false
else{
return false;
}
}
This is my stripped test page without name and value in select and in option, and here everything works fine. Here I manage to catch results and to show them:
<?php if(isset($records)) { ?>
<select>
<?php foreach ($records as $row){ ?>
<option value=""> <?php echo $row->sifra_predmeta; ?> </option>
<?php } ?>
</select>
<?php } ?>
But problem occures when in my controller I send data to other page(view) that actually is important to me. So when I do this in method my_courses():
$this->load->view('professor/professor_view_insert_labwork',$data);
I think that this should send data to my view: professor_view_insert_labwork, but I cannot get to that data on that view. I just don't know how and why!
This is my stripped but "IMPORTANT" view,again without name and value in select and in option, the professor_view_insert_labwork view:
<?php
echo form_open("controller_professor/insert_labwork");
?>
<table border="1">
<tr>
<td>course name</td> <td>button</td> <td></td>
</tr>
<tr>
<td>
<?php if(isset($records)) { ?>
<select>
<?php foreach ($records as $row){ ?>
<option value=""> <?php echo $row->sifra_predmeta; ?> </option>
<?php } ?>
</select>
<?php
}else {
echo"<h2>No records were returned!</h2>";
} ?>
</td>
<td>
<input type="submit" class="" value="Save" />
</td>
</tr>
</table>
<?php
echo form_close();
?>
but it constantly ejects in the else loop and echo out message No records were returned!
I do not know why in this view - professor_view_insert_labwork, I can not get up to data $records, but in test view I manage to pull out the data.
Please help me because I really do not know what was wrong in the code
Change model
function professors_classes($username)
{
$this->db->where("username", $username);
$query = $this->db->get("table_teach_a_course");
//if we have results then we will return them
$result = $query->result_array(); //here
if(!empty($result))
{
return $result; //here
}
//else we return false
else{
return false;
}
}
And controller
<?
function my_courses(){
$username = $this->session->userdata('username'); //1 username from session
$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table
$result =$this->model_professor->professors_classes($username); //3 so we send username to that model
if($result == false)
{
echo "Professor has no courses ";
}
else
{
$data['records'] = $this->model_professor->professors_classes($username);
$this->load->view('professor/test',$data);//make sure your page is correct
}
}
?>
In view
<?php if(!empty($records))
{
?>
<select>
<?php
foreach ($records as $row)
{
?>
<option value=""> <?php echo $row['sifra_predmeta'] ?> </option>
<?php
}
?>
</select>
<?php
}
?>
in your model replace
if($query->result()){
return $query->result();
}
with
if($query->num_rows() > 0){
return $query->result();
}