duangu1645 2017-04-06 15:01
浏览 11
已采纳

提供的参数无效Codeigniter

My program is not working properly, i do not know what should i do :S

I got this error message: enter image description here

Take a look at this: enter image description here

Here is my code: My controller file (Home):

    <?php

    class Home extends CI_Controller{

         public function __construct(){
             parent::__construct();

             $this->load->model("Crudmodel");

        }


 public function index(){

    # get all data in Study table
    $selectStudys = $this->Crudmodel->selectStudys();


    foreach ($selectStudys as $key => $study) 
    {
        # get UserNames
        $user = $this->Crudmodel->getName($study['user_id']);

        #get Subject Names
        $subject = $this->Crudmodel->getSubName($study['subject_id']);


        #append both NEW VALUES to same array

        if(!empty($user[0]['username'])){
        $data[$key]['user_id'] = $user[0]['username'];
        // your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
        }else{
         $data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler 
        }
        if(!empty($subject[0]['name'])){
        $data[$key]['subject_id'] = $subject[0]['name'];
        // your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
        }else{
           $data[$key]["subject_id"] = "";
          // or anything you can use as error handler
        }

    }


    $data['records'] = $selectStudys;
    $this->load->view('home', $data);

}

}
?>

Crudmodel:

       class Crudmodel extends CI_Model{

        public function __construct(){
         parent::__construct();

         $this->load->database();

        }


        function selectStudys()
{
    $query= $this->db->query("SELECT * FROM cursadas");
    if($query->num_rows()>0){
       $result = $query->result_array();
     }else{
      $result = "";
          // or anything you can use as error handler
      return $result;
    }
}

function getName($name)
{
    $query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
    if($query->num_rows()>0){
    $result = $query->result_array();
    }else{
    $result = "";
          // or anything you can use as error handler
    return $result;
  }
}

Dont know what to do now :(

Hope you can help me :S

  • 写回答

1条回答 默认 最新

  • duanfan5012 2017-04-06 17:47
    关注

    The problem is in the model. You only return something inside the else. Easy fix, move the return.

    You should probably return an empty array if there are no rows. Then the foreach will still have something to work with - even if it is empty. foreach will choke if given something that cannot be used in a loop - a string for instance.

    function selectStudys()
    {
        $query= $this->db->query("SELECT * FROM cursadas");
        if($query->num_rows()>0){
           $result = $query->result_array();
         }else{
          $result = array();
        }
        return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目