-1
  if (is_array($jsonhome)) {
                foreach ($jsonhome as $query) {
                    foreach ($query['results']['place'] as $places) {


                        if (is_array($places['country'])) {
                            echo "Country:\n";
                            echo "Content: " . $places['country']['content'] . "\n\n";
                        }

                        if (is_array($places['admin1'])) {
                            echo "State:\n";
                            echo "Content: " . $places['admin1']['content'] . "\n\n";
                        }

                        if (is_array($places['admin2'])) {
                            echo "District/City:\n";
                            echo "Content: " . $places['admin2']['content'] . "\n\n";
                        }
                    }
                }
            }

    $location_data['user_current_country'] = $places['country']['content'];

            $this->load->view('header_register', $location_data);
            $this->load->view('body_complete_register', $location_data);
            $this->load->view('footer_register');

I want to transfer the country to input type select in the view codeigniter:

Say i get two countries via the above queries say India and USA i want to pass it to the view

Do i do it via ajax?

Please help i am new to codeigniter

4
  • Is this view code or controller? where is the code to load view? Commented Mar 8, 2014 at 17:31
  • you have to place above code in your view. $places['country']['content'] will return only last record. Commented Mar 8, 2014 at 17:35
  • Do i pass the whole $jsonhome to view? Commented Mar 8, 2014 at 17:40
  • Yes. You have to pass it to view and do above code in view instead of controller. Commented Mar 8, 2014 at 17:42

1 Answer 1

1

So in your controller you want to build an array of options for your <select>. Something like

//Build array of country options
$aData['countryOptions'] = array();
foreach ($jsonhome as $query) {
    foreach ($query['results']['place'] as $places) {
        $aData['countryOptions'][] = $places['country']['content'];
    }
}

$this->load->view('body_complete_register', $aData);

Then in your view you can use the form_dropdown function providing you have the form helper loaded

$this->load->helper('form');
echo form_dropdown('countries', $countryOptions);

Of if you don't want to use the helper you could do

<select name="countries" id="countries">
    <?php foreach($countryOptions as $key => $countryName) { ?>
        <option value="<?php echo $key ?>"><?php echo $countryName ?></option>
    <?php } ?>
</select>

Please note that you shouldn't be echoing anything out in the controller. The controller is just for passing data between models, libraries and views

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.