I have a block of code that works perfect:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('date')));
$query->from($db->quoteName('#__webfoot_minutes_and_agendas'));
$query->order('date DESC');
$db->setQuery($query);
$results = $db->loadObjectList();
Until I try to assign it to a function:
function call_db() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('date')));
$query->from($db->quoteName('#__webfoot_minutes_and_agendas'));
$query->order('date DESC');
$db->setQuery($query);
$results = $db->loadObjectList();
return $results;
}
It breaks when I try to call the function, I get an error for $results I get, "Invalid argument supplied for foreach()...:
call_db();
foreach ($results as $result) {
$dateArr = explode('-', $result->date);
if (!in_array($dateArr[0], $already_echoed)) {
echo '<li>' . $dateArr[0] . '</li>';
$count++;
}
$already_echoed[] = $dateArr[0];
}
Anyone have any suggestions on how I can straighten out this syntax?
You have to assign result of your function to variable.
$results = call_db();
Though it is awful design:
foreach (call_db() as $result) { ... }
You haven't assigned return value of call_db() to anything. You have to assign it first to $results
$results = call_db();
And, after that it is better if you check that the $results is an array. See this.
Related
Is it possible to fill an array from the statement->fetchAll()
(PDO fetchAll)?
public function showDetails()
{
$db = ConnectDatabase::getInstance();
$connect = $db->getConnection();
$statment = $connect->query('SELECT * FROM users');
$result = $statment->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
Do you mean like this?
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
In order to echo an ARRAY, you must use print like so:
print_r($member2->showDetails());
That being said, it won't be very... pretty. Use a loop to format how you want:
$users = $member2->showDetails();
foreach ($users as $user) {
echo $user['User_Name'];
}
Where 'col1' is a column you want to echo.
Sorry. I am very new to Joomla.
After I load data from the database like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('user_id')));
$query->from($db->quoteName('#__user_profiles'));
$db->setQuery($query);
$results = $db->loadObjectList();
How to I show the results?
Try
foreach ($results as $result) {
echo $result->user_id.'<br />';
}
in joomla module to get data from database we use the code
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>";
}
}
my question is how to use this foreach loop in tmpl/default.php ? and then wat will be my helper.php code?
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
if i use this foreach loop into default.php then it will better for me. pls someone help
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:
//call the function from the helper.php
$result = modHelloWorldHelper::getdb($params);
//display the results
foreach ( $result as $row ) {
echo $row->extension . "<br>";
}
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.
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>";
}
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.
Am learning & executing php by working on Joomla project
How to Improve this code & resolve the PHP Notices - Any suggestions - solutions - well appreciated !!
Notice: Undefined variable: cond in*/home/mygames/public_html/components/com_toys/models/category.php on line 140
(which is $sql line)*
function loadSubCat($id,$Carmodel,$minprice,$maxprice){
$mainframe =& JFactory::getApplication();
$option = JRequest::getCmd('option');
$database =& JFactory::getDBO();
global $Itemid;
if($Carmodel!="")
$cond=" and prod_id='$Carmodel' ";
$sql = "Select * from #__toycar_products Where prod_cat_id='".$id."' $cond and prod_status='1' and prod_id in (select v_prod_id from #__toycar_variants) Order By prod_sorder";
Notice: Trying to get property of non-object in /home/truecar7/public_html/components/com_toys/models/category.php on line 200
Line 200 is return $row->id;
function getItemIdByName($Name){
$mainframe =& JFactory::getApplication();
$option = JRequest::getCmd('option');
$database =& JFactory::getDBO();
$sql = "Select id from #__menu Where name = '".$Name."'";
$database->setQuery($sql);
$row = $database->loadObject();
return $row->id;
}
Edit
Hello Lodder & Elin, it works but like this, else it's showing undefined variable notice for row on return $row line.
function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__menu')
->where('id = ' . $db->quote($Name));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row){
$row = $row->msg;
}
$row='';
return $row;
}
Try using the following. I have made some changes to your function and used Joomla 2.5 coding standards for the database query.
$Name = "XXXXXXXXX"; //define the name variable
function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__menu')
->where('id = ' . $db->quote($Name));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row){
$row = $row->msg;
}
return $row;
}
echo getItemIdByName($Name); //echo the result of the function
For your Undefined Notice, You have to modify your codes like this
$cond = '';
if($Carmodel!="") {
$cond = " and prod_id='$Carmodel' ";
}
For Trying to get property of non-object Notice : I think $row is empty that is why throws notice.Check $row
var_dump($row);
Problem :
$database->loadObject(); // This line