Building a repeating block

Permalink
Hi,

I'm building a team block, it contains Image and text fields to display a picture of the person, a name, job title and description.

This block works fine, however I'm trying to turn it into a repeating block so that there can be multiple members per block for the admins ease of use.

I'm thinking it would need similar functionality to the Image Slider block, where you can click "Add entry" and it appends a new form to the current one, then saves the data to the database.

Can anyone point me in the direction of any tutorials or documentation that may be able to help?

Thanks.

 
mnakalay replied on at Permalink Reply
mnakalay
My suggestion is to look at that code you're mentioning. The general idea is usually always the same.

1- Create a template for a user (name and picture) that waits hidden somewhere in your code
2- make sure field names end with [], for instance name[] instead of just name so each field will be treated as an array holding more than 1 value
3- upon save go through the array and save each user
4- avoid checkboxes at all cost as if not checked they simply don't send anything and your array's indexes will be all wrong
adamjellybean replied on at Permalink Reply
Thanks for your response, I've attempted to do as you've suggested but I'm getting nowhere fast, I assume I've misunderstood.

I've built a test block to try this out.

Form.php

<input type="text" name="name[]">
<input type="text" name="name[]">
<input type="text" name="name[]">


Controller.php - Save Function

public function save($data){
    $names = $data['name'];
    foreach ($names as $name) {
      parent::save($name);
    }
  }
mnakalay replied on at Permalink Reply
mnakalay
It should be more like
<input type="text" name="name[]">
<input type="text" name="jobTitle[]">
<input type="text" name="description[]">

Let's assume the name is a mandatory field. Then in your Controller.php - Save Function
public function save($data){
    $db = Loader::db();
    $names = $data['name'];
    if ($names) {
        foreach ($names as $index=>$name) {
          $jobTitle = $data['jobTitle'][$index];
          $description = $data['description'][$index];
          $args = array('name' => $name, 'jobTitle' => $jobTitle, 'description' => $description);
          // here you need to save $args. You need to use your own saving code.
          $db->query("INSERT INTO yourTableName (name, jobTitle, description) values (?, ?, ?)", $args);
        }
    }
  }

Don't forget to replace yourTableName by the name of the table where you want to save the data
adamjellybean replied on at Permalink Reply
Hi mnakalay,

Thanks for your help. I've tried to strip back the idea a bit to get a better understanding, so I've created a test block with only one field called "name".

I want to be able to repeat the name input field by appending the input onto the form with jQuery as it seems to work that way in the image slider block.

Therefore I've written my Form.php as so

<input type="text" name="name[]">
<input type="text" name="name[]">


I've written the save function like this:

public function save($data){
    $db = Loader::db();
    $names = $data['name'];
    foreach ($names as $index=>$name) {
      $args = array('name'=>$name);
      $db->query("INSERT INTO btRepeat (name) VALUES (?)", $args);
    }
  }


However, when attempting to save, I get the following error.

An exception occurred while executing 'INSERT INTO btRepeat (name) VALUES (?)': SQLSTATE[HY093]: Invalid parameter number: no parameters were bound


Thanks again for your help.
hutman replied on at Permalink Reply
hutman
There is an error in your syntax

$args = array($name);


Should fix it
hutman replied on at Permalink Reply
hutman
It might be helpful to you to take a look at the approach that some of the Slider blocks use, it's a similar idea and there are some free ones that would give you an idea of what to do.

https://www.concrete5.org/marketplace/addons/galleria_image_gallery/...