Platon Technologies
neprihlásený Prihlásiť Registrácia
SlovakEnglish
open source software development oslavujeme 10 rokov vývoja otvoreného softvéru! Sobota, 28. november 2026
O nás
Magazín
Otvorený softvér
CVS
Služby
Index  »  Projekty  »  phpMyEdit  »  Fórum  »  Entering optional item values from selected values

phpMyEdit Features     Entering optional item values from selected values
Odoslať novú tému   Odpovedať na tému    
 
bernie95     Založený: 03.09.2003   Príspevky: 10  
Príspevok Zaslal: 2003-09-03 15:07
Návrat hore  Odpovedať s citátom     

I was trying to enter data in a field that should contain an ID referencing another table; I thus use the following field configuration:
Kód:

$livres_opts['fdd']['CollectionID'] = array(
  'name'     => 'CollectionID',
  'select'   => 'T',
  'maxlen'   => 8,
  'required' => false,
  'sort'     => true,
  'values'   => array(
   'table'      => 'collections',
   'column'   => 'ID',
   'description'   => 'Collection'
  )
);


The problem I encountered is that, although the field is optional (required = false), the select droppdown list forces to choose an entry and do not allow to let the field "unspecified".

I made a few changes to phpMyEdit to allow for this:

At the beginning of display_add_record() I added some code to automatically add a default choice, either getting the provided default value, either NULL:
Kód:

   echo '<td class="',$this->getCSSclass('key', null, true, $css_postfix),'">',$this->fdd[$k]['name'],'</td>',"\n";
   echo '<td class="',$this->getCSSclass('value', null, true, $css_postfix),'">'."\n";
   if ($this->col_has_values($k)) {
+      if (!$this->fdd[$k]['required']) {
+         if (isset($this->fdd[$k]['default'])) {
+            $vals     = $this->set_values($k, array($this->fdd[$k]['default'] => ''));
+         } else {
+            $vals     = $this->set_values($k, array('NULL' => ''));
+         }
+      } else {
         $vals     = $this->set_values($k);
+      }
      $selected = @$this->fdd[$k]['default'];
      $multiple = $this->fdd[$k]['select'] == 'M' && ! $this->fdd[$k]['values']['table'];
      $readonly = $this->readonly($k);


Then I added a bit of code in htmlSelect() to be able to handle the NULL case cleanly:
Kód:

}
$found = false;
foreach ($kv_array as $key => $value) {
+   if ($key === 'NULL') {
+      $ret .= '<option value=NULL';
+   } else {
      $ret .= '<option value="'.htmlspecialchars($key).'"';
+   }
   if ((! $found || $multiple) && in_array((string) $key, $selected, 1)
         || (count($selected) == 0 && ! $found && ! $multiple)) {
      $ret  .= ' selected';


Finally in do_add_record() I avoid quoting the possible NULL values so that mySQL receive a NULL column value, and not an empty one, which is different:
Kód:

      } else {
         $fn = $this->get_cgi_var($this->fds[$k]);
      }
+      if ($fn != "NULL") {
       $newvals[$this->fds[$k]] = is_array($fn) ? join(',',$fn) : $fn;
       $vals_quoted[$k] = addslashes($newvals[$this->fds[$k]]);
      $vals_quoted[$k] = "'".$vals_quoted[$k]."'";
+      } else {
+         $newvals[$this->fds[$k]] = "NULL";
+         $vals_quoted[$k] = "NULL";
+      }
   }
}
// Creating array of changed keys ($changed)


This seems to work fine, at least for me. The only drawback that I see is that if the default value, or any value in fact, for a field that has to be select-ed appears to be 'NULL' it's sent to mySQL as a NULL (without quates) field, something that may not be what was expected.

To overcome this would need to be able to distinguish, in th eposted variable, between the result of selecting options with 'value=NULL' and 'value="NULL"'; if this is not possible, maybe we could simply replace NULL by some "impossible" value?

Please can anybody more fluent than me in these things look at this and either confirm my approach or propose a better one.

TIA

Bernard

 
bernie95     Založený: 03.09.2003   Príspevky: 10  
Príspevok Zaslal: 2003-09-06 12:52
Návrat hore  Odpovedať s citátom     

bernie95 Napísal:
I was trying to enter data in a field that should contain an ID referencing another table
....
I made a few changes to phpMyEdit to allow for this:


My changes as documented in the previous post weer a bit insufficient. They only handle the "add record" case, not the "change record" one. The small change to do are:

1) In display_change_field , beginning at the fourth line:
Kód:

   if ($this->col_has_values($k)) {
+      if (!$this->fdd[$k]['required']) {
+         if (isset($this->fdd[$k]['default'])) {
+            $vals     = $this->set_values($k, array($this->fdd[$k]['default'] => ''));
+         } else {
+            $vals     = $this->set_values($k, array('NULL' => ''));
+         }
+      } else {
         $vals     = $this->set_values($k);
+      }
      $multiple = $this->fdd[$k]['select'] == 'M' && ! $this->fdd[$k]['values']['table'];


2) In do_change_record , beginning at the seventh line:
Kód:

   if ($this->processed($k) && !$this->readonly($k)) {
      $fd = $this->fds[$k];
      if ($fd == $this->key) {
         $key_col_val = addslashes($this->get_cgi_var($fd));
      }
      $fn = $this->get_cgi_var($fd);
+      if ($fn != "NULL") {
         $newValue = $newvals[$this->fds[$k]] = is_array($fn) ? join(',',$fn) : $fn;
+         $newValue = "'".addslashes($newValue)."'";
-         $newValue = addslashes($newValue);
+      } else {
+         $newValue = "NULL";
+      }         echo "do_change_record: '$fn' => '$newValue'<br>\n";
      if ($query_real == '') {
+         $query_real   = 'UPDATE '.$this->tb.' SET '.$fd.'='.$newValue;
-         $query_real   = 'UPDATE '.$this->tb.' SET '.$fd.'=\''.$newValue.'\'';
         $query_oldrec = 'SELECT '.$fd;
      } else {
+         $query_real   .= ','.$fd.'='.$newValue;
         $query_oldrec .= ','.$fd;
-         $query_real   .= ','.$fd.'=\''.$newValue.'\'';
         $query_oldrec .= ','.$fd;
      }
   }


With these modifications, the change record mode works as well.

 
bernie95     Založený: 03.09.2003   Príspevky: 10  
Príspevok Zaslal: 2003-09-06 13:22
Návrat hore  Odpovedať s citátom     

Doug Hockinson Napísal:
A possible workaround: In the lookup table, insert a dummy record with a value of .. (two periods) and then filter the form used for the lookup table to exclude the dummy entry.

Kód:
$opts['filters'] = 'ColumnName <> ".."';


If properly sorted, then .. is the first option in your dropdown selector. The filter goes in the form for the lookup table, not in the form where the lookup is made.


Regretfully this does not work so well, as you get an ID for this entry, that gets inserted in the mySQL table instead of NULL; note that for various reasons I use InnoDB tables with foreign key constraints; that's why I need to insert NULL and not some invalid ID, like 0, in the table.

Also getting such a dummy entry in the lookup table needs to special case it in all other data handling code, while a NULL is meant for that.

 
Odoslať novú tému   Odpovedať na tému    

Copyright © 2002-2006 Platon Group
Stránka používa redakčný systém Metafox
Na začiatok · Odkazový formulár · Prihláška
Upozorniť na chybu na PLATON.SK webstránke · Podmienky použitia · Ochrana osobných údajov