php - codeigniter 中 undefined variable 查询

标签 php mysql database codeigniter

我是代码点火器新手

尝试创建排行榜时出现此错误

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: models/status_model.php

Line Number: 19

我的代码看起来像这样

<?php
class status_model extends CI_Model
{
function get_leaderboard(){
    //prepare a leaderboard
    $data = array();

    $details = array(
        'rank' => NULL,
        'fb_name' => NULL,
        'level' => NULL,
        'college' => NULL
    );

    $rank = 1;
    
    $sql = "SELECT fb_name, level, college, role FROM users ORDER BY level DESC, passtime ASC"; 
    $query = $this->db->query($sql,$start*50);
    if ($query->num_rows() > 0)
    
        foreach ($query->result() as $row)
        {
            //Only regular users are shown in the leaderboard
            //banned users and admins have a rank 0, and are excluded.
            if($row->role == 1){

                $details['rank'] = $rank;
                $details['fb_name'] = $row->fb_name;
                $details['level'] = $row->level;
                $details['college'] = $row->college;
                array_push($data, $details);

                $rank++;
            }
        }
        return $data;
    } else {
        //couldn't find any rows!?
        return false;
    }
    
}

function get_rank($fb_uid=''){
    //calculate rank of the current user, or given fb_uid

    if($fb_uid == ''){
        $fb_uid = $this->session->userdata('facebook_uid');
    }

    if($fb_uid=='' || $fb_uid==NULL){
        return 0;
    }

    //make sure the uid corresponds to a regular user
    $sql = "SELECT fb_uid, role FROM users WHERE fb_uid = $fb_uid"; 
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0)
    {
        $row = $query->row();
        $role = $row->role;
    }else{
        return 0;
    }

    if($role!=1){
        //Rank is 0 for anyone other than a regular user.
        return 0;
    }

    //count from 0 to the current user's position
    $rank = 0;

    $sql = "SELECT fb_uid FROM users WHERE role=1 ORDER BY level DESC, passtime ASC"; 
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $rank++;
            if($row->fb_uid == $fb_uid){
                return $rank;
            }
        }
    }

    return 0;

}

function get_winners(){
    //For future use, if winner's details are 
    //added to database

    //return an empty array for now.
    $data = array();
    return $data;
}
}

我的 HTML 看起来像这样

    <?php
    $list='';

    foreach($leaderboard as $row){
            $list.="<tr>";
            $list.="<td>".$row['rank']."</td>";
            $list.="<td>".$row['fb_name']."</td>";
            $list.="<td>".$row['level']."</td>";
            $list.="<td>".$row['college']."</td>";
            $list.="</tr>";
        }

    }

    ?>
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th>Rank #</th>
                <th>Username</th>
                <th>Level</th>
                <th>College</th>
            </tr>
        </thead>
        <tbody>
            <?=$list?>
        </tbody>
    </table>
</div>

最佳答案

关于您的问题,我有一些疑问。您在 foreach 中使用 if 而不是查询中的 WHERE 子句。您不使用 db 类中内置的 CodeIgniters 的任何原因?您可以使用 $this-> 而不是像 $sql = "SELECT fb_name, level, College, role FROM users WHERErank=$rank ORDER BY level DESC, passtime ASC"; 这样的查询db->select('fb_name'...)->where()->order_by()->get();

一些用户不喜欢链接它们,而是在不同的步骤中执行 $this->db。

我建议浏览Correct naming structure for CodeIgnitor这个 url 可以查看如何在 CodeIgniter 中完成命名

此外,这应该可行,使其更加高效和干净,它只获取排名为 1 的查询。然后从中获取结果,如果有超过 0 个结果,则生成结果。

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’); 
class status_model extends CI_Model {

    function get_leaderboard(){

        $sql = "SELECT fb_name, level, college, role FROM users WHERE rank=1 ORDER BY level DESC, passtime ASC"; 

        $query = $this->db->query($sql);

        if ($query->num_rows() > 0)
            $data = $query->result();
            return $data;
        } else {

            //couldn't find any rows!?
            return false;
        }
    }
...

关于php - codeigniter 中 undefined variable 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21363181/

相关文章:

javascript - MYSQL 表中未定义的结果

php - mysql更新倍数为什么不起作用?哦还有 php

database - Redis 高效创建键

php - 为什么我看到数据库中的多行

php - 为多个表编写 INSERT 查询

javascript - HTML 文本输入条件提交

database - 从 mat 文件中读取数据而不是从数据库中读取数据的好处

php - 像这样在 codeigniter 中使用 Curl

mysql - 这是将 MySQL 表上的字符集更改为 UTF-8 的安全方法吗

sql - 将表中的一个字段复制到同一个表中的另一个字段