CodeIgniter function for returning special dropdown
I want to crea开发者_StackOverflowte a function in my model that does something a bit different and need a bit of help.
I have a database table with names, description, ids etc etc .... there is only one of each name, but i want to return 3 entries for every 1 name, prefixed with a number on it.
So, for example, if i have 2 entries in my database with the names 'joe' and 'bob' .... i want my dropdown box to look like the following
<select>
<option value="joe1">joe1 - description</value>
<option value="joe2">joe2 - description</value>
<option value="joe3">joe3 - description</value>
<option value="bob1">bob1 - description</value>
<option value="bob2">bob2 - description</value>
<option value="bob3">bob3 - description</value>
and so on for additional entries in the DB.
i understand how to do a normal dropdown box, but not one like this.
Any help would be grand :)
Try something like this:
function get_users()
{
$this->db->select("id,name,desc");
$this->db->from("table_name");
$q = $this->db->get();
$rows = $q->row_array();
}
In controller method:
$data = array();
$data['users'] = $this->model_name->get_users();
$this->load->view('view_name',$data);
In view:
<?php
echo "<SELECT>";
foreach($users as $user)
{
for ($i=1; $i<=3; $i++)
{
$id = $user['id'].$i;
echo '<option value="'.$id.'">'.$id.' - '.$user['desc'].'</option>';
}
}
echo "</SELECT>";
?>
精彩评论