Array with inputs and a specific data!

0

Just right here to solve certain things.

I'm breaking my head to do this loop but I have not gotten it yet.

How do I put the highlight in this array (), with only 1 of those photos being marked as 1.

$fotos = $this->input->post('fotos[]');
$destaque = $this->input->post('destaque[]');

$foto = array();

foreach ( $fotos as $item => $value){
    array_push($foto, array(
        'idFoto' => $id,
        'img' => $value
        'destaque' => $
    ));
}

The result I hope is something like this:

array(
array(idFoto => 1,
img => 1.jpg,
destaque => Null),

array(idFoto => 2,
img = 2.jpg,
destaque => 1), //Nesse caso o radio foi checado

array(idFoto => 3,
img => 3.jpg,
destaque => Null)
);

In the case of html I make the input of several photos and one of them I select as a highlight.

    
asked by anonymous 24.08.2015 / 19:23

2 answers

0

Use foreach to change the highlight status when idFoto matches the value of $destaque . I made an example with array in the Ideone , but I just left the loop here.

The result is destaque = 1 , only in index array 1 (corresponding to photo idFoto 2 )

foreach( $fotos as $n => $foto )
{
    if( $foto['idFoto'] === $destaque /* 2 */ )
    $fotos[$n]['destaque'] = 1;
}

Output

Array(
    [0] => Array(
            [idFoto] => 1
            [img] => 1.jpg
            [destaque] => 
        )

    [1] => Array(
            [idFoto] => 2
            [img] => 2.jpg
            [destaque] => 1
        )

    [2] => Array(
            [idFoto] => 3
            [img] => 3.jpg
            [destaque] => 
        )
)
    
24.08.2015 / 20:01
0

An example of how to do implementation type is as follows:

Form

<form>
   <?php
      for($i=0;$i<5;$i++){
   ?>
   <input type="text" name="foto[<?=$i?>]"><input type="radio" name="destaque" value="<?=$i?>"><br>
   <?php
      }
   ?>
</form>

PHP

<?php
   // Fotos é um array
   $fotos = $this->input->post('fotos');
   // Destaque é o número do índice da foto que será o destaque
   $destaque = $this->input->post('destaque');

   $foto = array();

   foreach ( $fotos as $item => $value){
       array_push($foto, array(
           'idFoto' => $id,
           'img' => $value
           'destaque' => intval($item == $destaque)
       ));
   }

?>

The expression $item == $destaque checks if the index of the current element in loop is equal to the number of the highlight that will return true or false the intval function will transform the boolean result into numeric 1 or 0 .

    
24.08.2015 / 20:13