Creating a PHP Class for only Known Variables - php

Hello Fellow Stack Over Flow members, I hope you guys can help with me something that I have been unable to figure out. I have been unable to write a class for the below code, I was hoping one of you php experts could help
$obj = new ClassName ();
$obj->setName ('Name of Something');
$obj->price = 500.00;
$obj ['address_primary'] = 'First Line of Address';
$obj->address_secondary = 'Second Line of Address';
$obj->city = 'the city';
$obj->state = 'ST';
$obj->setZip (12345);
echo 'Name :: ', $obj->name, PHP_EOL;
echo 'Price :: $', $obj ['price'], PHP_EOL;
echo 'Address :: ', $obj->address_primary, ' ', $obj->getAddressSecondary (), PHP_EOL;
echo 'City, State, Zip :: ', $obj->city, ', ', $obj ['state'], ' ', $obj->getZip ();
Every time I attempt to write a class it either prints out blank or Web Storm is throwing an error about method not being declared in class.
code I used:
var $name; // House Name
var $price; // Price of House
var $address_1; // Address 1
var $address_2; // Address 2
var $city; // City
var $state; // state
var $zip; // zip
// Class Constructor
function Property($name, $price, $address_1, $address_2, $city, $state, $zip) {
$this->setName = $name;
$this->price = $price;
$this->address_primary = $address_1;
$this->address_secondary = $address_2;
$this->city = $city;
$this->state = $state;
$this->zip = $zip;
}
// Getter/Setter functions
function get_name() {
return $this->name;
}
function set_name($newname) {
$this->name = $newname;
}
function get_price() {
return $this->price;
}
function set_price($newprice) {
$this->price = $newprice;
}
function get_address_1() {
return $this->address_primary;
}
function set_address_1($newaddress_1) {
$this->address_primary = $newaddress_1;
}
function get_address_2() {
return $this->address_secondary;
}
function set_address_2($newaddress_2) {
$this->address_secondary = $newaddress_2;
}
function get_city() {
return $this->city;
}
function set_city($newcity) {
$this->city = $newcity;
}
function get_state() {
return $this->state;
}
function set_state($newstate) {
$this->state = $newstate;
}
function get_zip() {
return $this->setZip;
}
function set_zip($newzip) {
$this->setZip = $newzip;
}}
any code suggestions would greatly be appreciated!

that's kind of a big mess! :) You're mixing setters/getters and direct access, and are using old school constructors. You can clean it up really quickly.
class Property
{
public function __construct($name, $price, $address_1, $address_2, $city, $state, $zip)
{
$this->name = $name;
$this->price = $price;
$this->address_primary = $address_1;
$this->address_secondary = $address_2;
$this->city = $city;
$this->state = $state;
$this->zip = $zip;
}
}
If you set up your class like such with all your variables in the constructor, then you should be using it like:
$obj = new Property(
"Name of Something",
500,
"First Line",
"Second Line",
"City",
"State",
"90210"
);
echo 'Name :: ', $obj->name, PHP_EOL;
echo 'Price :: $', $obj->price, PHP_EOL;
echo 'Address :: ', $obj->address_primary, ' ', $obj->address_secondary, PHP_EOL;
echo 'City, State, Zip :: ', $obj->city, ', ', $obj->state, ' ', $obj->zip;
Marked issues with your code were:
You're mixing members with functions. This kind of stuff in your constructor is major breakage:
$this->setName = $price
You're also mixing substring access [] with -> and using getters on top ->getName(). Be consistent, and in this case, respect the semantic of public variables.
Lastly, you had parameters in your constructor, but weren't using it at all.
Alternative would be to assign values to your constructor signature variables by default to make them optional.
public function __construct($name = "", $price = 0, $address_1 = "", $address_2 = "", $city = "", $state = "", $zip = "" )
And then use the public member access as you had:
$obj = new Property();
$obj->name = "Some Name";
Good luck with your learning.

Related

Missing argument 1 for Product::__construct()

