Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Repeat of multiple objects

93 views
Skip to first unread message

Jeffrey Röling

unread,
Mar 24, 2020, 2:24:34 PM3/24/20
to Fat-Free Framework

Is there a way to do this in Fat Free Framework.
The question must be on repeat but the user is identical

<!-- Question 1 -->
<check if="{{ @questions[0].enabled  == '1' }}">
   
<true>
       
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3" style="margin-bottom:10px;">
           <div class="card
<check if="{{ @questions[0].answer == @user[0].answer0 }}"><true>text-white bg-success</true></check> <check if="{{ @user[0].answer0 == '' }}"><true>text-dark bg-light</true></check> <check if="{{ @questions[0].answer != @user[0].answer0 }}"><true>text-white bg-danger</true></check>">
                <a href="#" data-toggle="collapse" data-target="#collapse{{ @questions[0].id }}" aria-expanded="true" class="
<check if="{{ @user[0].answer0 == '' }}"><true>text-dark</true><false>text-white</false></check>">
                   
<header class="card-header p-2">
                       
<div class="row">
                           
<div class="col text-left"><b>Vos - {{ @questions[0].id }}</b></div>
                       
</div>
                   
</header>
               
</a>
               
<div class="collapse hide" id="collapse{{ @questions[0].id }}" style="">
                   
<div class="card-body p-2">
                       
<check if="{{ @SESSION.lvl=='1' }}"><true><div>{{ @questions[0].easy }}</div></true></check>
                       
<check if="{{ @SESSION.lvl=='2' }}"><true><div>{{ @questions[0].normal }}</div></true></check>
                       
<check if="{{ @SESSION.lvl=='3' }}"><true><div>{{ @questions[0].hard }}</div></true></check>
                   
</div>
                   
<div class="card-footer p-2">
                       
<check if="{{ @questions[0].answer == @user[0].answer0 }}">
                           
<true>Het antwoord <b>{{ @user[0].answer0 }}</b> is goed.</true>
                           
<false>
                               
<form role="form" method="POST" enctype="multipart/form-data" action="/app/checkanswer">
                                   
<div class="input-group p-0 m-0">
                                       
<input type="text" name="answer0" class="form-control" placeholder="Antwoord" value="{{ @user[0].answer0 }}" onkeyup="this.value = this.value.toLowerCase();">
                                       
<div class="input-group-append">
                                           
<input type="hidden" name="update" value="update" />
                                           
<input type="hidden" name="id" value="{{ @user[0].id }}" />
                                           
<button class="btn btn-primary" type="submit" >Controlleer</button>
                                       
</div>
                                   
</div>
                               
</form>
                           
</false>
                       
</check>
                   
</div>
               
</div> <!-- End Collapse .// -->
           
</div> <!-- End Card .// -->
       
</div>
   
</true>
</check>


<!-- Question 2 -->
<check if="{{ @questions[1].enabled  == '1' }}">
   
<true>
       
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3" style="margin-bottom:10px;">
           <div class="card
<check if="{{ @questions[1].answer == @user[0].answer1 }}"><true>text-white bg-success</true></check> <check if="{{ @user[0].answer1 == '' }}"><true>text-dark bg-light</true></check> <check if="{{ @questions[1].answer != @user[0].answer1 }}"><true>text-white bg-danger</true></check>">
                <a href="#" data-toggle="collapse" data-target="#collapse{{ @questions[1].id }}" aria-expanded="true" class="
<check if="{{ @user[0].answer1 == '' }}"><true>text-dark</true><false>text-white</false></check>">
                   
<header class="card-header p-2">
                       
<div class="row">
                           
<div class="col text-left"><b>Vos - {{ @questions[1].id }}</b></div>
                       
</div>
                   
</header>
               
</a>
               
<div class="collapse hide" id="collapse{{ @questions[1].id }}" style="">
                   
<div class="card-body p-2">
                       
<check if="{{ @SESSION.lvl=='1' }}"><true><div>{{ @questions[1].easy }}</div></true></check>
                       
<check if="{{ @SESSION.lvl=='2' }}"><true><div>{{ @questions[1].normal }}</div></true></check>
                       
<check if="{{ @SESSION.lvl=='3' }}"><true><div>{{ @questions[1].hard }}</div></true></check>
                   
</div>
                   
<div class="card-footer p-2">
                       
<check if="{{ @questions[1].answer == @user[0].answer1 }}">
                           
<true>Het antwoord <b>{{ @user[0].answer1 }}</b> is goed.</true>
                           
<false>
                               
<form role="form" method="POST" enctype="multipart/form-data" action="/app/checkanswer">
                                   
<div class="input-group p-0 m-0">
                                       
<input type="text" name="answer1" class="form-control" placeholder="Antwoord" value="{{ @user[0].answer1 }}" onkeyup="this.value = this.value.toLowerCase();">
                                       
<div class="input-group-append">
                                           
<input type="hidden" name="update" value="update" />
                                           
<input type="hidden" name="id" value="{{ @user[0].id }}" />
                                           
<button class="btn btn-primary" type="submit" >Controlleer</button>
                                       
</div>
                                   
</div>
                               
</form>
                           
</false>
                       
</check>
                   
</div>
               
</div> <!-- End Collapse .// -->
           
</div> <!-- End Card .// -->
       
</div>
   
</true>
</check>

Paul Herring

unread,
Mar 24, 2020, 2:52:21 PM3/24/20
to Fat-Free Framework
1) There's far too much logic in there that should really be being done in the php calling that template.
2) I think the concepts at https://fatfreeframework.com/3.6/views-and-templates#RepeatingSegments are what you're asking for (specifically `repeat group`)

--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/07bb0f7f-7ded-46f1-a385-b67fdd4720ac%40googlegroups.com.


