我正在尝试按日期排序并显示结果,但更改asc或desc对视图中订单的外观没有任何影响。
型号:
function DisplayLatest()
{
$query = $this->db->query("SELECT * FROM CIUploads INNER JOIN CIUsers ON CIUploads.UserID = CIUsers.UserID");
$this->db->order_by("UploadLikes","asc");
return $query->result();
}
控制器:
function index()
{
$this->load->view('../templates/header.php');
$this->load->model('Category_model');
$data['allwork'] = $this->Category_model->DisplayLatest();
$this->load->view('Latest_view', $data);
$this->load->view('../templates/footer.php');
}
查看:
foreach ($allwork as $value) //joined display//
{
echo "<div class='col-lg-4 display-item'>";
echo "<a href='BLANK'' class='display-link'>";
echo "<div class='caption'>";
echo "<div class='caption-content'>";
echo "<i class='fa fa-eye fa-3x'></i>";
echo "</div>";
echo "</div>";
echo "<img src='".base_url("/application/uploads/")."/".$value->UploadedAt."' class='img-responsive'>";
echo "</a>";
echo "<div class='showcase-information'>";
echo "<p class='caption'>"".$value->UploadTitle.""</p>";
echo "<p class='stats'><i class='fa fa-eye'></i>".$value->UploadViews." <i class='fa fa-heart'></i>".$value->UploadLikes." <i class='fa fa-comments'></i>".$value->UploadComments." </p>";
echo "</div>";
echo "<a href='BLANK'><div class='showcase-user'>";
echo "<p>".$value->Username."<img src=".$value->ProfileImg." class='img-circle'></p>";
echo "</a>";
echo "</div>";
echo "</div>";
}
(我意识到我通过上传来排序,因为它是一个整数,应该可以工作)
答案 0 :(得分:0)
试试这个
function DisplayLatest()
{
$query = $this->db->select("*")->from("CIUploads")->join("CIUsers","CIUploads.UserID = CIUsers.UserID","inner")->order_by("CIUploads.UploadLikes","asc")->get();
return $query->result();
}
答案 1 :(得分:0)
当您使用$this->db->query()
时,这不是编写查询的正确方法。
试试这个,这应该有用
$query = $this->db->query("SELECT * FROM CIUploads INNER
JOIN CIUsers ON CIUploads.UserID = CIUsers.UserID ORDER BY UploadLikes asc");
$this->db->query()
将直接执行查询。它不会检查下一行。
答案 2 :(得分:0)
Codeigniter查询生成器类/活动记录
http://www.codeigniter.com/user_guide/database/query_builder.html
模型功能
public function display_latest() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'CIUploads');
$this->db->join($this->db->dbprefix . 'CIUsers', 'CIUsers.UserID = CIUploads.UserID');
$this->db->order_by("CIUploads.UploadLikes","ASC");
$query = $this->db->get();
if ($query->num_rows() > 0 ) {
return $query->results();
// return $query->results_array
} else {
return FALSE;
}
}
我不认为在函数display_latest()
在codeigniter上调用模型到控制器时你不
文件名示例:Some_name.php
<?php
class Some_name extends CI_Controller {
public function __construct() {
parent::__construct();
// You do not need the upper case letter here
$this->load->model('category_model');
}
function index() {
/*
If view is stored in views folder you do not need to use.php
$this->load->view('templates/header.php');
*/
$this->load->view('templates/header');
$data['allwork'] = $this->category_model->display_latest();
/*
Not sure why you have upper case for view you do not need to on view files
Only Controllers / Models / libraries file names and class names have first letter upper case.
*/
$this->load->view('latest_view', $data);
$this->load->view('templates/footer');
}
}