1

I have a dynamic article in Joomla.

The article information is extracted from fabrik when the user is logged in.

But when the user is not logged.. I can not see the information.

The idea is to create a url .. something similar to this:

https://qrmenudigital.com/index.php?option=com_content&view=article&id=32&uid=969

adding the article_id and the user_id.. that must be shown.

I want to give this url to the people that need to see this article.

For people that are not registered users -- they see the article.

The article is a dynamic article, and I add this code in PHP to extract data:

{source}<?php
$user = JFactory::getUser();
$userId = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
//echo $userId;
$query->select($db->quoteName(array('nombre_establecimiento', 'user_id',)))
    ->from($db->quoteName('insertar_logo'))
    ->where($db->quoteName('user_id') . ' = ' . $userId);
$db->setQuery($query);
$results = $db->loadAssocList();
foreach ($results as $rows) {
    echo "<center>";
        //echo "<img src='images/stories/" . $userId . "/image-1.png'>";
        echo "<H1>" . $rows['nombre_establecimiento'] . "</H1>" . "<BR>";
    echo "</center>";
}
?>
{/source}

If the user is logged in, the article show the data.

But when I try to use the link to show the article without the logged-in user, I can not see anything.

How can I relate the link with the article if the user is not logged in?

1
  • Welcome to JSE Javier. Please take our tour. You don't really have -&gt; in your real code, right? You have -> and this was just a question posting error? Oct 28, 2020 at 23:08

3 Answers 3

1

If I understand your problem correctly then you just have to change the first few lines of your original code because you do not just want look for the user ID from the database (user table) but you also need to define the user ID from the particular article's URL. So try it with using the following at the beginning of your code:

$articleUrl = \Joomla\CMS\Uri\Uri::getInstance();
$segmentUserID = $articleUrl->getVar('uid');

So including this in your original code it should look like the following:

{source}
<?php

$articleUrl = \Joomla\CMS\Uri\Uri::getInstance();
$segmentUserID = $articleUrl->getVar('uid');
//echo $segmentUserID;

$db = JFactory::getDbo();
$query = $db->getQuery(true);    

$query->select($db->quoteName('nombre_establecimiento'))
    ->from($db->quoteName('insertar_logo'))
    ->where($db->quoteName('user_id') . ' = ' . $db->quote($segmentUserID));
$db->setQuery($query);
foreach ($db->loadColumn() as $value) {
    echo "<h1 style=\"text-align:center; margin-bottom: 14px;\">{$value}</h1>";
}
?>    
{/source}

I did not review the other parts of your code, I only spot this relevant approaching problem in your code regarding getting the user ID from the correct place (from the URL). I hope that I did not misunderstand you issue and this will give you the expected result and the addition will appear in your article where it is needed even if the visitor is not logged in.

FEW ADDITIONAL NOTES ON THE ORIGINAL CODE

I just wanted to include few review notes on the original code based on @mickmackusa very helpful comments (He improved these parts of the original code):

  1. values in the query always should be secured by using quote($value) due to security reasons.

  2. Since the user_id is already defined from the URL thus we do not need to select that from the database. Selecting nombre_establecimiento value looks enough in this case.

  3. Creating a new variable for the query result as $result is an unnecessary variable declaration (single-use variable) -- instead feed $db->loadAssocList() directly into the loop.

  4. I do not see the point of using loadAssocList() since it looks like that we will get a single value from the table. Maybe the OP has another purpose of using that... @mickmackusa already improved that part of the code too using $db->loadColumn(), thanks for that!

1
  • @mickmackusa - however I do not have too much time at the moment but looking at this code a bit more I also did not understand why the OP wanted to select user_id too from the table. I thought that he wants to use that datum somewhere else in his code and that is one of the reasons why I did not want to touch that part of his code basically. But it looks redundant for me too if it is not used anywhere... thanks for pointing that out! I hope that the OP will refine that in his final code.
    – Zollie
    Oct 30, 2020 at 8:22
0

You should first separate when you have a know user from a guest one:

$user = JFactory::getUser();

if ($user->guest) {
    echo "<p>You must login to see the content</p>";
} else {
    // add your code
}

There is not way of know what user is, if he not logged-in first, so I think the best is show a message to him for logged-in first before access to the content.

0

thanks for your answer.

Maybe i didn´t explain clearly.. the user that see the information.. must to be public.. the user is not a guest.. can not login .. only see the information.

i generate a link for each registered user --> this link they will share with the customers... so the idea is with the link see the article.

for example for the user_id = 969

https://qrmenudigital.com/index.php?option=com_content&view=article&id=39&uid=969

and with this link.. the customer.. should see the article_id = 39

I'm trying to get the user_id when the url is clicked, with some code like this.

<?php

$app = JFactory::getApplication(); $uid = $app->input->getInt('uid', ''); //$query = $db->getQuery(true); echo $uid; ?>

with this code I'm trying to put in the article.. from which user is the info that i must to show...because the user is not registered... NOw if you click in the link you will see show the user_id.. inside the article_id=39 so next step.. will be using php.. try to get the info that i need.. trouble is sometimes i can see, sometimes i can not see the info.. let's see what happen!

1
  • This seems to be an extension of the question instead of an answer. Please read How to Ask and How to Answer. I will ask you to edit your question to include any clarifications then delete this "answer". Stack Exchange Q&A pages are not designed like a forum thread (on purpose). All of the question details go into the question body. All answers must attempt to resolve the question asked. Oct 30, 2020 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.