DB_DataObject_FormBuilder: select_display_field improvements

From: Date: Sat, 24 Jan 2004 01:05:44 +0000
Subject: DB_DataObject_FormBuilder: select_display_field improvements
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-25244@lists.php.net to get a copy of this message
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

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