Soft-coded column names

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

Soft-coded column names

Postby TBriggs » Mon Oct 05, 2009 10:20 am

I want to have a database where some of the column names can be defined by the administrator. There will be a table where these column names are defined.
The other database tables will be created with generic column names, which should be replaced with the administrator-defined names wherever displayed to the user.
By reading through the documentation and forum posts it would seem that this should be quite possible using the fields.ini files, but I'm not sure exactly how this would be accomplished.
If anyone can point me to an example or documentation on this I would be very grateful.
TBriggs
 
Posts: 47
Joined: Mon Mar 02, 2009 6:38 am
Location: Gulfport, FL

Postby Jean » Tue Oct 06, 2009 2:45 am

It is in http://xataface.com/documentation/how-to/list_tab
in
Code: Select all
class table_people {
    ...
    function renderRowHeader( $tablename ){
        // First let's load the Dataface_Table object so we know what the labels are that we want to use.
        // ** NOTE: in this case the $tablename parameter is a little redundant because we already know
        // that we're working on the people table.  However, if we were implementing this method in the
        // application delegate class, we would need to use this parameter.
        $table =& Dataface_Table::loadTable($tablename);
        $fields =& $table->fields();
        return '<th>'.$fields['first_name']['widget']['label'].'</th>'.
               '<th>'.$fields['last_name']['widget']['label'].'</th>'.
               '<th>'.$fields['phone']['widget']['label'].'</th>';
    }


Cheers
Jean
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Postby TBriggs » Tue Oct 06, 2009 9:16 am

Merci, Jean.
There's a couple of things I'm not clear about. Firstly, where do you put this class definition? Secondly, this solution still seems to have the column labels hard-coded - how would you substitute values from a file as the column names?
Sorry if I seem to be missing something, but I'm new to PHP and this code is way beyond my experience.
TBriggs
 
Posts: 47
Joined: Mon Mar 02, 2009 6:38 am
Location: Gulfport, FL

Postby Jean » Tue Oct 06, 2009 9:41 am

You put this method inside the delegate class of your table
http://xataface.com/wiki/Delegate_class_methods
You may get the column names from a table like in the example or you can have them hard-coded inside your method
Code: Select all
return '<th>first title</th>'.
               '<th>second title</th>'.
               '<th>third title</th>';

This is just a string made of concatenated strings.
Cheers
Jean
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Postby TBriggs » Tue Oct 06, 2009 12:51 pm

I think I understand what I'm misunderstanding!
In your first example, you are retrieving the names of columns from a table and using these as the column headings.
But, what I'm trying to do is get field names that are stored as data in a table.
For example, table #1 will be called "labels" and one column would be called "male". This column could contain the values "buck", "stallion", "bull", etc.
Table #2 is called "offspring" and has a column containing the name of an animal's male parent. The column in table #2 would be called "male" (so that it can be related to the corresponding column in table #1), but on the screen I want this column's header to be whatever value the user put in the "male" column in table #1.
Does that make sense?
TBriggs
 
Posts: 47
Joined: Mon Mar 02, 2009 6:38 am
Location: Gulfport, FL

Postby Jean » Tue Oct 06, 2009 11:44 pm

You can get data from your table with this kind of code :

Code: Select all
$app =& Dataface_Application::getInstance();
$res = mysql_query("select...'", $app->db());
while ($row=mysql_fetch_object($res)){....
$chose=$row->chose;...


Jean
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Postby shannah » Wed Oct 07, 2009 11:27 am

Can you elaborate on your original question. E.g. give an example table structure and describe how you want these tables to appear to different users. I'm not sure I fully understand the question yet.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby TBriggs » Wed Oct 07, 2009 12:37 pm

shannah wrote:Can you elaborate on your original question. E.g. give an example table structure and describe how you want these tables to appear to different users. I'm not sure I fully understand the question yet.


Say I have a table called "animal_offspring", which has two columns called "father" and "mother".

I have another table called "labels" that also has two columns called "father" and "mother". In this table the user can enter in the "father" field a value like "sire" or "buck" or "stallion". This designates what they want the "father" column on the first table to be called wherever it appears.

The column will have the same title for all users in this implementation, but the entire system can be duplicated and used for a whole different animal type by just changing the contents of the "labels" table.

Does this make it clearer?

Thanks for any help with this. I think Jean may have got the right idea, but my PHP knowledge is still a little sketchy and I'm trying to work out what his code actually does!

Thanks.
TBriggs
 
Posts: 47
Joined: Mon Mar 02, 2009 6:38 am
Location: Gulfport, FL

Postby shannah » Wed Oct 07, 2009 5:49 pm

OK.. in the delegate class of the first table, implement the init method as follows:

Code: Select all
class tables_animal_offspring {

function init(&$table){
   
    // First load the labels from the labels table
    $res = mysql_query("select father, child from labels limit 1", df_db());
    if ( !$res ) throw new Exception(mysql_error(df_db()));
    list($fatherLabel, $childLabel) = mysql_fetch_row($res);
    mysql_free_result($res);

    // Now set the father label
    $fatherField =& $table->getField('father');
    $fatherField['widget']['label'] = $fatherLabel;
   
    // Now set the child label
    $childField =& $table->getField('child');
    $childField['widget']['label'] = $childLabel;
} // end function

} // end class
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 14 guests

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