如何从codeigniter中获取数据库中的所有数据

时间:2015-10-01 14:19:46

标签: php ajax codeigniter

我想显示db中的学生姓名和每个学生的两个按钮。 在我的代码中,我使用的是ajax函数。

控制器

    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;
     }

   }

但我只得到了姓氏(姓氏和两个按钮)

我希望列出所有名称和所有名称都有两个按钮

Plzz给出了建议......

4 个答案:

答案 0 :(得分:1)

如上面评论中提到的,以下是发现的问题:

  • 您从未定义$new_string
  • 您正在覆盖$new_string
  • 您没有以base_url正确的方式连接
  • 此外,由于您使用的是base_url,因此您应该在控制器中加载网址助手($this->load->helper('url'))或在autoload.php中自动加载

试试这个:

$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;

答案 1 :(得分:0)

你在这里覆盖了字符串。请进行如下所示的更改。

$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 />';
}

还要确保从这个变量得到什么:$filtered_students。 您可以按print_r($filtered_students);

进行检查

答案 2 :(得分:-1)

请尝试以下代码:

      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;
 }

}

答案 3 :(得分:-2)

像这样改变你的模型:

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; 
   }

修改你的控制器功能:

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;
     }

   }