• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble pass array from DB, model to view

#1
[eluser]Dandy_andy[/eluser]
This should be relatively simple, but I'm stuck. I have a DB with a table that has some country geo data organised in two rows:- country id (con_id) and country name (name). I have created a Model to extract the data from the DB and I want to pass it to the controller and then on to my view so that I can populate a dropdown list. But I'm having trouble passing an array.

Here is my data...

MODEL:-
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Geo_options extends CI_Model {

//call geo country options
function call_country() {
  
  $this->db->select('con_id, name');
  $query_con = $this->db->get('geo_countries');
  
  foreach ($query_con->result_array() as $row)
  {
  $row['con_id'];
  $row['name'];
  }
  return $row;  

}

}

?>

PART OF THE CONTROLLER:-

Code:
<?php

class User extends CI_Controller {

function myprofile()

{
  $this->load->model('geo_options');
  $data['country'] = $this->geo_options->call_country();
                $this->load->view('myprofile', $data);

}//function index

}

?>

MY VIEW (written just to test the output at the moment but what I want to do is populate an HTML dropdown list with the relevant options values and names:-
Code:
<?php echo $country; ?>

I know I have done something very wrong with passing the array and I would appreciate some help on this... I'm learning slowly but surely!

Thanks in advance.

#2
[eluser]PhilTem[/eluser]
Your code - in general - isn't wrong. Though I am wondering what this part should actually do/produce

Code:
foreach ($query_con->result_array() as $row)
{
  $row['con_id'];
  $row['name'];
}
return $row;

You are just looping over each row and basically do nothing but waste time Wink You probably want something like

Code:
function call_country()
{
  $this->db->select('con_id, name');
  $query_con = $this->db->get('geo_countries');
  
  $return = array();
  
  foreach ($query_con->result_array() as $row)
  {
    $return[$row['con_id']] = $row['name'];
  }
  
  return $return;

}

This way you will get an array which you can easily pass to form_dropdown.

#3
[eluser]Dandy_andy[/eluser]
All i want to do is pass all the DB row entries (con_id and name) to my view so I can echo them out initially. I should be able to figure out the rest from there. At the moment, all I am getting 'echoed' is the word 'Array' so I know I'm doing something wrong on the Array side!

#4
[eluser]Dandy_andy[/eluser]
Hours later and I am still stuck with this. What I need to have on the view side is the value of 'con_id' and 'name' for each of the DB rows so that I can create a list. How do I combine an array into an array?

#5
[eluser]chubbypama[/eluser]
i'm no ci expert, but can't you just do something like this in your model?
Code:
$query = $this->db->get('geo_countries');
return $query->result_array();

your controller can stay as is...
$data['country'] should contain an array of all the rows returned...

in the view, you can loop through the records by doing something like:
Code:
<?php foreach ($country as $country_item): ?>
<?php echo $country_item['name'] ?>
<?php endforeach ?>


#6
[eluser]Dandy_andy[/eluser]
That is not the way I would have thought of doing it but it works and it's very simple so thank you very much for the suggestion. I have always struggled with understanding PHP arrays and this is helping. Thanks again.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.