So I have this code:
<?php
class Product
{
public $name = 'default_name';
public $price = 0;
public $desc = 'default description';
function __construct($name, $price, $desc){
$this->name = $name;
$this->price = $price;
$this->desc = $desc;
}
public function getInfo(){
return "Product Name: " . $this->name;
}
}
$p = new Product();
$shirt = new Product("Space Juice T-Shirt", 20, "Awesome Grey T-Shirt");
$soda = new Product("Space Juice Soda", 2, "Grape Flavored Thirst Mutilator");
echo $shirt->getInfo();
?>
and PHP reports "Missing argument 1 for Product::__construct()" error. I got this example in one of the leading PHP courses and I'm confused because there seems to be an error somewhere inside this simple code. Help would be much appriciated.
In your code, if you are going to create an instance of Product, passing the parameters are mandatory. Whereas, you already have the default values. So, Make it safe:
<?php
class Product
{
public $name = 'default_name';
public $price = 0;
public $desc = 'default description';
function __construct($name = null, $price = null, $desc = null){
$this->name = $name ?: $this->name;
$this->price = $price ?: $this->price;
$this->desc = $desc ?: $this->desc;
}
public function getInfo(){
return "Product Name: " . $this->name;
}
}
$p = new Product();
$shirt = new Product("Space Juice T-Shirt", 20, "Awesome Grey T-Shirt");
$soda = new Product("Space Juice Soda", 2, "Grape Flavored Thirst Mutilator");
echo $shirt->getInfo();
?>
Your construct doesn't allow nulls for your variables, and so
$p = new Product();
is causing the problem. It's expecting some value, even if it's just empty strings, for $name, $price and $desc.
If you change your construct function to this:
function __construct($name = 'default_name', $price = 0, $desc = 'default description'){
$this->name = $name;
$this->price = $price;
$this->desc = $desc;
}
then it shouldn't throw that error any more.

PHP toString not printing

