Multiple Input fields at once

Hello,

I want save multiple prices for one meal at the same action.
The meals have different prices for the different user groups.
In the form i’ve try.

echo $this->Form->input('meal_id', ['options' => $meals]);
foreach($groups as $group){
echo $this->Form->input('price.'.$group['id'], array(
		 'label'   	=> $group['name'] . ' '. __('Price'),
		 'id'    	=> 'price['. $group['id'].']',
		 'empty'   	=> true,
		 'style'	=> 'width:160px;'
	));
}

And in the controller I've try this :) 
// $i is also the nr of the price[id] from the input field
for ($i = 1; $i <= $max; $i++) {
   $price = $this->Prices->patchEntity($price, $this->request->data);
   
   ????

}

First try …

Now I try this.
Intentions is. There is only one view for the client in which every price can be add or edit.

in my template

<div class="prices large-9 medium-8 columns content">
<?= $this->Form->create($price) ?>
<fieldset>
    <legend><?= __('Prices') ?></legend>
    <?php
	echo '<table>';
		echo '</thead><tr>';
	    echo '<th>Essen</th>';
		foreach($groups as $group){
			echo '<th><!-- layout --></th>';
		}
		echo '</tr></thead>';
		echo '<tbody>';
        foreach($meals as $meal){
        	echo '<tr><th>'.$this->Form->hidden($meal['id'], array(
					 'id'    	=> $meal['id']
				)) . $meal['name'].'</th>';
			foreach($groups as $group){
				echo '<th>';
				echo $this->Form->input('price.'.$group['id'], array(
					 'label'   	=> $group['name'] . ' '. __('Price'),
					 'id'    	=> 'price['. $group['id'].']',
					 'empty'   	=> true
				));
				echo '</th>';
			}
			echo '</tr>';
		}
	echo '</tbody></table>';
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

and in the controller I’ve try this now…

$price = $this->Prices->newEntity();

    if ($this->request->is('post')) {		
		return debug($this->request->data);
    }
	
	
    $groups = $this->Prices->Groups->find('all');
    $meals = $this->Prices->Meals->find('all');
	$prices = $this->Prices->find('all');
	$this->set('prices', $prices);
	$this->set('meals', $meals);
	$this->set('groups', $groups);
	$this->set(compact('price'));

but the debug return only… [ (int) 1 => ‘’, ‘price’=>[(int) 1 => ‘’; …]

What is the way to do this?
I’m going to the cellar and drink a beer, now.

Thank you for your help,
Marc

My solution looks like this now. But is it really the right way?

In the controller:

	$price = $this->Prices->newEntity();

    if ($this->request->is('post')) {
		//clean up the old prices
		$cn = ConnectionManager::get('default');
		$cn->query("DELETE FROM prices");
		$cn->query("ALTER TABLE prices AUTO_INCREMENT=1");
		
		$meldung = 0;		
		$mmax = sizeOf($this->request->data['meal_id']);
		for ($m = 1; $m <= $mmax; $m++) {
			$pmax = sizeOf($this->request->data['meal_id'][$m]['price']);
			for ($p = 1; $p <= $pmax; $p++) {
				$price = $this->Prices->newEntity();
				$price->group_id = $p;
				$price->meal_id = $m;
				$price->price = ($this->request->data['meal_id'][$m]['price'][$p] > 0.00) ? $this->request->data['meal_id'][$m]['price'][$p] : 0.00;

				if ($this->Prices->save($price)) {
					$meldung = 0;
				}else{
					$meldung = 1;
					$this->Flash->error(__('The price could not be saved. Please, try again.'));
					break;
				}	

			}
			
		}
	
		if($meldung == 0){
			$this->Flash->success(__('The prices has been saved.'));
		}
		return $this->redirect(['action' => 'index']);
    
    }

    $groups = $this->Prices->Groups->find('all');
    $meals = $this->Prices->Meals->find('all');
	$bla = $this->Prices->find('all');
	$prices = $bla->toArray();

	$this->set('prices', $prices);
	$this->set('meals', $meals);
	$this->set('groups', $groups);
	$this->set(compact('price'));
}

In the template:

<?= $this->Form->create($price) ?>
<fieldset>
    <legend><?= __('Prices') ?></legend>
    <?php
	    echo '<table>';
		echo '</thead><tr>';
	    echo '<th>Essen</th>';
		foreach($groups as $group){
			echo '<th><!-- layout --></th>';
		}
		echo '</tr></thead>';
		echo '<tbody>';
        foreach($meals as $meal){
        	echo '<tr><th>'.$this->Form->hidden('meal_id.'.$meal['id'].'.meal_id', array(
				     'value'=> $meal['id'],
					 'id'=> $meal['id']
				)) . $meal['name'].'</th>';
			foreach($groups as $group){
				
				$i = $this->Hkw->multidimensional_search($prices, array('group_id'=>$group['id'], 'meal_id'=>$meal['id']));
		
				echo '<th>';
				echo $this->Form->input('meal_id.'.$meal['id'].'.price.'.$group['id'], array(
					 'label'   	=> $group['name'] . ' '. __('Price'),
					 'id'    	=> 'price['. $group['id'].']',
					 'value'    => $prices[$i]['price'],
					 'placeholder' => '0.00',
					 'type'     => 'number',
					 'step'     => '0.01',
					 'pattern'  => '^\d+(?:\.\d{1,2})?$',
					 'onblur'   => "this.parentNode.parentNode.style.backgroundColor=/^\d+(?:\.\d{1,2})?$/.test(this.value)?'inherit':'red'",
					 'empty'   	=> true
				));
				echo '</th>';
			}
			echo '</tr>';
		}
	    echo '</tbody></table>';
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

The helper:

function multidimensional_search($parents, $searched) { 
  if (empty($searched) || empty($parents)) { 
    return false; 
  } 

  foreach ($parents as $key => $value) { 
    $exists = true; 
    foreach ($searched as $skey => $svalue) { 
      $exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue); 
    } 
    if($exists){ return $key; } 
  } 

  return false; 
}

Bye,
Marc