Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Tips and Tutorials
How to Write a controller for get data from table 2 with id table 1
soulcrys
December 2014
Hi everybody, I'm newbie fuel,
I'm bulding a simply student's gradebook system. I only use 2 tables are students and student_gradebooks.
table students(id, fullname, etc...)
table: student_gradebooks(id, student_id, subject, grade).
Problem is, I dont know how to create a controller to get data form table
student_gradebooks when I had id student from table students.
Anybody could send me some examples ?
Harro
December 2014
Accepted Answer
Look into ORM.
Create a model for your two tables, or use the 'oil refine fromdb' commandline command to generate them. Add the relation between the two (student has_many gradebooks, gradebook belongs_to student), and off you go.
After that it will be as easy as
$student = Model_Student::query()->where('id', '=', 1)->related('gradebook')->get_one();
// show the students fullname
echo $student->fullname;
// show the subjects and grade
foreach ($student->gradebook as $grade)
{
echo $grade->subject, $grade->grade;
}
Job done.
;-)
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
December 2014