I keep getting the following error while trying to using __toString():
Catchable fatal error: Object of class Address could not be converted
to string in C:wamp\www\demo.php on line 36
Where did I go wrong?
demo.php
echo '<h2>Instantiating Address</h2>';
$address = new Address;
echo '<h2>Empty Address</h2>';
echo '<tt><pre>' . var_export($address, TRUE) . '</pre></tt>';
echo '<h2>Setting properties...</h2>';
$address->street_address_1 = '555 Fake Street';
$address->city_name = 'Townsville';
$address->subdivision_name = 'State';
$address->postal_code = '12345';
$address->country_name = 'United States of America';
echo '<tt><pre>' . var_export($address, TRUE) . '</pre></tt>';
echo '<h2>Displaying address ...</h2>';
echo $address->display();
echo '<h2>Testing magic __get and __set</h2>';
unset($address->postal_code);
echo $address->display();
echo '<h2>Testing Address __construct with an array</h2>';
$address_2 = new Address(array(
'street_address_1' => '123 Phony Ave',
'city_name' => 'Villageland',
'subdivision_name' => 'Region',
'postal_code' => '67890',
'country_name' => 'Canada',
));
echo $address_2->display();
echo '<h2>Address __toString</h2>';
echo $address_2;
?>
oop.php
<!DOCTYPE html>
<html>
<head><title>OOP</title></head>
<body>
<?php
class Address {
public $street_address_1;
public $street_address_2;
public $city_name;
public $subdivision_name;
public $country_name;
protected $_postal_code;
// Primary key of an address
protected $_address_id;
// When the recorded created and last updated
protected $_time_created;
protected $_time_updated;
/*
Constructor
#param array data optional array of property names
*/
function __construct($data = array()) {
$this->_time_created = time();
// Ensure that the address can be populated.
if(!is_array($data)){
trigger_error('Unable to construct address with a ' . get_class($name));
}
// If there is at lease one value, populate the Address with it
if(count($data) > 0) {
foreach($data as $name => $value){
// Special case for protected properties
if(in_array($name, array(
'time_created',
'time_updated',
))){
$name = '_' . $name;
}
$this->$name = $value;
}
}
}
/*
Magic __get
#param string $name
#return mixed
*/
function __get($name){
// Postal code lookup if unset.
if (!$this->_postal_code){
$this->_postal_code = $this->_postal_code_guess();
}
/*
Magin __set.
#param string $name
#param mixed $value
*/
function __set($name, $value){
// Allow anything to set the postal code.
if ('postal_code' == $name){
$this->$name = $value;
return;
}
// Unable to access property; trigger error,
trigger_error('Undefined or unallowed property via __set(): ' . $name);
}
/*
Magic __toString
#return string
*/
function __toString() {
return $this->display();
}
$protected_property_name = '_' . $name;
if(property_exists($this, $protected_property_name)){
return $this->$protected_property_name;
}
// unable to access property; trigger error
trigger_error('Undefined_property via __get: ' . $name);
return NULL;
}
/*
Guess the postal code given the subdivision and city name.
#todo Replace with a database lookup
#return string
*/
protected function _postal_code_guess(){
return 'LOOKUP';
}
/*
display an address in HTML
return string.
*/
function display(){
$output = '';
// Street address
$output .= $this->street_address_1;
if ($this->street_address_2){
$output .= '<br />' . $this->Street_address_2;
}
// City, Subdivision Postal
$output .= '<br />';
$output .= $this->city_name . ', ' . $this->subdivision_name;
$output .= ' ' . $this->postal_code;
// Country
$output .= '<br />';
$output .= $this->country_name;
return $output;
}
}
?>
</body>
</html>
function __get() isn't properly closed. I`ve removed some code to make it work. Check for differences.
<?php
class Address {
public $street_address_1;
public $street_address_2;
public $city_name;
public $subdivision_name;
public $country_name;
protected $_postal_code;
// Primary key of an address
protected $_address_id;
// When the recorded created and last updated
protected $_time_created;
protected $_time_updated;
/*
Constructor
#param array data optional array of property names
*/
function __construct($data = array()) {
$this->_time_created = time();
// Ensure that the address can be populated.
if(!is_array($data)){
trigger_error('Unable to construct address with a ' . get_class($name));
}
// If there is at lease one value, populate the Address with it
if(count($data) > 0) {
foreach($data as $name => $value){
// Special case for protected properties
if(in_array($name, array(
'time_created',
'time_updated',
))){
$name = '_' . $name;
}
$this->$name = $value;
}
}
}
/*
Magic __get
#param string $name
#return mixed
*/
function __get($name){
// Postal code lookup if unset.
if (!$this->_postal_code){
$this->_postal_code = $this->_postal_code_guess();
}
$protected_property_name = '_' . $name;
if(property_exists($this, $protected_property_name)){
return $this->$protected_property_name;
}
// unable to access property; trigger error
trigger_error('Undefined_property via __get: ' . $name);
return NULL;
}
/*
Magin __set.
#param string $name
#param mixed $value
*/
function __set($name, $value){
// Allow anything to set the postal code.
if ('postal_code' == $name){
$this->$name = $value;
return;
}
// Unable to access property; trigger error,
trigger_error('Undefined or unallowed property via __set(): ' . $name);
}
/*
Magic __toString
#return string
*/
function __toString() {
return $this->display();
}
/*
Guess the postal code given the subdivision and city name.
#todo Replace with a database lookup
#return string
*/
protected function _postal_code_guess(){
return 'LOOKUP';
}
/*
display an address in HTML
return string.
*/
function display(){
$output = '';
// Street address
$output .= $this->street_address_1;
if ($this->street_address_2){
$output .= '<br />' . $this->Street_address_2;
}
// City, Subdivision Postal
$output .= '<br />';
$output .= $this->city_name . ', ' . $this->subdivision_name;
$output .= ' ' . $this->postal_code;
// Country
$output .= '<br />';
$output .= $this->country_name;
return $output;
}
}

How can I add a break within two values in php?

