【问题标题】:Display pho echo list in Descending order按降序显示照片回显列表
【发布时间】: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-&gt;items 进行排序
  • 尝试了很长时间,但不幸的是,我的坏新手无法实现如何让它工作。启动赏金 - 如果可以帮助可行。

标签: php sql-order-by echo


【解决方案1】:

要按降序显示列表,您可以修改用于从数据库中检索新闻项的 SQL 查询。查询中的 ORDER BY 子句可用于指定项目的排序标准。在您的情况下,您可以将以下行添加到 models/list.php 文件中的 getListQuery() 函数:

$query->order('a.n_date DESC');

这将导致项目按 n_date 字段按降序排序(即,最近的项目将首先出现在列表中)。您还可以添加额外的 ORDER BY 子句以进一步细化项目的顺序,例如如果存在具有相同 n_date 值的项目,则按 id 字段排序。

$query->order('a.n_date DESC, a.id DESC');

通过此更改,列表应在加载页面时以正确的顺序显示。请注意,此解决方案假定 #__news 表中的 n_date 字段包含可用于订购商品的有效日期和时间值。如果不是这种情况,您可能需要使用不同的字段或方法来确定列表中项目的顺序。

【讨论】:

    【解决方案2】:

    看起来您正在尝试在 Joomla 中显示按日期降序排列的新闻项目列表。您发布的代码在按日期降序排列新闻项目方面似乎是正确的。但是,问题似乎出在新闻项目的分页上。

    在您发布的代码中, $this->items 变量似乎包含数据库中的所有新闻项目。这意味着当代码循环遍历项目以将它们显示在页面上时,它会同时显示所有新闻项目。这就是为什么当您访问该页面时首先看到新闻条目的最后一页。

    要解决此问题,您需要在代码中添加分页。在 Joomla 中,这可以通过使用 JPagination 类来完成。以下是如何在代码中实现分页的示例:

    1. 在您的 models/list.php 文件中,将以下代码添加到 return $query 行之后的 getListQuery 函数:

      // Add pagination

      $limit = $this->getState('list.limit'); $limitstart = $this->getState('list.start');

      $this->setState('list.total', $query->count_all());

      $query->setLimit($limit, $limitstart);

    2. 在您的 views/list/tmpl/default.php 文件中,将以下代码添加到 显示分页链接:

      $pagination = $this->get('分页'); echo $pagination->getPagesLinks();

      此代码将向您的新闻项目列表添加分页,允许您在多个页面中显示新闻项目,这些页面按日期降序排列。您可能需要调整代码以满足您的特定需求,但这应该为您提供了一个良好的起点。

      我希望这有帮助!如果您有任何其他问题,请告诉我。

      我的捐赠地址:BTC:178vgzZkLNV9NPxZiQqabq5crzBSgQWmvs,ETH:0x99753577c4ae89e7043addf7abbbdf7258a74697

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多