遇到PHP错误 - Code Igniter

时间:2018-05-24 04:14:34

标签: php codeigniter

我正在尝试将数据从我的数据库提取到数据表,表格标题为“学生总数|男性|女性”

这是它显示的错误

  

严重性:注意

     

消息:未定义属性:stdClass :: $ totalcount

     

文件名:pages / studentgender.php

     

行号:20

     

回溯:

     

文件:C:\ xampp \ htdocs \ libsystem \ application \ views \ pages \ studentgender.php       行:20       功能:_error_handler

     

文件:C:\ xampp \ htdocs \ libsystem \ application \ controllers \ Students.php       行:30       功能:查看

这是我的模特

public function studentCountGender(){
    $this->db->select('studgender, COUNT(*) as totalcount, COUNT(   case when studgender = "Male" then 1 else 0 end)
                        AS malecount, COUNT(    case when studgender = "Female" then 1 else 0 end) AS femalecount');
    $this->db->from('student');
    $query = $this->db->get();
    return $query->result();
}
}

这是我的控制器

 public function studentList(){

    $this->load->model('student_model');
    $data['result'] = $this->student_model->studentCountGender();
    $this->load->view('templates/header');
    $this->load->view('templates/sidebar');
    $this->load->view('pages/studentgender', $data);
}

这是我的观点

    <thead>
        <tr>
          <th>Total No. of Students</th>
            <th>Total Number of Males</th>
            <th>Total Number of Females</th>
        </tr>
    </thead>
    <tbody> 
    <?php
    foreach($sample as $item){
      echo '<tr>
        <td>'.$item->totalcount.'</td>
        <td>'.$item->malecount.'</td>
        <td>'.$item->femalecount.'</td>
      </tr>
    </tbody>
    ';}?>
</table>
            </div>
        </div>
    </div>
</div>

有人可以帮我解析它并且不会显示错误吗?我一直试图调试这个2个小时,它让我疯了非常感谢!!!

5 个答案:

答案 0 :(得分:3)

请尝试以下代码希望这对您有所帮助。如果您没有得到结果或在错误的视图中传递$数据,请相应地进行检查。

<?php 
    if(isset($result)&& !empty($result)){
            foreach($result as $item){
              echo "<tr>
                 <td>".$item->totalcount."</td>
                  <td>".$item->malecount;"</td>
                 <td>".$item->femalecount."</td>
                </tr>";
            }
    }
        ?>

答案 1 :(得分:2)

尝试使用此代码一次。

<?php
    foreach($result as $item){
      echo "<tr>
         <td>{$item->totalcount}</td>
          <td>{$item->malecount}</td>
         <td>{$item->femalecount}</td>
        </tr>";
    }
?>

答案 2 :(得分:0)

在您的视图中使用$ result而不是$ sample希望这将起作用

答案 3 :(得分:0)

您正在将$data['result']传递给视图,那么如何在$sample中将$item的结果作为foreach传递给import requests import json BING_URI_BASE = "http://www.bing.com" BING_WALLPAPER_PATH = "/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US" # open the Bing HPImageArchive URI and ask for a JSON response resp = requests.get(BING_URI_BASE + BING_WALLPAPER_PATH) if resp.status_code == 200: json_response = json.loads(resp.content) wallpaper_path = json_response['images'][0]['url'] filename = wallpaper_path.split('/')[-1] wallpaper_uri = BING_URI_BASE + wallpaper_path # open the actual wallpaper uri, and write the response as an image on the filesystem response = requests.get(wallpaper_uri) if resp.status_code == 200: with open(filename, 'wb') as f: f.write(response.content) else: raise ValueError("[ERROR] non-200 response from Bing server for '{}'".format(wallpaper_uri)) else: raise ValueError("[ERROR] non-200 response from Bing server for '{}'".format(BING_URI_BASE + BING_WALLPAPER_PATH)) 。请再次检查。

答案 4 :(得分:0)

试试这个,对你有用。

你的控制器:

public function __construct()
{
  parent::__construct();
  $this->load->model('student_model');
}

public function studentList(){
    $data = [];
    $data['result'] = $this->student_model->studentCountGender();
    $this->load->view('templates/header');
    $this->load->view('templates/sidebar');
    $this->load->view('pages/studentgender', $data);
}

你的模特:

<?
public function studentCountGender(){
    $this->db->select('studgender, COUNT(*) as totalcount, COUNT(   case when studgender = "Male" then 1 else 0 end)
                        AS malecount, COUNT(    case when studgender = "Female" then 1 else 0 end) AS femalecount');
    $this->db->from('student');
    $query = $this->db->get();
    return $query->result();
}

您的观点:

foreach($result as $item){
      echo '<tr>
        <td>'.$item->totalcount.'</td>
        <td>'.$item->malecount.'</td>
        <td>'.$item->femalecount.'</td>
      </tr>
    </tbody>
    }

问候!