--
PJH

ved

unread,
Mar 24, 2020, 2:59:48 PM3/24/20
to f3-fra...@googlegroups.com
Hi,

+1 on what @PJH said and you'll have to do a better job describing what you're trying to achieve in more detail.

"a way to do this" is not really helpful if we then have to interpret that wall of templated markup to guess what "this" is.

What exactly are you trying to do? What are the source data arrays like? What is the issue exactly? etc

Cheers

Jeffrey Röling

unread,
Mar 24, 2020, 3:05:52 PM3/24/20
to Fat-Free Framework
Sorry, my bad. I'll try to explain a little bit more.

I was trying to do that into php but on idea where to start at the moment.

I got 2 tables 1 with questions each row is a question it had a column id,question,answer.
the other table is the users table a column id,email,answer1,anser2,answer3, enz.

In the page i create for each question at bootstrap card where the question is in and the answer of the user should be in as well. based on bad or good ill color the card in red or green.

v.

unread,
Mar 24, 2020, 4:56:25 PM3/24/20
to Fat-Free Framework
I'm having trouble understanding what it is exactly you are trying to achieve and what your problem is.
To loop over the questions you can use
<repeat group="{{ @questions }}" value="{{ @question }}">
<check if="{{ @question.enabled == '1' }}">
//work your magic here
</check>
</repeat>

Jeffrey Röling

unread,
Mar 24, 2020, 5:02:20 PM3/24/20
to Fat-Free Framework
I tried that that works but now i want to use also the {{ @user }} in there with for each answer on a question answer1 answer2 answer3.

so the first item in the repeat will be.
{{ @question.question }} and {{ @user[0].answer1 }}

The second
{{ @question.question }} and {{ @user[0].answer2 }}

the thirth
{{ @question.question }} and {{ @user[0].answer3 }}

ved

unread,
Mar 24, 2020, 5:24:59 PM3/24/20
to Fat-Free Framework
Hi,

The main issue seems to be that your database is not designed correctly or optimally for this situation so there's really no solution that I can give you that is in the scope of this forum for F3 issues or doubts as it would require going into database normalization rules which is a whole entire lesson on its own.

Your current database schema requires you to create a new "answerN" column on the users table every time you need to add a new question unless you have a fixed number of questions, and even then it wouldn't be the way to go about this.

You should have (for example) a third "user_answers" table with the following columns: "id, question_id, user_id, answer" where you place the user's answer for each question.

But since I'm kind of stuck with not much to do due to the COVID-19 quarantine, I'll give you some hints that may or may not help:

Your schema should ideally be something like the following:


questions:
id
| question                        | correct_answer
-----------------------------------------------------
1  | Who discovered relativity?      | Albert Einstein
2  | Who discovered quantum physics? | Max Planck
3  | Who discovered radioactivity?   | Marie Curie


users
:
id
| email                   | name
-------------------------------------------
1  | goodstudent@example.com | Good Student
2  | badstudent@example.com  | Bad Student


user_answers
:
id
| uid | qid | answer
----------------------------
1  | 1   | 1   | Albert Einstein
2  | 1   | 2   | Max Planck
3  | 1   | 3   | Marie Curie
4  | 2   | 1   | Donald Duck
5  | 2   | 2   | Walter White
6  | 2   | 3   | Radio Gaga

Then you can query it for each users by joining the tables with a query similar to:

SELECT q.*,u.name,a.answer FROM questions AS q
LEFT JOIN user_answers AS a ON q
.id=a.qid
LEFT JOIN users AS u ON a
.uid=u.id
WHERE a
.uid=1

This will give you a result like the following:

id | question                        | correct_answer  | name         | answer
1  | Who discovered relativity?      | Albert Einstein | Good Student | Albert Einstein
2  | Who discovered quantum physics? | Max Planck      | Good Student | Max Planck
3  | Who discovered radioactivity?   | Marie Curie     | Good Student | Marie Curie

Which are the questions and answers for user with id=1 (good student). You can then loop through this and compare answer with correct_answer and act accordingly.

This can still be optimized or queried in different ways but is much more flexible than hardcoding columns named answer1, answer2, answer3, etc.

Hope this helps. 

Cheers





Jeffrey Röling

unread,
Mar 24, 2020, 5:30:47 PM3/24/20
to Fat-Free Framework
ved, i thinks ill need to redesign my database the way you described.

ved

unread,
Mar 24, 2020, 5:45:06 PM3/24/20
to Fat-Free Framework
I think so too, it will be much more flexible and allow you to filter the data in many more ways directly with SQL.

You'll also be able to add an arbitrary number of questions without having to manually create columns for each answer. Otherwise, a questionnaire with 100 questions would require 100 columns and that is always a sign that your database can be optimized and better designed.

Good luck, cheers.

Jeffrey Röling

unread,
Mar 24, 2020, 6:05:10 PM3/24/20
to Fat-Free Framework
There is one thing.
I implemented it works okay but i got 50 question but 1 is showed, i guess thats because of the 
WHERE a.uid = '1'
Is there a solution to show all questions? and the not filled in ones as null?

Jeffrey Röling

unread,
Mar 24, 2020, 6:26:34 PM3/24/20
to Fat-Free Framework
Added this "AND a.uid=1" to the first Left Join

ved

unread,
Mar 24, 2020, 6:50:59 PM3/24/20
to Fat-Free Framework
Yes, that should return null for the questions that the user didn't answer. 

The main thing to take away from this is that, with this database schema, once you figure out the required SQL statements to get the data however you wish, your problems are no longer related with F3 or PHP and you can then use PHP/F3 to simply loop and do basic operations or comparisons.

Have fun, cheers.
Reply all
Reply to author
Forward
0 new messages