How can I display a calculated value in my template?

A place for users and developers of the Xataface to discuss and receive support.

How can I display a calculated value in my template?

Postby s_nalepa » Mon Feb 02, 2009 3:46 am

In my tables/objekt folder I created a file objekt.php with these contents:
Code: Select all
<?php
class tables_objekt {
   function summa_est(){
      $result_est = mysql_query("select sum(est_low) as sum_low_est from objekt", $app->db());
          return $result_est;
   }
}
?>

Then I have made an extra slot in my own copy of the entire Dataface_Main_Template.html in my templates folder. In this slot I call another template. How can I access the value of the summa_est() function?
s_nalepa
 
Posts: 16
Joined: Sat Dec 29, 2007 8:43 am
Location: Lund, Sweden

Postby shannah » Mon Feb 02, 2009 6:10 pm

You probably want to do this as a block... Unfortunately you're not returning any text in this function - which a block is meant to do.

e.g.

Code: Select all
<php>db());
          while ($row = mysql_fetch_row($result_est) ){
             echo $row[0].'<br>';
          }
    }
}



Then you could access this in your template by adding:
Code: Select all
{block name="summa_est"}


You could also create a calculated field using your delegate class, however a calculated field is meant to be a member of a particular record. With calculated fields you would be able to return arbitrary types of data though (not just text).

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby s_nalepa » Mon Feb 02, 2009 9:30 pm

Where should I put this piece of code?
Could it look like this:
Code: Select all
<?php
$result_est = mysql_query("select sum(est_low) as sum_low_est from objekt", $app->db());
echo $result_est;
?>

?
s_nalepa
 
Posts: 16
Joined: Sat Dec 29, 2007 8:43 am
Location: Lund, Sweden

Postby s_nalepa » Mon Feb 02, 2009 9:59 pm

Now I tried this code in my objekt.php in this table's folder:
Code: Select all
<?
class tables_objekt {
   function block__before_main_table(){
      $result_est = mysql_query("select sum(est_low) as sum_low_est from objekt", $app->db());
      echo $result_est;
   }
}
?>
,
but I get this error:

Fatal error: Call to a member function db() on a non-object in /customers/xx/xx/httpd.www/xx/xx/tables/objekt/objekt.php on line 4
Last edited by s_nalepa on Mon Feb 02, 2009 10:10 pm, edited 1 time in total.
s_nalepa
 
Posts: 16
Joined: Sat Dec 29, 2007 8:43 am
Location: Lund, Sweden

Postby shannah » Mon Feb 02, 2009 10:04 pm

I was just using the code that you posted... Notice that you are using the $app object without first retrieving it (it is null). That is your error.

If you need to access the Dataface_Application object, you need to call:

Code: Select all
$app =& Dataface_Application::getInstance();


Alternatively, you could just use the df_db() function which returns the database connection resource.

E.g.

Code: Select all
$result_est = mysql_query("select sum(est_low) as sum_low_est from objekt", df_db());
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby s_nalepa » Mon Feb 02, 2009 10:16 pm

OK, now there's no error, but I get a "Resource id #78" instead of the value. How do I resolve this to a value?
s_nalepa
 
Posts: 16
Joined: Sat Dec 29, 2007 8:43 am
Location: Lund, Sweden

Resolved

Postby s_nalepa » Mon Feb 02, 2009 10:48 pm

Steve!

I finally solved it! Thank you very, very much for your help!
The final, working code is:
Code: Select all
<?
class tables_objekt {
   function block__before_main_table(){
      $res = mysql_query("select sum(est_low) from objekt", df_db());
      while ($row = mysql_fetch_array($res)){
         $result_est = 'Summa utrop: '.$row[0].' SEK';
      }
      echo $result_est;
   }
}
?>

Found the solution after some searching.

So this morning I have learnt a few quite important things - and feel that I am ready for further work with Xataface. I'm hopeful that I will get a nice system in the end.

Kudos.
s_nalepa
 
Posts: 16
Joined: Sat Dec 29, 2007 8:43 am
Location: Lund, Sweden


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 13 guests

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