• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using foreach in codeigniter view to generate select options

#1
[eluser]thefatladysingsopera[/eluser]


I ma using codeigniter to generate some html options,but i only get one result,first result from the table.

This is my controller

Code:
public function edit_event($id = 0){
    $id = $this->uri->segment(3);
    $current_id = $this->ion_auth->get_user_id();
    $data['data'] = $this->as->the_event_edit($id);
    $data['groups'] = $this->as->the_groups_list($current_id);
    $this->load->view('editevent',$data);
    }

This is my model

Code:
public function the_groups_list($current_id){
    $query = $this->db->get_where('all_groups', array('group_owner' => $current_id));
    foreach ($query->result() as $row)
    {
    $data = array(
               'group_title' => $row->group_title,
               'group_name' => $row->group_name,
               'group_owner' => $row->group_owner
          );  
          return $data;
    }
    }

This is the other model

Code:
public function as_event_edit($id){
    $query = $this->db->get_where('all_scheduled_messages', array('id' => $id));
    foreach ($query->result() as $row)
    {
    $data = array(
               'as_title' => $row->as_title,
               'as_event_name' => $row->as_event_name,
               'as_owner' => $row->as_owner,
               'as_type' => $row->as_type,
               'as_target_dataset' => $row->as_target_dataset,
               'as_timestamp' => $row->as_timestamp,
               'as_time' => $row->as_time,
               'as_day' => $row->as_day
          );  
          return $data;
    }
    }

I am then using $groups['group_title'] in view and only the first group title gets displayed even though i have like 4 group titles from the other rows.

How can i return and pass an array that i can then to the view so as to use foreach to iterate and display the group titles?.

#2
[eluser]Tim Brownlaw[/eluser]
Just so you can see what's happening, try this...


Code:
public function the_groups_list($current_id) {
  $query = $this->db->get_where('all_groups', array('group_owner' => $current_id));
  if( $query !== FALSE AND $query->num_rows() > 0)
  {
    $rows = $query->result_array();
    var_dump($rows); // This is a temporary Debug you can put here or after your caller to this function to see the output. Remove it after you've seen the output.
    return $rows;
   }
   else
   {
      return FALSE; // Nothing was found
   }
}

Note: This code is not tested but "should" work!

What you should get is an Array of the arrays of the results which you can then pull apart in your view.
Just keep in mind that the return of this will either be an array or the boolean FALSE ( for the case there are no results ) So you should test for it...

Or you could simply return an empty array and perform a if(count($result_array)>0)... That just keeps the return type the same instead of being mixed types. That's up to you to decide! But you should always have some test to deal with the case where nothing comes back and deal with it.

#3
[eluser]InsiteFX[/eluser]
If there is only one group owner then you will only get back one record, if there is multiple group owner then you are over writing the $data in the foreach loop every time because you are defining it as an array in the foreach.

Use var_debug() to see what your getting back.

Code:
public function get_comments($id)
{
  $data  = array();
  $options = array('id' => $id);

  $this->db->order_by('pub_date', 'desc');

  $query = $this->db->get_where('comments', $options, 1);

  if ($query->num_rows() > 0)
  {
   $data = $query->result_array();
  }

  $query->free_result();    

  return $data;    
}

Or as Tim stated above which will give you an associated array.

#4
[eluser]thefatladysingsopera[/eluser]
I now have this model


Code:
public function das_groups_list($current_id){
$query = $this->db->query("select group_title,group_name from all_groups");
foreach ($query->result() as $row)
{
   $data[] = array(
               'group_title' => $row->group_title,
               'group_name' => $row->group_name
          );
}
    return $data;
}

and this is the controller

Code:
public function edit_event($id = 0,$current_id=0){
    $id = $this->uri->segment(3);
$current_id = $this->ion_auth->get_user_id();
    $data['event'] = $this->asgard->das_event_edit($id);
$data['groups'] = $this->asgard->das_groups_list($current_id);
$this->load->view('editevent',$data);
}

I get this array

Code:
Array
(
    [0] => Array
        (
            [group_title] => le_glutten borg
            [group_name] => legluttenborg
        )

    [1] => Array
        (
            [group_title] => le grand gandalf
            [group_name] => legrandgandalf
        )

    [2] => Array
        (
            [group_title] => rhyyuj
            [group_name] => rhyyuj
        )

)

when i
Code:
print_r ($groups)
in my view however
Code:
$groups['group_title']
complains of an illegal offset.

Update:

This
Code:
echo $groups[0]['group_title'];
gets me only the first group.How should i list all groups?.

I have this

Code:
<select class="form-control"><option value="">&lt;?php for($i = 0; $i > count($groups); $i++){echo $groups[$i]['group_title'];} ?&gt;</option></select>

which does not display any value.

#5
[eluser]CroNiX[/eluser]
Well, of course, because it's an array of associative arrays. You'd have to loop over it to access the 'group_title' key of each subarray.

I guess the first question is how do you want your dropdown to look? Show the actual
Code:
<option value="what_goes_here">What Text Goes Here</option>
And what fields those are coming from the database.

The CI dropdown helper needs the array in this format:
Code:
array(
  'option_value' => 'option_text',
  'option_value' => 'option_text',
  'option_value' => 'option_text'
)

The 'option_value' is what gets put as the what_goes_here (value) of the dropdown as shown above in my <option> example. The 'option_text' is what becomes the 'What Text Goes Here'.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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