0

I am trying to parse data from from API to my view on my website. When I put the script directly to the index , just for test , it return:

 array(2) 
 { 
    ["status"]=> int(1) 
    ["categories"]=> array(2) 
        { 
            [0]=> array(2) 
                { 
                    ["id"]=> string(1) "5" 
                    ["name"]=> string(19) "Pensionar / Veteran" 
                } 
                [1]=> array(2) 
                { 
                    ["id"]=> string(1) "6" 
                    ["name"]=> string(14) "Alta categorie" 
                } 
        } 
} 

This is how my model looks like :

public function getPassengerCategories()
{
    try {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://api_link");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $parsed_json = curl_exec($ch);
        $parsed_json = json_decode($parsed_json, true);

        $data = $parsed_json;

        return $data;
    }
    catch (Exception $e) {
        return $e->getMessage();
    }
}

And my controller looks just like this:

function getPassengerCategories()
{
    $this->load->model('Curse_Interne_Model');
    $data = $this->Curse_Interne_Model->getPassengerCategories();

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

    }
}

I try first to check if this array is parsed to the view, and that's how my view looks like:

<?php
    $data = array('categories' => $category);
    print('<pre>' . print_r($data) . '</pre>');

?>

The problem is that all it returns to the view is the following result:

Array ( [categories] => )

1

Any idea how to resolve this? Thank you !

1 Answer 1

1

Your iteration logic over $data is wrong. Here's your data:

$data = [
  "status"     => 1,
  "categories" => [
       ["id" => 5, "name" => "Pensionar / Veteran"],
       ["id" => 6, "name" => "Alta categorie"],
  ]
]

Now if you want to iterate over categories and pass each one to a view:

foreach ($data['categories'] as $category) {
    $html = $this->load->view('front/search_box', ['categories' => $category], true);

    echo json_encode(["status" => 1, "html" => $html]);
}

The overall logic seems wrong to me; you don't echo out a JSON response in a foreach loop. Anyway, whatever your logic is, you need to pay more attention to your data structure format.

Also, read these resources to improve your code:

  • Use CodeIgniter's Output class to generate JSON responses with proper headers
  • $this->load->view() does not require .php extension
  • Utilize Requests for PHP, instead of vanilla curl_* calls
  • Use the more readable dd() instead of print_r()
  • CodeIgniter model classes are mostly for database interactions, not service calls. Instead use a service class.
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.