Hello again,
I'm trying to get some info from my database to display in a module position. I think I have the code right, but I keep getting an "invalid foreach loop error"...and I can't seem to figure out whats wrong. Any ideas are appreciated. 😀
My code:
Thanks again,
Daniel-
I'm trying to get some info from my database to display in a module position. I think I have the code right, but I keep getting an "invalid foreach loop error"...and I can't seem to figure out whats wrong. Any ideas are appreciated. 😀
My code:
<?php
$user =& JFactory::getUser();
$user_id = "$user->id";
$db =& JFactory::getDBO();
$query ="
SELECT 'category', 'cf_user_id'
FROM 'jos_chronoforms_email_notification'
WHERE 'cf_user_id' = '$user_id' ;
";
$db->setQuery($query);
$subscriptions = $db->loadObjectList() ;
foreach ( $subscriptions as $subscription ) {
echo "$subscription->category" ;
}
?>
Thanks again,
Daniel-
Hi Daniel,
Please try this:
Max
Please try this:
<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$db =& JFactory::getDBO();
$query ="SELECT * FROM #__chronoforms_email_notification WHERE 'cf_user_id' = '$user_id' ;";
$db->setQuery($query);
$subscriptions = $db->loadObjectList() ;
foreach ( $subscriptions as $subscription ) {
echo $subscription->category;
}
?>
Max
Hi Daniel,
This does look like a quoting problem - your database query is returning no results. I'm not sure that Max has gone far enough though. Broadly the MySQL quoting rules are that table names and column names should be quoted with back-ticks ` `, string values should be quoted with single quotes ' ' and numeric values can be left unquoted (though single quotes will also work).
If in doube there are two Joomla functions $db->Quote('value') for values and $db->nameQuote('name') that will do this for you.
Try
Bob
This does look like a quoting problem - your database query is returning no results. I'm not sure that Max has gone far enough though. Broadly the MySQL quoting rules are that table names and column names should be quoted with back-ticks ` `, string values should be quoted with single quotes ' ' and numeric values can be left unquoted (though single quotes will also work).
If in doube there are two Joomla functions $db->Quote('value') for values and $db->nameQuote('name') that will do this for you.
Try
$query ="
SELECT `category`, `cf_user_id`
FROM `#__chronoforms_email_notification`
WHERE `cf_user_id` = '$user_id' ;
";
Bob
Thanks Max and Bob, that worked.
Working code is:
Daniel-
Working code is:
<?php
$user =& JFactory::getUser();
$user_id = $user->id;
$db =& JFactory::getDBO();
$query ="
SELECT `category`, `cf_user_id`
FROM `#__chronoforms_email_notification`
WHERE `cf_user_id` = '$user_id'
ORDER BY `category` ;
";
$db->setQuery($query);
$subscriptions = $db->loadObjectList() ;
echo '<ul type="circle">';
foreach ( $subscriptions as $subscription ) {
echo '<li>';
echo '<b>';
echo $subscription->category;
echo '</b>';
echo '</li>';
echo '<br>';
}
echo '</ul type="circle">';
?>
Daniel-
This topic is locked and no more replies can be posted.