I got this code
$title = new FullName($reservation->Title, $reservation->Description)
which shows the values Title and Description inside a box, but it does so directly following each other. When the box is too small it does a line break, but only at the exact point of the end of the box. so how can i force a line break between $reservation->Title and $reservation->Description ?
Here is the Full Name Class
class FullName
{
/**
* #var string
*/
private $fullName;
public function __construct($firstName, $lastName)
{
$formatter = Configuration::Instance()->GetKey(ConfigKeys::NAME_FORMAT);
if (empty($formatter))
{
$this->fullName = "$firstName $lastName";
}
else
{
$this->fullName = str_replace('{first}', $firstName, $formatter);
$this->fullName = str_replace('{last}', $lastName, $this->fullName);
}
}
public function __toString()
{
return $this->fullName;
}
}
Not a good way without proper explanation, but a Quick solution
Replace
$title = new FullName($reservation->Title, $reservation->Description)
with
$t = $reservation->Title . "<br />";
$d = $reservation->Description;
$title = new FullName($t, $d);
HTML line break can be inserted as:
$this->fullName = $firstName . '<br />' . $lastName
or with generic (non-HTML) new-line character:
$this->fullName = $firstName . "\n" . $lastName
It is important to use double quotes in last case (").
See the link for working example: http://codepad.viper-7.com/qS7nNv
You can add a third parameter to the class.
class FullName
{
/**
* #var string
*/
private $fullName;
public function __construct($firstName, $lastName, $delimiter = null)
{
$formatter = Configuration::Instance()->GetKey(ConfigKeys::NAME_FORMAT);
if (empty($formatter))
{
if($delimiter) {
$this->fullName = "$firstName $delimiter $lastName";
} else {
$this->fullName = "$firstName $lastName";
}
}
else
{
$this->fullName = str_replace('{first}', $firstName, $formatter);
$this->fullName = str_replace('{last}', $lastName, $this->fullName);
}
}
public function __toString()
{
return $this->fullName;
}
}
then add the delimiter:
$title = new FullName($reservation->Title, $reservation->Description, "<br />");

PHP Error when using variable variable to insert data into static variable

I'm not very good at this, so I'm sure this is a stupid question.
I have a class:
class debug {
private static $messages = array();
private static $errors = array();
private static $all = array(); // includes both of above
private static $types = array('messages','errors');
public static function add($type, $message) {
if(!in_array($type,self::$types) ) {
self::add('errors','Bad type "' . $type . '" specified when sending this message: ' . $message);
return false;
}
self::$$type[] = $message; // ERROR IS HERE (see below)
self::$all[] = $message; // no error
}
}
I'm calling this from another class in order to debug (Surprise).
debug::add('error', 'Error in ' . __FILE__ . ' on line ' . __LINE__);
PHP error message from error.log:
PHP Fatal error: Cannot use [] for reading in /var/www/lib/lib.php on line 1248
It refers to the above-specified line in the debug class.
EDIT:
What I am trying to do is use a variable variable (hence the posting title) to determine which static array to add data to.
I.e. if $type == 'messages', then $$type == $messages.
So I want self::$$type[] == self::$messages[]
Or if $type == 'errors', then $$type == $errors and self::$$type[] == self::$errors[]
Change the following line to. This ensures that $type is evaluated into 'message' or 'error' first.
self::${$type}[] = $message;
To expand on this, this is the code that I have. There seems to be additional syntax errors in your code that is causing the other failures, but this is why $$type[] is giving you that error.
class debug {
public static $messages = array();
public static $errors = array();
public static $all = array(); // includes both of above
private static $types = array('messages','errors');
public static function add($type, $message) {
self::${$type}[] = $message;
self::$all[] = $text;
}
}
debug::add('messages', "Regular Message");
debug::add('errors', "Error Message");
print_r(debug::$messages);
print_r(debug::$errors);
And this is the output that I get
Array
(
[0] => Regular Message
)
Array
(
[0] => Error Message
)
2 Errors
A. if(!in_array($type,self::$types) ) { not properly closed .. you used ) insted of } at the end
B. self::$all[] = $text; $text not defined anywhere in the script
Try
class Debug {
private static $errors = array ();
private static $types = array (
'messages',
'errors'
);
public static function add($type, $message) {
if (! in_array ( $type, self::$types )) {
return false;
}
self::$errors [$type][] = $message; // results in error (see below)
}
public static function get($type = null) {
if (! in_array ( $type, self::$types ) && $type !== null) {
return false;
}
return ($type === null) ? self::$errors : self::$errors [$type] ;
}
}
debug::add ( 'errors', 'Error in ' . __FILE__ . ' on line ' . __LINE__ );
debug::add ( 'messages', 'Error in ' . __FILE__ . ' on line ' . __LINE__ );
var_dump(debug::get());
var_dump(debug::get("messages"));

