【发布时间】:2022-12-03 09:08:32
【问题描述】:
我正在显示列表
Date
News Heading
Short Descrption
该列表分布在大约 100 页,每页有 20 条新闻
问题是:这在 joomla 3.10 中的 php 7.3、7.4 中工作得很好,点击 url - 列表显示在多个页面上,以日期排序为第一标准,最新发布日期即将到来
但是当在 php 8.0.x 上使用相同时 - 它显示不正确,在单击 URL 时 - 首先显示页码为 100 的列表的最后一页。现在,当我在 url 中添加 limitstart=0 时,它会正确显示为第一页。
现在,当我从降序变为升序时——它把内容带到最后一页并开始,但页码又是 100
似乎打开时的 URL 直接转到发布的新闻项的最后一页(尽管其中没有提到 limitstart),这是不正确的,因为理想情况下应该按降序打开并打开最新的页面
下面是 views/list/tmpl/default.php 的代码
if(count($this->items) >0){
//$i=1;
foreach($this->items as $newslist)
{
$date = JFactory::getDate($newslist->n_date);
$list .='<h3><strong>'.$newslist->v_heading.'</strong></h3>
<p>'. $date->format('F j, Y').'</p>
<p>'.substr($newslist->v_short_description,0,100).'</p>
<p><i>Know More on:- </i><a href="index.php?option=com_news&view=detail&v_id='.$newslist->id.'&Itemid='.$Itemid.'"><b><i>'.$newslist->v_heading.'</i></b></a></p><hr/><br>';
//$i=$i+1;
}
}else{
JError::raiseError(404, "Message");
}
<?php echo $list?>
对于 models/list.php,这是函数
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query
->select(
$this->getState(
'list.select', 'DISTINCT a.*'
)
);
$query->from('`#__news` AS a');
if (!JFactory::getUser()->authorise('core.edit', 'com_news'))
{
$query->where('a.state = 1');
}
// Filter by search in title
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_heading LIKE ' . $search . ' )');
}
}
/*
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
if ($orderCol && $orderDirn)
{
$query->order($db->escape($orderCol . ' ' . $orderDirn));
}
*/ //Order by date
$query->order ('a.n_date DESC');
$query->order ('a.id DESC');
return $query;
}
这是 views/list/view.html.php 的代码
public function display($tpl = null)
{
$app = JFactory::getApplication();
$this->state = $this->get('State');
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->params = $app->getParams('com_news');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
throw new Exception(implode("\n", $errors));
}
$this->_prepareDocument();
parent::display($tpl);
}
不确定如何实现以及为什么它在 php 8.0 中不起作用,其中 url 应该打开第一页而不是最后一页
【问题讨论】:
-
ORDER BY必须在 SQL 查询中,而不是 PHP。 -
哦,所以这意味着在上面的代码中这样做是不正确的,我宁愿看看它有 Mysql 函数。
-
是的,就像你在标签中说的:
sql-order-by。 -
您还可以使用 PHP
usort()函数对$this->items进行排序 -
尝试了很长时间,但不幸的是,我的坏新手无法实现如何让它工作。启动赏金 - 如果可以帮助可行。
标签: php sql-order-by echo