Array with inputs and a specific data!

Asked

Viewed 73 times

0

only here to settle certain things.

I’m breaking my head to make this loop but I haven’t got it yet.

How do I put the highlight in this array() and only 1 of these photos will be marked with the value 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 input several photos and one of them I select as highlight.

  • The highlight is a array also? What about the variable $destaque? is the index of some element of the array $fotos?

  • $featured comes from a radio and if checked records a value 1 but is only one of the photos will contain the value 1. in case I forgot to put the bracket there...but the bracket is not the issue...

  • In the case highlight is the photo ID or the array index? Can you give an example of the final result you expect?

  • @Jacielplacidino each photo has a radio button?

  • To make it easier, you can make one var_dump of the variables $fotos and $destaque and/or also post the HTML for the form, placing this information in the body of the question by clicking on edit just below your question.

  • @Kaduamaral yes for each photo has a radio highlight[].

  • @Jacielplacidino, try so on each radio put the value with a number of 0 incrementing one each photo, 0,1,2,3,... and there in the loop put like this 'destaque' => ($destaque == $item ? 1 : 0).

Show 2 more comments

2 answers

0

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

The result is the destaque = 1, only in the 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] => 
        )
)

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 whether the index of the current element in loop is equal to the number of the highlight that will return true or false the function intval will transform the result Boolean in numerical 1 or 0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.