How to traverse a php fix in javascript?

4

I'm using laravel for a project, I present the following problem, in the controller I have an array called scaling value that is filled, then that array was sent to view using return and compact, in the view I need to pass that array to a script and traverse it within javascript, the php array passed to the script as follows in the blade view:

<script>
var cont= "<?php echo $count;  ?>";
for (var index1 = 0; index1 < cont; index1++) {

        precioesca[index1]="<?php echo $escalafonvalor[".index1."] ?>";        


        alert(precioesca[index1]);

    }
</script>

the array arrives in this way:

array:3 [▼
  0 => 13282000
  1 => 4688000
  2 => 2344000
]

The array passes correctly to the script, because if I give it index 0 it shows its value and stores it in the variable js. My problem is, when trying to traverse the array with the variable index1 of the javascript for, since it tells me that the variable index1 is undefined:

What can be wrong here? or how should it be done?

    
asked by Ándres Felipe Patiño 25.04.2018 в 21:52
source

4 answers

0

Using the information that Marcos gave me, I used it in this way, I enclose my code in case it serves someone.

var precioesca = [];
    var escalafon = "<?php echo json_encode($escalafonvalor); ?>";

    precioesca=JSON.parse(escalafon);


    for (var index = 0; index < precioesca.length; index++) {

        alert(precioesca[index]);
    }
    
answered by 26.04.2018 / 00:12
source
3

In this case the variable index1 is a variable in javascript, but you try to access it in php. The correct way to do it would be:

<script>
var precioesca = [];
var escalafon = <?php echo json_encode($escalafonvalor);  ?>;
var newEscalafon = JSON.parse(escalafon);

newEscalafon.forEach(function(value, index) {
    precioesca[index] = value;
    alert(precioesca[index]);
});
</script>
    
answered by 25.04.2018 в 22:02
3

Consider that the code PHP is compiled and executed on the server side, instead JS is compiled and executed on the client side ( normally a browser ).

In other words, first the code PHP is executed, the result is returned to the client and it executes JS .

Bearing this in mind, if you need to scroll in JS information stored in a variable of PHP , what you have to do is print with PHP such information becomes visible in JS

Solution:

You can use the json_encode function, which allows you to convert the value of the variable to an object that JS can process.

Example:

<script>
var precioesca = [];
var escalafon = <?php echo json_encode((array) $escalafonvalor);  ?>;

escalafon.forEach(function(value, index) {
    precioesca[index] = value;
    alert(precioesca[index]);
});
</script>

PD: The expression (array) $escalafonvalor will convert ( in the case that it was not ) $escalafonvalor in an array, thus allowing correct iteration with JS .

    
answered by 25.04.2018 в 23:19
1

You can do it in the following way by using length , which causes you to take the total of the length of the array and it would be translated as for index < 10 , so to speak as an example.

<script>
var cont= "<?php echo $count;  ?>";
for (var index1 = 0; index1 < cont.length; index1++) {  
        alert(cont[index1]);

    }
</script>

I leave you an executable example so you can see it in action.

 
    var cont = ["valor1",2,3,4,"valor3","texto",2,4];
    for (var index1 = 0; index1 < cont.length; index1++) {  
            console.log(cont[index1]);

    }
    
answered by 25.04.2018 в 21:57