[eluser]nydeveloper[/eluser]
I apologize in advance if this is a simple question, but I've been unable to figure it out or find any reference to it anywhere.
I'm retrieving specific results from a DB table, and then displaying them. Next, what I'd like to do is assign each individual result to a variable. I'd then like to use these variables in other functions. In the code below, I'd like to assign math_grade, history_grade, art_grade, and science_grade to variables that I can use in formulas and DB insert/updates.
Code: function get_student_grades($student_id)
{
$this->db->where('student_id', $student_id);
$this->db->select('math_grade, history_grade, art_grade, science_grade');
$query = $this->db->get('student_grades');
return $query->result();
}
Any help here is greatly appreciated!
[eluser]Bhashkar Yadav[/eluser]
i think, you shared code of your model and doing in controller like
Code: $this->load->model('you_model_nane');
$result = $this->you_model_nane->get_student_grades($student_id);
you can fetch each records within foreach loop
Code: foreach($result as $res){
$a = $res->column_name;
.....
.....
}
if student_id is treated as primary key why don't you use
Code: return $query->row();
into your model. you can direct assign values fetched from db like
Code: $a = $result->column_name;
[eluser]nydeveloper[/eluser]
Sorry about that. I forgot to post the controller code. Here it is:
Code: function getStudentGrades() {
// Assign the URI variables
$student_id = $this->uri->segment(3);
// Load the Database Model
$this->load->model('Student_Model', '', TRUE);
// Call the Database Select from student/grades, json encode and echo to view
echo json_encode($this->Student_Model->get_student_grades($student_id));
}
I will try your solution, thanks! Once these variables are assigned can they be used in other functions within the same controller?
[eluser]Aken[/eluser]
If that function is only designed to return a single row for a single student, you can reduce your code like so:
Code: function get_student_grades($student_id)
{
$this->db->select('math_grade, history_grade, art_grade, science_grade')
->where('student_id', $student_id);
return $this->db->get('student_grades')->row_array();
}
That function will return a single associative array like:
Code: array(
'math_grade' => value,
'history_grade' => value,
//etc...
);
Then you can run that variable through PHP's extract() function to assign individual variables for each array value.
[eluser]Bhashkar Yadav[/eluser]
yes, use variables in controller like
private $variable1 ; (set accessibility yourself)
and assign values into controller method getStudentGrades() using $this
Code: $this->variable1 = $value1;
$this->variable1 can be used into other methods also.
[eluser]nydeveloper[/eluser]
I'm having some issues with this part:
Quote:yes, use variables in controller like
private $variable1 ; (set accessibility yourself)
what is the proper syntax for defining said variables in the controller?
I've only been able to successfully declare variables in the controller if they are coming from the URI, or if I'm assigning direct values to them in the controller itself.
In this case they are obtaining their value as part of the results of the db select and get in the model...
[eluser]PhilTem[/eluser]
There is no need to loop over the variables like @Bhashkar did. You can use PHP's handy function extract which will use an associative array to create variables. So the key will be the variable name, the value it's value.
You can create your results from the db with
Code: $query = $this->db
->where('student_id', $student_id)
->select('math_grade, history_grade, art_grade, science_grade');
// For getting all rows
$rows = $query->result();
// For getting one row (i.e. the first matching row)
$row = $query->row();
And then use either
Code: foreach ( $rows as $row )
{
extract $row;
echo $math_grade;
echo $history_grade;
echo $art_grade;
echo $science_grade;
}
for multiple rows, or for a single row without the foreach.
Don't forget: extract() comes in pretty handy
[eluser]nydeveloper[/eluser]
I have tried all day to get my student_model file to like this syntax, only to keep coming up with a server error:
Quote:Code: $query = $this->db
->where('student_id', $student_id)
->select('math_grade, history_grade, art_grade, science_grade');
// For getting all rows
$rows = $query->result();
// For getting one row (i.e. the first matching row)
$row = $query->row();
And then use either
Code: foreach ( $rows as $row )
{
extract $row;
echo $math_grade;
echo $history_grade;
echo $art_grade;
echo $science_grade;
}
for multiple rows, or for a single row without the foreach.
I've tried both the multiple rows and single row approach, to no avail. Your syntax looks perfectly fine to me, so I'm having a very tough time figuring out what is going on here...
[eluser]CroNiX[/eluser]
Try putting the select() before the where(). I believe the order is still important with AR. Oh, you also never get() the data
Code: $data = $this->db
->select('math_grade, history_grade, art_grade, science_grade')
->where('student_id', $student_id)
->get('student_grades')
->row_array();
if(count($data))
{
extract($data);
echo $math_grade . '<br />';
echo $history_grade . '<br />';
echo $art_grade . '<br />';
echo $science_grade . '<br />';
}
else
{
echo 'No results.';
}
[eluser]nydeveloper[/eluser]
Quote:Try putting the select() before the where(). I believe the order is still important with AR. Oh, you also never get() the data
Code: $data = $this->db
->select('math_grade, history_grade, art_grade, science_grade')
->where('student_id', $student_id)
->get('student_grades')
->row_array();
if(count($data))
{
extract($data);
echo $math_grade . '<br />';
echo $history_grade . '<br />';
echo $art_grade . '<br />';
echo $science_grade . '<br />';
}
else
{
echo 'No results.';
}
That did the trick. Thanks everyone!!!
|