Reputation: 47
Being a self-confessed newbie I'm looking for some advice and guidance :)
I have a table in my database called Dams which is simply a list of dams throughout the world. One of the fields within that table is called Country; the country that the named dam is in.
I want to put a search form on my site allowing someone to search through the dams, and for the Country field I want to present it as a dropdown list, listing all the countries that occur in that field, in alphabetical order with a number after each country's name with the number of times that country occurs in the field. For example, there are 15 dams in Brazil so I want the dropdown entry for Brazil to read 'Brazil (15 dams)'.
Things are further complicated because the dam countries saved in the database are just the country codes (US, GB, etc.) not the actual country names (United States, United Kingdom etc.) which is what I want use.
I've created an array of country codes/names like so:
$countries = array(
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
...
"ZW" => "Zimbabwe"
);
And I've created the query below to retrieve the country field contents from the table, count how many of each country occurs, and group the results:
$result = $this->db->query("select country,count(*) as count from default_dams group by country")->result_array();
Now I've got to the point where I don't know how to proceed.
I've experimented using a foreach loop like so:
foreach ($query as $row)
{
$dropdown[$row['country']] = $row['count'];
}
return $dropdown;
and it's giving...
<option value="AL">3</option>
<option value="CN">94</option>
<option value="ZW">1</option>
...so I feel I'm getting somewhere, but I don't know how to bring in my $countries array in order to provide output like:
<option value="AL">Albania (3 dams)</option>
<option value="CN">China (94 dams)</option>
<option value="ZW">Zimbabwe (1 dam)</option>
I'm guessing I need a foreach loop to go through $result and form an array of results to pass from my model to the view, but how to build that array?
By the way, the code snippet from my view file that displays the dropdown is:
$first_field = '><option value="all">Any Country</option';
echo form_dropdown('dam-country', $dropdown, 'all', $first_field);
All help and advice is gratefully received. :)
Tony.
Upvotes: 0
Views: 455
Reputation: 1800
Uhh, i'm not sure if following code be correct since i didn't get some parts of your descriptions, but i'd rewrite foreach like so:
foreach ($query as $row)
{
$option = $countries[$row['country']] . '(' . $row['count'];
// check dams number if one then word "dam" is singular, otherwise plural
if ($row['count'] == 1) {
$option .= ' dam)';
} else {
$option .= ' dams)';
}
$dropdown[$row['country']] = $option;
}
return $dropdown;
I myself not a fan of html code generation functions, i'd just write everything myseld in the view (example based on some assumptions):
// rows - data from the DB sent to View
echo '<select name="countries">';
echo '<option value="all">Any Country</option>';
foreach ($rows as $row)
{
echo '<option value="' . $row['country'] . '">';
$option = $countries[$row['country']] . '(' . $row['count'];
// there must be a dedicated function to deal with singular/plural words in CI
if ($row['count'] == 1) {
$option .= ' dam)';
} else {
$option .= ' dams)';
}
echo $option;
echo '</option>';
$dropdown[$row['country']] = $option;
}
echo '</select>';
Upvotes: 1