简体   繁体   English

根据Joomla中的当前用户ID显示动态内容

[英]Display dynamic content based on current user ID in Joomla

I am developing a custom module that displays a dynamic list of articles based on whether the current user has read an article or not. 我正在开发一个自定义模块,该模块根据当前用户是否阅读文章来显示文章的动态列表。 When a user reads or 'submits' an article, the article id and title is stored in a table called "completed_quests." 当用户阅读或“提交”文章时,文章ID和标题将存储在名为“ completed_quests”的表中。 The dynamic list will only display articles that are NOT found in the completed_quests table. 动态列表将仅显示在completed_quests表中找不到的文章。 The code below is completely functional, however I am running into a bit of an issue. 下面的代码可以完全正常工作,但是我遇到了一个问题。

Currently, the list is being displayed in the same way regardless of which user is logged in. For instance, let's say that user 540 reads and submits an article. 当前,无论哪个用户登录,列表都以相同的方式显示。例如,假设用户540阅读并提交了文章。 When user 541 logs in - it is displaying the list as if user 541 has submitted the quest as well. 当用户541登录时-它显示列表,就像用户541也已提交任务一样。 I may be over complicating this, but I need to get the list to display in a unique way depending on which user is logged in. Please see the code below. 我可能会为此复杂化,但是我需要使列表以唯一的方式显示,具体取决于登录的用户。请参见下面的代码。 I have already pulled the user ID from the database and defined the $userID variable. 我已经从数据库中提取了用户ID并定义了$ userID变量。 Now I just need to know where it fits in to the equation. 现在,我只需要知道它适合方程式的位置。

Edit: Below is the revised, working code. 编辑:下面是修改后的工作代码。

<?php

$catID = JRequest::getVar('id');
$user = JFactory::getUser();
$userID = (int)$user->id;

$query = "SELECT * FROM arp2i_completed_quests AS r
RIGHT JOIN arp2i_content AS c
ON c.id=r.id 
AND c.catid=r.catid 
AND r.user_id = $userID
WHERE r.user_id IS NULL "; // prepare query

$db = &JFactory::getDBO(); // get database object
$db->setQuery($query); // apply query
$articles = $db->loadObjectList(); // execute query, return result list

foreach($articles as $article){ // loop through articles

if ($article->id >1 && $article->catid == $catID) {
       echo '<a href="http://localhost/quest/index.php/quests/' . $article->id . '-' . $article->title . '">' . 'ID:' . $article->id . ' Title: ' . $article->title . '</a>' . '<br />';}
} 

?>

According to your answer is the id field in the arp2i_completed_quests the article id. 根据您的回答是arp2i_completed_quests中文章ID的ID字段。 Then you have to swap the join and make a condition with the user id. 然后,您必须交换联接并使用用户ID进行条件设置。

<?php
$catID = JRequest::getVar('id');
$user = JFactory::getUser();
$userID = (int)$user->id;

$query = "SELECT * FROM arp2i_content as c ";
$query .= "left JOIN arp2i_completed_quests as r on ";
$query .= "c.id=r.id AND c.catid=r.catid and r.user_id != ".$userID." ";
$query .= "where r.id is null"; // prepare query

$db = JFactory::getDBO(); // get database object
$db->setQuery($query); // apply query
$articles = $db->loadObjectList(); // execute query, return result list

foreach ($articles as $article) { // loop through articles
    if ($article->id > 1 and $article->catid == $catID) {
        echo '<a href="http://localhost/quest/index.php/quests/' . $article->id . '-' . $article->title . '">' . 'ID:' .
             $article->id . ' Title: ' . $article->title . '</a>' . '<br />';
    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM