【发布时间】:2014-01-26 12:13:31
【问题描述】:
我是代码点火器的新手
我在尝试创建排行榜时收到此错误
遇到了 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>
【问题讨论】:
-
你能直接显示你的问题行吗?为什么是整个代码?
-
什么是行号:19 ??
-
变量
$start未定义。 @echo_Me 跟踪错误通常更容易,因为在这种情况下,如果您粘贴更多内容,那么他得到错误的那一行实际上没有错误。 -
@echo_Me 我得到#start undefined
-
@Emz 我该怎么做?或者我现在应该怎么做我很新
标签: php mysql database codeigniter