Pass array to Modal

Asked

Viewed 619 times

1

I have the following array $detalhes:

 Array ( [0] => Array ( [dt_detalhes] => 2016-03-09 [desc_detalhes] => Viabilidade [tp_processo] => Viabilidade [vl_protocolo] => 1234 ) 
         [1] => Array ( [dt_detalhes] => 2016-03-12 [desc_detalhes] => Sincronizado [tp_processo] => Sincronizado [vl_protocolo] => 12345 ) 
         [2] => Array ( [dt_detalhes] => 2016-03-11 [desc_detalhes] => Integrador [tp_processo] => Integrador [vl_protocolo] => 123456 ) )

I’d like to move him into a modal, that way:

<button type="button" data-toggle="modal" data-target="#detalhes" 
data-id="<?=$linha['id_processo']?>" data-detalhes="<?=$details?>">

How can I do that? In the way tried above the following error:

Array to String Conversion

OBS: I need to pass the entire array.

  • Your echo should be in a specific key and not in the entire array

  • @rray has no way to pass the entire array?

  • Post the complete structure of your array, what you really want to print??

2 answers

1


The echo serves to print scaler values(int, string float etc) except arrays and objects.

You can take the array and turn it into a json string with php and by javascript convert to an object.

<button id="button" type="button" data-toggle="modal" data-target="#detalhes" 
data-id="<?=$linha['id_processo']?>" data-detalhes="<?=json_encode($detalhes);?>">

In javascript:

var data = document.getElementById('button');
var json = JSON.parse(data.getAttribute('data-detalhes'));
console.log(json);
  • posted the full array, can’t you pass the entire array? I tried with the serialize data-Details="<?= serialize($Details)? >", passed correctly but how to recover it in Jquery?

  • @Bia vc take this array and turn into a json string with php and retrieve it through data-attribute

  • i get it like this: var Details = button.data('details') in Jquery has the equivalent of unserialize?

  • @Bia edited the answer.

  • returns the error: Uncaught Syntaxerror: Unexpected end of input, I checked the array in JSON and it is closed correctly with [ { } ]

  • @Bia gave error in JSON.parse()? you put the id on the button?

Show 1 more comment

1

If you want to print array specific data, you need to print so:

echo $details[0]['dt_detalhes'];

He will print the field dt_detalhes of the first array item 0

If you want to print all fields and arrays, you should run them using some loop function, example:

for($i=0; $i < count($details); $i++) {
    echo $details[$i]['dt_detalhes']; // campo desejado, exemplo 'vl_protocolo'
}

To better understand how an array works read the documentation, in case I have doubts, post here.

  • I want to pass the whole array to modal through data-attribute

  • 1

    this data (dt_details, desc_details, tp_process, vl_protocol) will be separated as when they are printed? all together in the same field?

  • I will print this way, but first I would like to pass this data to a specific modal.

  • 1

    Bia, really is a little confused what you want, give more details, how you want to show this information, where it comes from and etc..

Browser other questions tagged

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