Access parent info in custom title?

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

Postby Aoirthoir » Thu Nov 02, 2006 8:02 pm

THIS IS SOLVED...see the second post.

I have the following code for my tables/patients.php file:

Code: Select all

class tables_patients{
   function getTitle(&$record){
      $firstname=$record->val('name_first');
      $middlename=$record->val('name_middle');
      $lastname=$record->val('name_last');
      $fullname=$firstname.' '.$middlename.' '.$lastname;
      $return $fullname;
   }
}




It works splendidly. In firefox I have my tabs resizable to giganto proportions so my users can see the full name on each tab. I am curious if I can do the same thing for the patients child tables. So it would have the name of the person..followed by any other designator I choose....



[[ Edited to remove php tags since it caused problems with my original post... but obviously those are the greater than question mark etc etc ]]

[[edited again since the problem was fixed..see below....]]

I know that code isnt correct since it would try to reference the child's 'name_first' field, of which there is none.

Thank you kindly in advance. This is not a rush question.
Aoirthoir
 
Posts: 420
Joined: Wed Dec 31, 1969 5:00 pm

Postby Aoirthoir » Fri Nov 03, 2006 9:13 am

Ok this may not be the best way to do this..but it works. I just did a delegate class for each of the sub tables, and a call to mysql directly. Here is the code:

Code: Select all
class tables_patients_dates {
   function getTitle(&$record){
      $username="username";
      $password="mypassword";
      $database="mydb";
      $host="myhost";
      $connect=mysql_connect($host,$username,$password);
      $dbsel=@mysql_select_db($database) or die("Unable to select database");
      $currentrecord=$record->val('lookupfield');
      $query="SELECT * FROM patients where lookupfield="."'".$currentrecord."'";
      $result=mysql_query($query);
      $firstname=mysql_result($result,0,"name_first");
      $middlename=mysql_result($result,0,"name_middle");
      $lastname=mysql_result($result,0,"name_last");
      $fullname=$firstname.' '.$middlename.' '.$lastname;
      return $fullname."'s Dates";
   }
}



The fields username, password, host etc, would have values the same as in conf.ini. Probably not too secure to have it in the php but its a local intranet. Later on when I reread how to read the settings from conf.ini I will change those to match that.

I also think this method will work to allow me to display fields from a parent record on a childs tab. Going to experiment with that in a minute.
Aoirthoir
 
Posts: 420
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Fri Nov 03, 2006 1:18 pm

This looks like a good solution. One thing that would make it perform better and be a little more elegant is to use the existing database connection rather than creating a new one. You can do this by replacing:
Code: Select all
      $username="username";
      $password="mypassword";
      $database="mydb";
      $host="myhost";

      $connect=mysql_connect($host,$username,$password);

      $dbsel=@mysql_select_db($database) or die("Unable to select database");


with:
Code: Select all
     $app =& Dataface_Application::getInstance();
     $dbsel = $app->db();


Then for robustness, you should also specify the db connection in your mysql_query() calls (in case your script uses 2 db connections at some point.
e.g.:
Code: Select all
$result=mysql_query($query, $dbsel);


Thanks for posting this question/example/solution... I think it will help a lot of people.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Aoirthoir » Fri Nov 03, 2006 4:07 pm

Thanks for the tip Mr. Steve. Worked like a charm. Also regarding your suggestion to use $dbsel in the mysql_query line to allow 2 db connections at once..that in once case I would use the settings I have..perhaps connecting to a different database into a field (called perhaps $dbsel2) and do my own entire mysql statement as I have posted above..while still doing the mysql statement you posted to connect to the dataface app databases. Is this correct?

Also I have modified the app a little. Ive split out the common things to an .inc file and placed it just in my tables directory. Thus in this case tables/fullname.inc is the file. Then each of the delegate classes has an include statement. However....again as I understand it.. the php include is relative to the calling php or something. So in each of my tables/tablename/table.php delegate classes I was unable to just do ../fullname.inc since it appeared from the error message I got that it was trying to load fullname.inc from one of the dataface directories. My current solution was to simply use the full system path to my tables directory and it worked fine.

So here is the code in tables/fullname.inc :
Code: Select all
[??
      $app =& Dataface_Application::getInstance();
      $dbsel = $app->db();
      $currentrecord=$record->val('pat_acct');
      $query="SELECT * FROM patients where pat_acct="."'".$currentrecord."'";
      $result=mysql_query($query,$dbsel);
      $firstname=mysql_result($result,0,"name_first");
      $middlename=mysql_result($result,0,"name_middle");
      $lastname=mysql_result($result,0,"name_last");
      $fullname=$firstname.' '.$middlename.' '.$lastname;
?]]


And for my delegate classes I have this:
Code: Select all
[[?
class tables_patients_notes {
   function getTitle(&$record){
      include "/dfapps/estim/tables/fullname.inc";
      return $fullname."'s Notes";
   }
}
?]]


This way I can change it in one place. At first I got an error I thought was related to the variables not being global. So I moved the include inside of the getTitle function and it works fine.
Aoirthoir
 
Posts: 420
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 31 guests

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