Re: DB_DataObject_FormBuilder: select_display_field improvements

From: Date: Sat, 24 Jan 2004 22:25:04 +0000
Subject: Re: DB_DataObject_FormBuilder: select_display_field improvements
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-25255@lists.php.net to get a copy of this message
I hate to burst your bubble, but I've already done multiple field select options as well as recursive looking up of links. I didn't do formatting, however. If you add a dbName.formBuilder.ini file which has: [tableName__display_fields] 0 = field1 1 = field2 [tableName__order_fields] 0 = field1 1 = field2 Then put this in your DataObject.ini: [DB_DataObject_FormBuilder] no_follow_links = 0 You will get this option. It also parenthesizes recursive lookups. If you could add your formatting option to what it already does that would be great. (Note that these features are already in the released version of FormBuilder.) Norbert Mocsnik wrote:
Hi, To be short: The current version of FormBuilder can handle two type of $select_display_field: a) $select_display_field is a field name $select_display_field = 'country'; output1: <option value=1>USA</option> output2: <option value=2>Germany</option> b) $select_display_field is an array of field names $select_display_field = array('country', 'lang'); output1: <option value=1>USA, English</option> output2: <option value=2>Germany, German</option> The b) version is better but it doesn't allow you to use an own display format. Here is my new c) version, hope you like it: c) $select_display_field has my new format :) $select_display_field = '{country} (language: {lang})'; output1: <option value=1>USA (language: English)</option> output2: <option value=2>Germany (language: German)</option> You may have any combination of field names but BE SURE TO USE {} CORRECTLY (don't interleave {} blocks, be sure to open and close them well etc). Of course I took care about backward compatibility. Adding this code fragment doesn't break existing applications based on FormBuilder, as long as they don't use {} in their field names (I'm sure there's no developer using {} in field names, even if there's a dbms allowing it). code changes (no diff actually, sorry): in DB_DataObject_FormBuilder::getSelectOptions: replace: } else {
    $list[$opts->$pk] = $opts->$displayfield;
} to the following code fragment: if (strpos($displayfield, '{')!==false) {
    // parse field names like "{test1} {test2} - {test3}"
    // collect field names (variables) from $displayfield
    $fieldnames = array();
    $s = $displayfield; // "{test1} {test2} - {test3}"
    while ( ((strpos($s, "{")!==false) && (strpos($s, "}")!==false)) ) {
        $fieldnames[] = substr($s, strpos($s, '{') + 1, strpos($s, '}') - strpos($s, '{') - 1);
        $s = substr($s, strpos($s, '}') + 1);
    }
    // replace vars to record values
    $s = $displayfield; // "{test1} {test2} - {test3}"
    foreach ($fieldnames as $fieldname) {
        $s = str_replace('{'.$fieldname.'}', $opts->$fieldname, $s);
    }
    $list[$opts->$pk] = $s;
} else {
    // one field only, no need to parse
    $list[$opts->$pk] = $opts->$displayfield;
} After this has been added FormBuilder I'll be glad to post my new version d) which I'm actually working on: d) $select_display_field has my NOT-YET-EXISTING format $select_display_field = '{country} (language: {lang_id->name})'; output1: <option value=1>USA (language: English)</option> output2: <option value=2>Germany (language: German)</option> In this example lang_id->name refers to the "name" field of a linked table ("languages"; specified in db.links.ini). Links will be automagically resolved. Hope y'all like it! Regards, Norbet
-- paperCrane <Justin Patrin>

« previous php.pear.dev (#25255) next »