how to show all data from joomla table?

2019-09-08 12:00发布

问题:

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>";
}