how to show all data from joomla table?

谁说我不能喝 提交于 2020-01-06 15:07:58

问题


recently i learned how to get data from database by this method

public static function getdb($params)
{
// Get a database object
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');

// sets up a database query for later execution
$db->setQuery($query);

// fetch result as an object list
$result = $db->loadObjectList();
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
}

by this line

echo "$row->extension .<br>";

we get single row value. how to we get all row value of #__categories? by any short code.


回答1:


You can use another foreach inside

foreach ( $result as $row ) {  // it is like  foreach($objects as $object)
  foreach ($row as $r) {       // it is like  foreach($object as $values)
    echo "$r .<br>";
  }
}

But you should print the code in the view not in the model, controller, etc.




回答2:


The foreach loop should give you all the rows for your query. Try making a few changes like so:

helper.php

public static function getdb($params) {

    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $query->select('*');
    $query->from('#__categories');

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

    return $result;
}

default.php

$result = modHelloWorldHelper::getdb($params);

foreach ( $result as $row ) {
    echo $row->extension . "<br>";
}


来源:https://stackoverflow.com/questions/20245525/how-to-show-all-data-from-joomla-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!