1

Having following array in my model:

$data = [
            'route' => [
                    [
                        'id' => 1,
                        'departure_station' => 'Cluj-Napoca, Autogara',
                        'destination_station' => 'Bucuresti Nord',
                        'departure_date'  => 'Joi, 18 Mar 2017. 20:45',
                        'arrival_date' => 'Vi, 19 Mar 2017. 02:15',
                        'price' => 150
                    ],
                    [
                        'id' => 2,
                        'departure_station' => 'Budapest',
                        'destination_station' => 'London',
                        'departure_date'  => 'Vin, 28 Mar 2017. 20:45',
                        'arrival_date' => 'Lu, 29 Mar 2017. 02:15',
                        'price' => 250
                    ],
                    [
                        'id' => 3,
                        'departure_station' => 'Paris',
                        'destination_station' => 'Berlin',
                        'departure_date'  => 'Joi, 18 Mar 2017. 20:45',
                        'arrival_date' => 'Vi, 19 Mar 2017. 02:15',
                        'price' => 450
                    ],
            ]
        ];

I get it on my controller and it shows correctly when I do var_dump($data) I try to send this multidimensional array in my view as following :

foreach($data as $key => $route)
        {

            foreach ($route as $element)
            {

                $id = $element['id'];
                $departure_station = $element['departure_station'];
                $destination_station = $element['destination_station'];
                $departure_date = $element['departure_date'];
                $arrival_date = $element['arrival_date'];
                $price = $element['price'];
                $i = count($route);

                for($j=0; $j<=$i; $j++)
                {
                    $html = $this->load->view("front/curse_interne.php", array(
                        'id' => $id,
                        'departure_station' => $departure_station,
                        'destination_station' => $destination_station,
                        'departure_date' => $departure_date,
                        'arrival_date' => $arrival_date,
                        'price' => $price), true);
                    echo json_encode(array("status" => 1, "html" => $html));
                }

            }
        }

And I receive this array in my view in this way:

    <?php
$data = array('route'=>(array(array('id'=>$id, 'departure_station'=>$departure_station , 'destination_station'=>$destination_station, 'departure_date'=>$departure_date, 'arrival_date'=>$arrival_date, 'price'=>$price ))));

var_dump($data);
exit();

?>

All I get from this var_dump in view is just the first array:

    array(1) {
  ["route"]=>
  array(1) {
    [0]=>
    array(6) {
      ["id"]=>
      int(1)
      ["departure_station"]=>
      string(21) "Cluj-Napoca, Autogara"
      ["destination_station"]=>
      string(14) "Bucuresti Nord"
      ["departure_date"]=>
      string(23) "Joi, 18 Mar 2017. 20:45"
      ["arrival_date"]=>
      string(22) "Vi, 19 Mar 2017. 02:15"
      ["price"]=>
      int(150)
    }
  }
}

How can I get the entire array in my View? Thank you!

1 Answer 1

1

First of all change your function:

<?php
  $data = [
    'route' => [
      [
        'id' => 1,
        'departure_station' => 'Cluj-Napoca, Autogara',
        'destination_station' => 'Bucuresti Nord',
        'departure_date'  => 'Joi, 18 Mar 2017. 20:45',
        'arrival_date' => 'Vi, 19 Mar 2017. 02:15',
        'price' => 150
      ],
      [
        'id' => 2,
        'departure_station' => 'Budapest',
        'destination_station' => 'London',
        'departure_date'  => 'Vin, 28 Mar 2017. 20:45',
        'arrival_date' => 'Lu, 29 Mar 2017. 02:15',
        'price' => 250
      ],
      [
        'id' => 3,
        'departure_station' => 'Paris',
        'destination_station' => 'Berlin',
        'departure_date'  => 'Joi, 18 Mar 2017. 20:45',
        'arrival_date' => 'Vi, 19 Mar 2017. 02:15',
        'price' => 450
      ],
    ]
  ];

  foreach($data as $key => $route) {
    foreach ($route as $element) {         
      $html = $this->load->view("front/curse_interne.php", $element, true);
      echo json_encode(array("status" => 1, "html" => $html));
    }
  }
?>

each HTML (front/curse_interne.php) will get its JSON object, in your case 3 objects:

{
  "id": 1,
  "departure_station": "Cluj-Napoca, Autogara",
  "destination_station": "Bucuresti Nord",
  "departure_date": "Joi, 18 Mar 2017. 20:45",
  "arrival_date": "Vi, 19 Mar 2017. 02:15",
  "price": 150   
}

,

{
  "id": 2,
  "departure_station": "Budapest",
  "destination_station": "London",
  "departure_date": "Vin, 28 Mar 2017. 20:45",
  "arrival_date": "Lu, 29 Mar 2017. 02:15",
  "price": 250
}

and

{
  "id": 3,
  "departure_station": "Paris",
  "destination_station": "Berlin",
  "departure_date": "Joi, 18 Mar 2017. 20:45",
  "arrival_date": "Vi, 19 Mar 2017. 02:15",
  "price": 450
}

then on each page you should get in PHP only one different array

In order to get a whole array change your iteration logic to:

  foreach($data as $route) {       
    $html = $this->load->view("front/curse_interne.php", array(
      'route' => $route
    ), true);
    echo json_encode(array("status" => 1, "html" => $html));
  }

and in your view:

<?php
  $data = array('route' => $route);
  print('<pre>' . print_r($data, 1) . '</pre>');
  exit();
?>

this should bring your original array back:

Array
  (
    [route] => Array
      (
        [0] => Array
            (
                [id] => 1
                [departure_station] => Cluj-Napoca, Autogara
                [destination_station] => Bucuresti Nord
                [departure_date] => Joi, 18 Mar 2017. 20:45
                [arrival_date] => Vi, 19 Mar 2017. 02:15
                [price] => 150
            )

        [1] => Array
            (
                [id] => 2
                [departure_station] => Budapest
                [destination_station] => London
                [departure_date] => Vin, 28 Mar 2017. 20:45
                [arrival_date] => Lu, 29 Mar 2017. 02:15
                [price] => 250
            )

        [2] => Array
            (
                [id] => 3
                [departure_station] => Paris
                [destination_station] => Berlin
                [departure_date] => Joi, 18 Mar 2017. 20:45
                [arrival_date] => Vi, 19 Mar 2017. 02:15
                [price] => 450
            )

    )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.