Problem with a sql query and the dashboard

A place to discuss development of the Xataface core.

Problem with a sql query and the dashboard

Postby siggj » Sun Sep 19, 2010 9:29 am

Hello everybody !
I have a question about sql and the dashboard from the wiki.

So, I have a dashboard.php and in the handle function i have this :
Code: Select all
   
        $app =& Dataface_Application::getInstance();  // reference to Dataface_Application object
        $auth =& Dataface_AuthenticationTool::getInstance();
        $user =& $auth->getLoggedInUser();

        // Perform a custom SQL Query:
          $res = mysql_query("select * from Event where fk_user = $user->getValue('id') ", $app->db());

      
        df_display(array('Project'=>$projects,'Event'=>$res), 'dashboard_project.html');


Then in the dashboard_project.html (from the templates directories) , I have :
Code: Select all
{use_macro file="Dataface_Main_Template.html"}
                {fill_slot name="main_column"}     
                    {foreach from=$Event item=event}
                           <li><a href="{$event->getURL('-action=view')}#embed"> {$event->getTitle()} </a><br  /><em>from project name_of_project</em></li>
                    {/foreach}
                  {/fill_slot}
            {/use_macro}


But this code doesn't work... :cry:

Anybody know something about this ?

Thank you !
siggj
 
Posts: 12
Joined: Tue Jul 20, 2010 4:22 am

Re: Problem with a sql query and the dashboard

Postby shannah » Mon Sep 20, 2010 10:56 am

The $res variable is not an array. It's a database resource pointer. Hence you can't loop through it directly with a foreach loop. You first need to put the elements into an array. e.g.

Code: Select all
$items = array();
while ($row = mysql_fetch_assoc($res) ) $items[] = $row;
@mysql_free_result($res);  // free up memory with database pointer now that we're done.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Problem with a sql query and the dashboard

Postby siggj » Tue Sep 21, 2010 1:43 pm

Hello, thanks for responding !

I dont understand very well..
Can i do Something like this ?
Code: Select all
   
$app =& Dataface_Application::getInstance();  // reference to Dataface_Application object
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();

$res = mysql_query('SELECT * FROM Event', $app->db());

$items = array();
while ($row = mysql_fetch_assoc($res) ) $items[] = $row;
@mysql_free_result($res);  // free up memory with database pointer now that we're done.

df_display(array('Project'=>$projects,'Event'=>$items), 'dashboard_project.html');


Thank you for your time !
siggj
 
Posts: 12
Joined: Tue Jul 20, 2010 4:22 am

Re: Problem with a sql query and the dashboard

Postby shannah » Tue Sep 21, 2010 1:52 pm

Yes.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Problem with a sql query and the dashboard

Postby siggj » Wed Sep 22, 2010 3:02 pm

I'm sorry but it doesn't work... I have exactly this :
Code: Select all
<?php
class actions_dashboard_project {
    function handle(&$params){   
      $app =& Dataface_Application::getInstance();  // reference to Dataface_Application object
             
                 // Perform a custom SQL Query:
      $sql = 'SELECT * FROM Event;';
                $res = mysql_query($sql, $app->db());
      
      $items = array();
      while ($row = mysql_fetch_assoc($res)) $items[] = $row;
       
      df_display(array('Event'=>$items), 'dashboard_project.html');
    }
}


And then in the dashboard :
Code: Select all
       {foreach from=$Event item=event}
            <li>{$event->val('ID_Event')}</li>
       {/foreach}


So I ask you if you can explain me where it's wrong, and why...
THank you for your time ..
siggj
 
Posts: 12
Joined: Tue Jul 20, 2010 4:22 am

Re: Problem with a sql query and the dashboard

Postby shannah » Fri Sep 24, 2010 10:53 am

You are passing associative arrays to the template, but associative arrays don't have any methods (e..g no val() method). The val() method is available only when working with Dataface_Record objects.

Using arrays in smarty templates, you access their elements with dot notation.

e.g.
Code: Select all
{foreach from=$Event item=event}
            <li>{$event.ID_Event}</li>
       {/foreach}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Developers

Who is online

Users browsing this forum: No registered users and 16 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved