我是代码点火器的新手
尝试创建排行榜时出现此错误
遇到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>
答案 0 :(得分:0)
为什么不像那样使用它
$query = $this->db->query($sql);
答案 1 :(得分:0)
我对你的问题有一些疑问。您在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-&gt; db。
我建议浏览Correct naming structure for 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;
}
}
...