Display database info in module position

tidusx18 18 May, 2009
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:
<?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-
Max_admin 19 May, 2009
Hi Daniel,

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
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 19 May, 2009
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
$query ="
  SELECT `category`, `cf_user_id`
    FROM `#__chronoforms_email_notification`
    WHERE `cf_user_id` = '$user_id' ;
";

Bob
tidusx18 19 May, 2009
Thanks Max and Bob, that worked.

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.