Reputation: 1479
I'am making a stock control system for sugar, I need to print and compare values, just have a problem when I want to printf a valor of a array multidimencional example, this is my array:
Array
(
[0] => Array
(
[Azucar] => Array
(
[id] => 1
[nombre] => 21
[produccion] => verano.
[cantidad] => 10032.32
[fecha] =>
)
[grupo] => Array
(
[id] => 2
[categoria] => azucar
[subcategoria] => ingenio
[created] => 2008-02-13 18:34:56
)
[AzucarExistencia] => Array
(
[0] => Array
(
[id] => 1
[cantidad] => 3250
[cantidadtotal] => 325.12
[sacarosa] => 32
[refinada] => 956
)
[1] => Array
(
[id] => 2
[cantidad] => 4000
[cantidadtotal] => 564
[sacarosa] => 12
[refinada] => 780
)
[2] => Array
(
[id] => 3
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[3] => Array
(
[id] => 4
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
)
)
[1] => Array
(
[Azucar] => Array
(
[id] => 2
[nombre] => 32
[produccion] => verano.
[cantidad] => 9032.32
[fecha] =>
)
[grupo] => Array
(
[id] => 3
[categoria] => azucar
[subcategoria] => ingenio
[created] => 2008-02-13 18:34:56
)
[AzucarExistencia] => Array
(
[0] => Array
(
[id] => 6
[cantidad] => 3250
[cantidadtotal] => 325.12
[sacarosa] => 32
[refinada] => 956
)
[1] => Array
(
[id] => 7
[cantidad] => 4000
[cantidadtotal] => 564
[sacarosa] => 12
[refinada] => 780
)
[2] => Array
(
[id] => 8
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[3] => Array
(
[id] => 9
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
[4] => Array
(
[id] => 10
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[5] => Array
(
[id] => 11
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
)
)
)
I need to compare all the values within AzucarExistencia, the first foreach is so:
<?php foreach ($azucares as $azucar): ?>
<tr>
<td style="text-align: center;"><?php echo $azucar['Azucar']['username']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['email']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['subcategoria']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($azucar); ?>
but then needed to compare values within AzucarExistencia, try that:
<?php foreach ($azucares as $azucar): ?>
<tr>
<td style="text-align: center;"><?php echo $azucar['Azucar']['username']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['email']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['subcategoria']; ?></td>
<?php foreach ($azucar as $azucarexistencia): ?>
<td style="text-align: center;"><?php
if ($azucarexistencia['AzucarExistencia']['cantidad']<1500) {
echo 'peligro';
}
?></td>
</tr>
<?php endforeach; ?>
<?php unset($azucar); ?>
now the error is
Undefined index: azucarexistencia
or
Undefined index: AzucarExistencia
checked with debugkit the values ,come okay it's just make a good foreach, I try but i dont now how.
model
<?php
/**
*
*/
class Azucar extends AppModel
{
public $name ='User';
public $useTable = 'users';
public $primaryKey = 'id';
public $belongsTo = 'Grupo';
public $hasOne = 'Perfil';
public $hasMany = array('Deposito','AzucarExistencia');
}
?>
controller
<?php
/**
*
*/
class AzucaresController extends AppController
{
public $components = array('Session','RequestHandler');
public function index()
{
$this->loadModel('Azucar');
$this->paginate = array(
'conditions' => array('Grupo.categoria' => 'Azucar'),
'limit' => 25
);
$this->set('azucares', $this->paginate('Azucar'));
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
Upvotes: 0
Views: 96
Reputation: 3164
<?php
foreach ($azucares as $azucar){
foreach ($azucar['AzucarExistencia'] as $azucarexistencia){
if ($azucarexistencia['cantidad']<1500) {
echo 'peligro';
}
}
}
?>
Upvotes: 0
Reputation: 435
Replace the loop as so
<?php foreach ($azucar['AzucarExistencia'] as $azucarexistencia): ?>
<td style="text-align: center;"><?php
if ($azucarexistencia['cantidad']<1500) {
echo 'peligro';
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
What you were doing wrong was you were trying to loop through all indexes of $azucar
so it would loop through $azucar["Azucar"]
$azucar["grupo"]
$azucar["AzucarExistencia"]
but only $azucar["AzucarExistencia"]
needs to be looped through
Upvotes: 1