1

This seems to be such an easy thing to do but I can't fine any information on how to do it. Can someone please tell me how to add the results of a query to an article in Joomla 2.5?

In an article this is what I am trying to accomplish.

Bob's favorite color is: <<SELECT favorite_color FROM COLORS WHERE name = 'Bob'>>

I have no idea how to do this.

user3242794
  • 23
  • 1
  • 3

2 Answers2

1

First thing is first, download and install the Sourcerer plugin which will allow you to add custom code to your articles.

One done, open your article and click the "Add Code" button below the textarea. Here you can add you custom code.

I'll write more or less the query you want using Joomla coding standards which most people seem to forget about:

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select($db->quoteName(array('favorite_color')))
      ->from($db->quoteName('colors'))
      ->where($db->quoteName('name') . ' = '. $db->quote('bob'));
$db->setQuery($query);
$rows = $db->loadObjectList();

foreach ($rows as $row) {
    echo $row->favorite_color;
}

Notes that if the colors tables belongs to a Joomla extensions, then use #__colors

Hope this helps

Lodder
  • 19,758
  • 10
  • 59
  • 100
  • Lodder, thank you for the reply. I found that extension earlier in my search and unfortunately it requires PHP 5.3 and I have 5.2.17. – user3242794 Feb 01 '14 at 21:21
  • 1
    Here is a download link to an older version of the extension that works with PHP 5.2 ;: http://download.nonumber.nl/?ext=sourcerer&v=3.1.0 ... enjoy :) – Lodder Feb 01 '14 at 21:23
  • Thanks! I'm getting closer. I've got sourcerer installed and I went to the point in my article where I want the result displayed. I clicked the Insert Code <> button and added the code you provided in the PHP section. Now my page displays the code not the results. Also my query will always return a single value. How do I actually display the result? – user3242794 Feb 01 '14 at 21:44
  • This is what displays on my page: getQuery(true); $query->select($db->quoteName(array('favorite_color'))) ->from($db->quoteName('colors')) ->where($db->quoteName('name') . ' = '. $db->quote('bob')); $db->setQuery($query); $rows = $db->loadObjectList(); foreach ($rows as $row) { //display results here } ?> – user3242794 Feb 01 '14 at 21:45
  • Sorry about the formatting, I don't know how to insert code! – user3242794 Feb 01 '14 at 21:46
  • I have updated my answer with an `echo` that will display the results for you. In the box that appears allowing you to add your code, remove the – Lodder Feb 01 '14 at 21:47
  • Lodder, thank you, everything is working as I need it to. I really appreciate your help. – user3242794 Feb 01 '14 at 23:45
  • You're more than welcome. Please don't forget to help the [Joomla Proposal](http://area51.stackexchange.com/proposals/58842/joomla?referrer=Ph_HGDW6OiEqU7r0cS-muA2) by committing to it. We're really trying to get a dedicated Stackexchange site for Joomla – Lodder Feb 01 '14 at 23:54
0

You can make a content plugin. THe other thing is that you can make a module to show the data and then use loadposition to load the module.

Elin
  • 6,507
  • 3
  • 25
  • 47