0

I need to display data in an article in tabular form.
I am using sourcerer by NoNumber for adding the select code in the article:

<?php

defined('_JEXEC') OR die("Restricted access"); 
$user =&JFactory::getUser();
$db =&JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__bat_score'); 
$query->where('id = 1');   //put your condition here    
$db->setQuery($query);
//echo $db->getQuery();exit;
if ($db->getErrorNum()) {
  echo $db->getErrorMsg();
  exit;
}
return $db->loadObjectList();

// from Joomla Resources 
// the current code is not showing anything 


The error i'm getting is like in this image:

enter image description here

The desired output is displaying the rows of the mysql table in a html table.

Please suggest some guidance on this as the best method to show the database data in the articles. Thanks in advance.

  • Try using var_dump($variable) in certain areas to find out of anything is undefined. You should also make sure you quote your values, column name and table name. Have a read through the Joomla documentation for Database queries – Lodder Mar 18 '15 at 8:09
  • thanks for the suggestion @lodder,can you post any links to examples where i can start with (any specific part of documentation which would help me right away for accomplishing the task!) – Srinath Naidu Mar 18 '15 at 12:00
  • See my answer below. – Lodder Mar 18 '15 at 12:00
1

First, remove the following line of code as you won't need it.

defined('_JEXEC') OR die("Restricted access");

Secondly, you are returning something but you're only supposed to return in functions. In your case, all you need to do is echo the values,

Try using the following:

<?php
$user   = JFactory::getUser();
$db     = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select(array('*'))
      ->from($db->quoteName('#__bat_score'))
      ->where($db->quoteName('id') . ' = 1');

$db->setQuery($query);
$rows = $db->loadObjectList();

if ($db->getErrorNum()) 
{
  echo $db->getErrorMsg();
  exit;
}

echo '<table>';
foreach ($rows as $row)
{
    echo '<tr>';
    echo '<td>' . $row->COLUMN_NAME   . '</td>'; 
    echo '<td>' . $row->COLUMN_NAME_2 . '</td>'; 
    echo '</tr>';
}
echo '</table>';
?>

You of course need to change COLUMN_NAME to whatever your column name if for your database table.

  • Just to be specific I have added the COLUMN_NAME to batsman to howout ( the actual names of columns ) in single qoutes , double quotes but they are reflecting similar result as it was previously awesomescreenshot.com/0a34nndn64 – Srinath Naidu Mar 18 '15 at 12:08
  • You don't need to use quotes. Simple use something like $row->hello – Lodder Mar 18 '15 at 12:09
  • I m sorry but what am i doing wrong ?awesomescreenshot.com/02a4nnen40 it doesnt show after removing the quotes also. – Srinath Naidu Mar 18 '15 at 12:12
  • Are you sure you've wrapped all your PHP code around the {source} {/source} tags in the article? – Lodder Mar 18 '15 at 12:14
  • Yes sir I m sure of that ! is it my database table name ! My database table name is tabs_bat_score; I have mentioned it as #__bat_score ! – Srinath Naidu Mar 18 '15 at 12:18
-1

you don't need a third party extension you can copy the article view in your template html folder and then add your custom code (better with a content plugin), but you can get your data with:

$db = JFactory::getDBO();
$db->setQuery('select * from #__bat_score where id = 1');
$rows = $db->loadObjectList();
if(count($rows)) {
echo '<table>';
foreach($rows as $row) {
//print your table rows
}
echo '</table>';
}
  • You don't put queries in views. Nor have you quoted any the column names or database table – Lodder Mar 18 '15 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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