Call to a member function on a non-object (first try of writing OOP) [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
The class below is my first attempt at writing my own OOP application. I've used procedural for a while and the OO techniques are not coming as easily as I'd hoped.
The class is designed to put together input elements for HTML forms, optionally using SQL table records. I'm starting with the select box and will add more when I get this much working.
So the problem is that I'm getting
"Call to a member function get_table() on a non-object" on the 'public function get_table()' line of the Class code below.
I'm not sure why this is happening. Help/tips would be GREATLY appreciated.
and now the code:
Application:
$_input = new html_form_input();
$_input->set_input_type('select');
$_input->set_table('stores');
$_input->set_fieldname_id('store_id');
$_input->set_fieldname_desc('store_name');
$_input->set_sql_order(' ORDER BY store_name ASC ');
$_input->set_select();
$html_select_facility = $_input->get_select();
Class:
class html_form_input
{
public $input_type;
public $table;
public $fieldname_id;
public $fieldname_desc;
public $passed_id;
public $sql_where;
public $sql_order;
public $array_input_options;
public function __construct()
{
// constructor method
}
/*
setters
*/
public function set_input_type($input_type)
{
$this->input_type = $input_type;
}
public function set_array_input_options($array_input_options)
{
$this->array_input_options = $array_input_options;
}
public function set_table($table)
{
$this->table = $table;
}
public function set_fieldname_id($fieldname_id)
{
$this->fieldname_id = $fieldname_id;
}
public function set_fieldname_desc($fieldname_desc)
{
$this->fieldname_desc = $fieldname_desc;
}
public function set_passed_id($passed_id)
{
$this->passed_id = $passed_id;
}
public function set_sql_where($sql_where)
{
$this->sql_where = $sql_where;
}
public function set_sql_order($sql_order)
{
$this->sql_order = $sql_order;
}
/*
getters
*/
public function get_input_type()
{
return $this->$input_type;
}
public function get_array_input_options()
{
return $this->$array_input_options;
}
public function get_table()
{
return $this->$table;
}
public function get_fieldname_id()
{
return $this->$fieldname_id;
}
public function get_fieldname_desc()
{
return $this->$fieldname_desc;
}
public function get_passed_id()
{
return $this->$passed_id;
}
public function get_sql_where()
{
return $this->$sql_where;
}
public function get_sql_order()
{
return $this->$sql_order;
}
/*
set_query_form_data() queries the database for records to be used in the input element.
*/
public function set_query_form_data()
{
global $dbx;
$debug = true;
$_debug_desc = "<span style='color:blue;'>function</span> <b>set_query_form_data</b>()";
if ($debug) { echo "<p>BEGIN $_debug_desc <blockquote>"; }
$table->get_table();
$fieldname_id->get_fieldname_id();
$fieldname_desc->get_fieldname_desc();
$passed_id->get_passed_id();
$sql_where->get_sql_where();
$sql_order->get_sql_order();
if ($passed_id)
{
if (!is_array($passed_id))
{
$passed_id[] = $passed_id;
}
}
if ($sql_where!='')
{
$sql_where = " WHERE $sql_where ";
}
$q = "
SELECT
$fieldname_id,
$fieldname_desc
FROM
$table
$sql_where
$sql_order
";
$res = $mdb2_dbx->query($q);
if (PEAR::isError($res)) { gor_error_handler($res, $q, __LINE__,__FILE__,'die'); }
while ( $r = $res->fetchRow(MDB2_FETCHMODE_ASSOC) )
{
$id = $r[$fieldname_id];
$desc = $r[$fieldname_desc];
$array_values[$id] = $desc;
}
$this->sql_array_values = $array_values;
if ($debug) { echo "<p></blockquote>END $_debug_desc "; }
}
/*
getter for set_query_form_data (above)
*/
public function get_query_form_data()
{
return $this->$array_values;
}
/*
set_select() pieces together a select input element using database derived records, or a passed array of values.
*/
public function set_select($flag_query_db=1, $array_static_values=null)
{
if ($flag_query_db==1)
{
$array_values = $this->set_query_form_data();
} else if (is_array($array_static_values)) {
$array_values = $array_static_values;
}
$array_values = $array_data['row_data'];
$fieldname_id = $array_data['fieldname_id'];
$fieldname_desc = $array_data['fieldname_desc'];
$passed_id = $array_data['passed_id'];
if (!is_array($passed_id))
{
$passed_id[] = $passed_id;
}
foreach ($array_values as $id=>$desc)
{
// handle passed values (multiple or single)
$sel = null;
if (in_array($id,$passed_id))
{
$sel = ' selected ';
}
// construct html
$html_options .= " <option value='$id' $sel>$desc</option>\n";
}
$disabled = null;
$multiple = null;
$size = null;
$style = null;
$class = null;
$element_id = null;
$javascript = null;
if (is_array($array_input_options))
{
$s_disabled = $array_input_options['disabled'];
$s_multiple = $array_input_options['multiple'];
$s_size = $array_input_options['size'];
$s_style = $array_input_options['style'];
$s_id = $array_input_options['id'];
$s_class = $array_input_options['class'];
$s_javascript = $array_input_options['javascript'];
if ($s_disabled!='') {$disabled = ' disabled '; }
if ($s_multiple!='') {$multiple = ' multiple '; }
if ($s_size!='') {$size = ' size="' . $s_size . '"'; }
if ($s_style!='') {$style = ' style = "' . $s_style . '"'; }
if ($s_id!='') {$element_id = ' id = "' . $s_id . '"'; }
if ($s_class!='') {$class = ' class = "' . $s_class . '"'; }
if ($s_javascript!='') {$javascript = $s_javascript; }
}
$html = "
<select name='$fieldname_id' $element_id $disabled $multiple $size $style $class $javascript>
$html_options
</select>
";
$this->select_html = $html;
}
/*
getter for set_select (above)
*/
public function get_select()
{
return $this->$select_html;
}
}
With your getters, instead of using $this->$var it should be $this->var, for example $this->table and not $this->$table.
In the following code, $table hasn't been initialised.
public function set_query_form_data()
{
global $dbx;
$debug = true;
$_debug_desc = "<span style='color:blue;'>function</span> <b>set_query_form_data</b>()";
if ($debug) { echo "<p>BEGIN $_debug_desc <blockquote>"; }
$table->get_table();
You probably intend it to be $this, i.e. "the object currently being used":
public function set_query_form_data()
{
global $dbx;
$debug = true;
$_debug_desc = "<span style='color:blue;'>function</span> <b>set_query_form_data</b>()";
if ($debug) { echo "<p>BEGIN $_debug_desc <blockquote>"; }
$this->get_table();
The problem is in your set_query_form_data method:
public function set_query_form_data() {
// $table is no object
$table->get_table();
// you should use this instead
$this->table
}
Note:
// Are you sure with this calls? Shouldn't it be $this->array_input_options ?
return $this->$array_input_options;
You're making use of variable functions/dereferencing, unintentionally. Example:
$foo = 'name';
echo $object->$foo; // same as echo $object->name
Object properties and methods do not need to be prefixed with $.
With the pointers from the other answers and some more RTM, I got the basic script working. In particular, I removed the "$" from property names and accessed the properties of $this instead of calling get_* methods.
Application:
$array_input_options = array(
'include_blank_option' => 1,
'disabled' => 0,
'multiple' => 0,
'size' => '',
'style' => '',
'id' => '',
'class' => '',
'javascript' => '',
);
$_input = new html_form_input();
$_input->set_input_type('select');
$_input->set_table('gor_facility');
$_input->set_fieldname_id('facilityid');
$_input->set_fieldname_desc('facilityname');
$_input->set_sql_where(' status = 1');
$_input->set_sql_order(' ORDER BY facilityname ASC ');
$_input->set_array_input_options($array_input_options);
// $_input->set_passed_id('');
$html_select_facility = $_input->create_select();
Class:
class html_form_input
{
public $input_type;
public $table;
public $fieldname_id;
public $fieldname_desc;
public $passed_id;
public $sql_where;
public $sql_order;
public $array_input_options;
public function __construct()
{
// constructor method
}
/*
setters
*/
public function set_input_type($input_type)
{
$this->input_type = $input_type;
}
public function set_array_input_options($array_input_options)
{
$this->array_input_options = $array_input_options;
}
public function set_table($table)
{
$this->table = $table;
}
public function set_fieldname_id($fieldname_id)
{
$this->fieldname_id = $fieldname_id;
}
public function set_fieldname_desc($fieldname_desc)
{
$this->fieldname_desc = $fieldname_desc;
}
public function set_passed_id($passed_id)
{
$this->passed_id = $passed_id;
}
public function set_sql_where($sql_where)
{
$this->sql_where = $sql_where;
}
public function set_sql_order($sql_order)
{
$this->sql_order = $sql_order;
}
/*
getters
*/
public function get_input_type()
{
return $this->input_type;
}
public function get_array_input_options()
{
return $this->array_input_options;
}
public function get_table()
{
return $this->table;
}
public function get_fieldname_id()
{
return $this->fieldname_id;
}
public function get_fieldname_desc()
{
return $this->fieldname_desc;
}
public function get_passed_id()
{
return $this->passed_id;
}
public function get_sql_where()
{
return $this->sql_where;
}
public function get_sql_order()
{
return $this->sql_order;
}
/*
set_query_form_data() queries the database for records to be used in the input element.
*/
public function set_query_form_data()
{
global $mdb2_dbx;
$debug = false;
$_debug_desc = "<span style='color:blue;'>function</span> <b>set_query_form_data</b>()";
if ($debug) { echo "<p>BEGIN $_debug_desc <blockquote>"; }
$table = $this->table;
$fieldname_id = $this->fieldname_id;
$fieldname_desc = $this->fieldname_desc;
$passed_id = $this->passed_id;
$sql_where = $this->sql_where;
$sql_order = $this->sql_order;
if ($passed_id)
{
if (!is_array($passed_id))
{
$passed_id[] = $passed_id;
}
}
if ($sql_where!='')
{
$sql_where = " WHERE $sql_where ";
}
$q = "
SELECT
$fieldname_id,
$fieldname_desc
FROM
$table
$sql_where
$sql_order
";
if ($debug) {echo "<p>$q<br>";}
$res = $mdb2_dbx->query($q);
if (PEAR::isError($res)) { gor_error_handler($res, $q, __LINE__,__FILE__,'die'); }
while ( $r = $res->fetchRow(MDB2_FETCHMODE_ASSOC) )
{
$id = $r[$fieldname_id];
$desc = $r[$fieldname_desc];
$array_values[$id] = $desc;
}
$this->sql_array_values = $array_values;
if ($debug) { echo "<p></blockquote>END $_debug_desc "; }
}
/*
getter for set_query_form_data (above)
*/
public function get_query_form_data()
{
return $this->sql_array_values;
}
/*
set_select() pieces together a select input element using database derived records, or a passed array of values.
*/
public function construct_select($flag_query_db=1, $array_static_values=null)
{
if ($flag_query_db==1)
{
$this->set_query_form_data();
$row_data = $this->sql_array_values;
} else if (is_array($array_static_values)) {
$row_data = $array_static_values;
}
$fieldname_id = $this->fieldname_id;
$fieldname_desc = $this->fieldname_desc;
$passed_id = $this->passed_id;
$array_input_options = $this->array_input_options;
if (!is_array($passed_id))
{
$passed_id[] = $passed_id;
}
$disabled = null;
$multiple = null;
$size = null;
$style = null;
$class = null;
$element_id = null;
$javascript = null;
$html_option_blank = null;
if (is_array($array_input_options))
{
$s_disabled = $array_input_options['disabled'];
$s_multiple = $array_input_options['multiple'];
$s_size = $array_input_options['size'];
$s_style = $array_input_options['style'];
$s_id = $array_input_options['id'];
$s_class = $array_input_options['class'];
$s_javascript = $array_input_options['javascript'];
$s_blank = $array_input_options['include_blank_option'];
if ($s_disabled!='') {$disabled = ' disabled '; }
if ($s_multiple!='') {$multiple = ' multiple '; }
if ($s_size!='') {$size = ' size="' . $s_size . '"'; }
if ($s_style!='') {$style = ' style = "' . $s_style . '"'; }
if ($s_id!='') {$element_id = ' id = "' . $s_id . '"'; }
if ($s_class!='') {$class = ' class = "' . $s_class . '"'; }
if ($s_javascript!='') {$javascript = $s_javascript; }
if ($s_blank==1) { $row_data = array(''=>'Select an option below:') + $row_data; }
}
if (is_array($row_data))
{
foreach ($row_data as $id=>$desc)
{
// handle passed values (multiple or single)
$sel = null;
if (in_array($id,$passed_id))
{
$sel = ' selected ';
}
// construct html
$html_options .= " <option value='$id' $sel>$desc</option>\n";
}
}
$html = "
<select name='$fieldname_id' $element_id $disabled $multiple $size $style $class $javascript>
$html_option_blank
$html_options
</select>
";
$this->select_html = $html;
}
/*
getter for set_select (above)
*/
public function create_select()
{
$this->construct_select();
return $this->select_html;
}
}

Categories