未在Codeigniter中进行变量查询 [英] Undefined Variable query in codeigniter
问题描述
我是代码点火器的新手
尝试创建排行榜时遇到此错误
I get this error when try to create a leader board
A PHP遇到错误
A PHP Error was encountered
严重性:通知
消息:未定义变量:查询
Message: Undefined variable: query
文件名:models / status_model .php
Filename: 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看起来像这样
And My HTML looks like this
<?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?代替 $ 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();
I have a some questions regarding your question. You use an if inside your foreach instead of a WHERE
clause in your query. Any reason you aren't using CodeIgniters built in db class? Instead of a query like $sql = "SELECT fb_name, level, college, role FROM users WHERE rank=$rank ORDER BY level DESC, passtime ASC";
you can use $this->db->select('fb_name'...)->where()->order_by()->get();
某些用户更喜欢不链接它们,而是以不同的步骤执行$ this-> db。
Some users prefer to not chain them, instead doing $this->db in different steps.
我建议浏览为CodeIgnitor正确命名结构此网址,以了解应如何在CodeIgniter中进行命名
I recommend browsing Correct naming structure for CodeIgnitor this url to see how naming should be done in CodeIgniter
此外,这应该可以工作,使效率和清洁度都更高,它只会获取排名为1的查询。然后,如果查询结果超过0个结果然后生成结果。
Also, this should work, making it both more effiecient and cleaner, it only gets queries where the rank is 1. Then gets the results from them, if there are more than 0 results then generating the results.
<?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;
}
}
...
这篇关于未在Codeigniter中进行变量查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!