how to show all data from joomla table?

2019-09-08 11:59发布

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.

2条回答
Root(大扎)
2楼-- · 2019-09-08 12:06

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>";
}
查看更多
该账号已被封号
3楼-- · 2019-09-08 12:13

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.

查看更多
登录 后发表回答