繁体   English   中英

未在Codeigniter中查询变量

[英]Undefined Variable query in codeigniter

我是代码点火器的新手

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

遇到PHP错误

严重程度:注意

消息:未定义的变量:查询

文件名:models / status_model.php

行号: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>

你为什么不那样使用它

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

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

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

我建议浏览此URL的CodeIgnitor的正确命名结构,以了解应如何在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;
        }
    }
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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