Page 1 of 1

Soft-coded column names

PostPosted: Mon Oct 05, 2009 10:20 am
by TBriggs
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.

PostPosted: Tue Oct 06, 2009 2:45 am
by Jean
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

PostPosted: Tue Oct 06, 2009 9:16 am
by TBriggs
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.

PostPosted: Tue Oct 06, 2009 9:41 am
by Jean
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

PostPosted: Tue Oct 06, 2009 12:51 pm
by TBriggs
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?

PostPosted: Tue Oct 06, 2009 11:44 pm
by Jean
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

PostPosted: Wed Oct 07, 2009 11:27 am
by shannah
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.

PostPosted: Wed Oct 07, 2009 12:37 pm
by TBriggs
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.

PostPosted: Wed Oct 07, 2009 5:49 pm
by shannah
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