PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/fields/class.dbListField.php

https://github.com/reshadf/Library
PHP | 82 lines | 39 code | 10 blank | 33 comment | 4 complexity | ff6246b046c256b1fdb04782440721c7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class dbListField
  4. *
  5. * Create a listfield from records retrieved from the db
  6. *
  7. * @author Teye Heimans
  8. * @package FormHandler
  9. * @subpackage Fields
  10. */
  11. class dbListField extends ListField
  12. {
  13. /**
  14. * dbListField::dbListField()
  15. *
  16. * Create a new dbListField object
  17. *
  18. * @param object &$oForm: the form where the datefield is located on
  19. * @param string $sName: the name of the datefield
  20. * @param object $oDb: object of the database handler
  21. * @param string $sTable: the table to get the fields from
  22. * @param mixed $mFields: array of string with the names of the fields which data we should get
  23. * @param string $sExtraSQL: extra SQL statements
  24. * @return dbListField
  25. * @access public
  26. * @author Teye Heimans
  27. */
  28. function dbListField( &$oForm, $sName, &$oDb, $sTable, $mFields, $sExtraSQL = null )
  29. {
  30. // make sure that the fields are set in an array
  31. $aFields = !is_array($mFields) ? array( $mFields ) : $mFields;
  32. // generate the query to retrieve the records
  33. $sQuery =
  34. 'SELECT '. implode(', ', $aFields).
  35. ' FROM '. $oDb->quote( $sTable).' '.$sExtraSQL;
  36. // get the records and load the options
  37. $aOptions = array();
  38. // execute the query
  39. $sql = $oDb->query( $sQuery );
  40. // query succeeded?
  41. if( $sql )
  42. {
  43. // fetch the results
  44. while( $row = $oDb->getRecord( $sql ) )
  45. {
  46. if( sizeof( $row ) == 1 )
  47. {
  48. $aOptions[] = array_shift( $row );
  49. }
  50. else
  51. {
  52. $aOptions[array_shift( $row )] = array_shift( $row );
  53. }
  54. }
  55. }
  56. // query failed
  57. else
  58. {
  59. trigger_error(
  60. "Error, could not retrieve records.<br '. FH_XHTML_CLOSE .'>\n".
  61. "Error message: ". $oDb->getError()."<br '. FH_XHTML_CLOSE .'>\n".
  62. "Query: ". $sQuery,
  63. E_USER_WARNING
  64. );
  65. }
  66. // call the constructor of the listfield with the new options
  67. parent::ListField( $oForm, $sName, $aOptions );
  68. // if two fields are given, use the first field as value
  69. $this->useArrayKeyAsValue( sizeof( $aFields) == 2 );
  70. }
  71. }
  72. ?>