简体   繁体   中英

How to get all data from database in codeigniter

I want to display the student names from db and two buttons for each students. In my code i am using ajax function.

controller

    function get_sib_filter()
    {
       $list_id= $this->input->post('id2');
       if(($list_id)==1)
       {
         $filtered_students = $this->home_model->filter_by_sibling(); 
         $new_string = "";
         foreach($filtered_students->result() as $detail)
         {
           $new_string.=$detail->applicant_first_name;
           $new_string=$new_string.'<a href="<?echo base_url();?>home/change_filter_status_green/"'.$detail->applicant_id.'" class="btn green button_style" title="Filter">Selected For Interview</a>';
           $new_string=$new_string.'<a href="<?echo base_url();?>home/change_filter_status_red/"'.$detail->applicant_id.'" class="btn red but_style" title="Rejected">Rejected</a></br>';

        }

      echo $new_string;
     }

   }

But i got the last name only(last name and two buttons)

I want list all name and all name having two buttons

Plzz give suggetions..

Like mentioned in the comments above, here are the issues found:

  • you never defined $new_string
  • you are overwriting $new_string
  • you are not concatenating the base_url the right way
  • Also, since you are using base_url you should be loading the URL helper in your controller ( $this->load->helper('url') ) or autoloading it in autoload.php

Try this:

$students = $filtered_students->result();
$string = "";
foreach ($students as $student) {
    $string .= $student->applicant_first_name . " ";;
    $string .= "<a href='" . base_url() . "home/change_filter_status_green/$student->applicant_id' class='btn green button_style' title='Filter'>Selected For Interview</a> | ";
    $string .= "<a href='" . base_url() . "home/change_filter_status_red/$student->applicant_id'' class='btn red but_style' title='Rejected'>Rejected</a><br/>";
}
echo $string;

You are overwriting the string here. Please make some changes as shown below.

$new_string = ""; // Define $new_string..
foreach($filtered_students->result() as $detail){
   $new_string.=$detail->applicant_first_name;
   $new_string.='<a href="'.base_url().'home/change_filter_status_green/"'.$detail->applicant_id.'" class="btn green button_style" title="Filter">Selected For Interview</a>';
   $new_string.='<a href="'.base_url().'home/change_filter_status_red/"'.$detail->applicant_id.'" class="btn red but_style" title="Rejected">Rejected</a><br />';
}

And also make sure what are you getting from this variable: $filtered_students . You can check it by print_r($filtered_students);

Try the following code:

      function get_sib_filter()
{
   $list_id= $this->input->post('id2');
   if($list_id == 1)
   {
     $filtered_students = $this->home_model->filter_by_sibling(); 
     $new_string = "";
     $count = 0;
     foreach($filtered_students->result() as $detail)
     {
        if($count == 0){
            $new_string = "<a href='".base_url('home/change_filter_status_green/').$detail->applicant_id."' class='btn green button_style' title='Filter'>Selected For Interview</a><a href='".base_url('home/change_filter_status_red').$detail->applicant_id."' class='btn red but_style title='Rejected'>Rejected</a>";
            $count++;
        }else{
            $new_string .= $new_string."<a href='".base_url('home/change_filter_status_green/').$detail->applicant_id."' class='btn green button_style' title='Filter'>Selected For Interview</a><a href='".base_url('home/change_filter_status_red').$detail->applicant_id."' class='btn red but_style title='Rejected'>Rejected</a>";
        }

     }

  echo $new_string;
 }

}

change your model like this:

function filter_by_sibling()
   {
        $this->db->select('applicant_id,applicant_first_name');
        $this->db->from('student_application');
        $this->db->order_by('sib_count','desc');
        $result = $this->db->get()->result_array();
        return $result; 
   }

Modify your controller function like this:

function get_sib_filter()
    {
       $list_id= $this->input->post('id2');
       if(($list_id)==1)
       {
         $filtered_students = $this->home_model->filter_by_sibling(); 
         $new_string = "";
         foreach($filtered_students as $detail)
         {
           $new_string .=$detail['applicant_first_name'];
           $new_string .='<a href="'.base_url().'home/change_filter_status_green/'.$detail['applicant_id'].'" class="btn green button_style" title="Filter">Selected For Interview</a>';
           $new_string .='<a href="'.base_url().'home/change_filter_status_red/'.$detail['applicant_id'].'" class="btn red but_style" title="Rejected">Rejected</a><br />';
        }

      echo $new_string;
     }

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM