Library /lib/fields/class.dbListField.php

Language PHP Lines 82
MD5 Hash ff6246b046c256b1fdb04782440721c7
Repository https://github.com/reshadf/Library.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
 * class dbListField
 *
 * Create a listfield from records retrieved from the db
 *
 * @author Teye Heimans
 * @package FormHandler
 * @subpackage Fields
 */

class dbListField extends ListField
{

    /**
     * dbListField::dbListField()
     *
     * Create a new dbListField object
     *
     * @param object &$oForm: the form where the datefield is located on
     * @param string $sName: the name of the datefield
     * @param object $oDb: object of the database handler
     * @param string $sTable: the table to get the fields from
     * @param mixed $mFields: array of string with the names of the fields which data we should get
     * @param string $sExtraSQL: extra SQL statements
     * @return dbListField
     * @access public
     * @author Teye Heimans
     */
    function dbListField( &$oForm, $sName, &$oDb, $sTable, $mFields, $sExtraSQL = null )
    {
		// make sure that the fields are set in an array
		$aFields = !is_array($mFields) ? array( $mFields ) : $mFields;

		// generate the query to retrieve the records
		$sQuery =
		  'SELECT '. implode(', ', $aFields).
		  ' FROM '. $oDb->quote( $sTable).' '.$sExtraSQL;

		// get the records and load the options
		$aOptions = array();

		// execute the query
		$sql = $oDb->query( $sQuery );

		// query succeeded?
		if( $sql )
		{
		    // fetch the results
    		while( $row = $oDb->getRecord( $sql ) )
    		{

    			if( sizeof( $row ) == 1 )
    			{
    				$aOptions[] = array_shift( $row );
    			}
    			else
    			{
    		        $aOptions[array_shift( $row )] = array_shift( $row );
    		    }
    		}
		}
		// query failed
		else
		{
		    trigger_error(
		      "Error, could not retrieve records.<br '. FH_XHTML_CLOSE .'>\n".
		      "Error message: ". $oDb->getError()."<br '. FH_XHTML_CLOSE .'>\n".
		      "Query: ". $sQuery,
		      E_USER_WARNING
		    );
		}

		// call the constructor of the listfield with the new options
		parent::ListField( $oForm, $sName, $aOptions );

		// if two fields are given, use the first field as value
		$this->useArrayKeyAsValue( sizeof( $aFields) == 2 );
    }
}

?>
Back to Top