Incrementation on blueprint field name?

Hi,
I have 10 text fields in a blueprint, which are only differentiated by their number:
fields:
– text1:
– – …
– text2:
– – …

When calling for example page->text(), is there a way to increment the number after “text” in a loop ? I have tried different things like “putting the incremented number between the field name and the too bracets” before calling it, but isn’t it any cleaner solution ?

You could use a variable that store the number, something like:

$i = 1;
foreach($fields as $field) {
  $method = 'text' . $i;
  echo $page->$method();
  $i++;
}
2 Likes

Thank you texnixe, this helped to clear my solution.

Thank you for the solution!

I just got a bit lost implementing it in my controller.php. I am saving input form values in a text file using Creating pages from frontend.

risposte is the folder within the folder associated with this specific controller.php, but in this case it’s not clear to me the function of $risposta, since we are not iterating through that item.

This is the relevant code so far, but tell me if more is needed:

$survey_number = 1;
$risposte = $page->find('risposte');

foreach($risposte->children() as $risposta) {
  $newSurvey = $risposte->children()->create(str::slug($survey_number . '-' . date('Y-m-d-H-i-s')) , 'survey', $data);
  $survey_number++;
}

I don’t quite see what your question has to do with the above topic?

It seems you are trying to create new pages but why within that foreach loop? Can you tell me what exactly you are trying to achieve?

Sorry about it.

I am trying to add an incremental number to the beginning of the folder’s name (see $newSurvey). I thought it was possible to add to each new folder created, a new number based on the total amount of folders present in risposte's folder.

So I think I should count, within the foreach loop, how many folders there are after I created a new folder (or before?) and add the relative number based on it?

Thanks for making me clarifying my thoughts!

You can get the number of folders in risposte using the count() method; so any new page would then just be +1;

$risposte = $page->find('risposte');
$survey_number = $risposte->children()->count() + 1;

No foreach loop required here.

Yes, thanks for the answer.

I only realised when I was re-formulating the question that it was a matter of counting the number of already existing folders!