All I am trying to do is query a database table for a list of counties in my state. I want to pull 5 cities that correspond to those counties and list them on the page. I am using a JOIN in order to do this because that is what my searching lead me to believe that I need.
So far I am passing all of the information from my Model to my Controller, and then displaying it in my View with no issues. The problem is that the cities list seems to be limiting its self to only 1 city per county. Do you see a problem with my query? I have tried (seemingly) everything, but because I am so new I am sure that there is something silly that I am missing.
<?
class Index_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
}
public function countiesWithCities() {
$this->db->select("*");
$this->db->from("cities");
$this->db->join("counties", "cities.county_id = counties.county_id");
$query = $this->db->get();
return $query->result();
}
}
This is the result of that query:
array(5) {
[0]=> object(stdClass)#17 (5) {
["city_id"]=> string(1) "0"
["county_id"]=> string(1) "5"
["city_name"]=> string(8) "Munising"
["county_name"]=> string(5) "Alger"
["number_of_cities"]=> string(1) "1"
}
[1]=> object(stdClass)#18 (5) {
["city_id"]=> string(1) "1"
["county_id"]=> string(1) "1"
["city_name"]=> string(7) "Redford"
["county_name"]=> string(5) "Wayne"
["number_of_cities"]=> string(1) "2"
}
[2]=> object(stdClass)#19 (5) {
["city_id"]=> string(1) "2"
["county_id"]=> string(1) "1"
["city_name"]=> string(7) "Livonia"
["county_name"]=> string(5) "Wayne"
["number_of_cities"]=> string(1) "2"
}
[3]=> object(stdClass)#20 (5) {
["city_id"]=> string(1) "3"
["county_id"]=> string(1) "4"
["city_name"]=> string(6) "Monroe"
["county_name"]=> string(6) "Monroe"
["number_of_cities"]=> string(1) "1"
}
[4]=> object(stdClass)#21 (5) {
["city_id"]=> string(1) "4"
["county_id"]=> string(1) "2"
["city_name"]=> string(16) "Farmington Hills"
["county_name"]=> string(7) "Oakland"
["number_of_cities"]=> string(1) "1"
}
}
It seems fine but for example, array[2] should have more cities listed under it, other than "Livonia." I can't seem to figure out how to complete this? Anyone have any advice or help?