Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 Model - One to Many - how to?
#1

(This post was last modified: 09-28-2021, 01:43 AM by InsiteFX. Edit Reason: Fixed Bad text output please do not copy and paste )

Hey guys,

I am new to CodeIgniter 4 and self-taught, so for sure I am missing some skill or knowledge :-).

Anyway - I am building simple quiz. One page will have multiple questions, and these questions will have multiple answers.

I have three database tables for pages, questions and answers. Each of them has own Model. And I'm storing IDs of answers in questions table as json encoded aswers IDs. Same for questions IDs in pages table.

Getting data for view is like scratching your left ear with your right hand.

I am looping for json decoded arrays and finding items by IDs from other Model
It is working well, but I am not sure that "this is the way".

I was googling But I'm not sure how to do it the CodeIgniter. Can you kick me in the right direction?

Thanks.
Reply
#2

Sorry, I can't your post. Would you fix the format?
Reply
#3

Your better off using pivot tables see the link below.

Pivot Tables in MySQL

Your data will start to run wild when it gets larger.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

I don't know if you've already done it, but let me suggest you take a look at data modeling, most of the time it can come up with solutions to your questions.
Reply
#5

(09-27-2021, 08:04 AM)Scott103 Wrote: Hey guys,

I am new to CodeIgniter 4 and self-taught, so for sure I am missing some skill or knowledge :-).

Anyway - I am building simple quiz. One page will have multiple questions, and these questions will have multiple answers.

I have three database tables for pages, questions and answers. Each of them has own Model. And I'm storing IDs of answers in questions table as json encoded aswers IDs. Same for questions IDs in pages table.

Getting data for view is like scratching your left ear with your right hand.

I am looping for json decoded arrays and finding items by IDs from other Model
It is working well, but I am not sure that "this is the way".

I was googling But I'm not sure how to do it the CodeIgniter. Can you kick me in the right direction?

Thanks.

Hi,

You are new to CI. What are you comfortable with and what have you used previously? e.g. backend PHP or frontend JavaScript. CI is backend so your data would normally be recordsets and arrays, whereas json is usually for feeding into frontend JavaScript. So you shouldn't need to decode the data unless your are storing json in the table columns.

I think you will only need one Model to create, read, update and delete your questions/answers data.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB