How can I add a break within two values in php? - 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 />");

Related

Need to identify an administrator with PHP and PDO

I do a project of an classified advertisements website, with PHP, Twig, SQL and PDO.
I want to put an elseif in the main.php (last part of code) during the users identification, and check if the user is an admin or not. My Admins value is a boolean in my table. I don't know how to do this checking.
First my user class :
<?php
class Utilisateur
{
private $ID_UTILISATEUR;
private $MDP_UTILISATEUR;
private $MAIL_UTILISATEUR;
private $NOM_UTILISATEUR;
private $PRENOM_UTILISATEUR;
private $ADMINS;
//CONSTRUCTEUR
public function __construct($id_user = -1, $user_pass = null, string $user_mail = null, string $nom = null, string $prenom = null, $admins = null)
{
$this->ID_UTILISATEUR = $id_user;
$this->MDP_UTILISATEUR = $user_pass;
$this->MAIL_UTILISATEUR = $user_mail;
$this->NOM_UTILISATEUR = $nom;
$this->PRENOM_UTILISATEUR = $prenom;
$this->ADMINS = $admins;
}
//GETTERS
public function getID_UTILISATEUR()
{
return $this->ID_UTILISATEUR;
}
public function getMDP_UTILISATEUR()
{
return $this->MDP_UTILISATEUR;
}
public function getMAIL_UTILISATEUR()
{
return $this->MAIL_UTILISATEUR;
}
public function getNOM_UTILISATEUR()
{
return $this->NOM_UTILISATEUR;
}
public function getPRENOM_UTILISATEUR()
{
return $this->PRENOM_UTILISATEUR;
}
public function getADMINS()
{
return $this->ADMINS;
}
//SETTERS
public function setID_UTILISATEUR($ID_UTILISATEUR)
{
$this->ID_UTILISATEUR = $ID_UTILISATEUR;
}
public function setMDP_UTILISATEUR($MDP_UTILISATEUR)
{
$this->MDP_UTILISATEUR = $MDP_UTILISATEUR;
}
public function setMAIL_UTILISATEUR($MAIL_UTILISATEUR)
{
$this->MAIL_UTILISATEUR = $MAIL_UTILISATEUR;
}
public function setNOM_UTILISATEUR($NOM_UTILISATEUR)
{
$this->NOM_UTILISATEUR = $NOM_UTILISATEUR;
}
public function setPRENOM_UTILISATEUR($PRENOM_UTILISATEUR)
{
$this->PRENOM_UTILISATEUR = $PRENOM_UTILISATEUR;
}
public function setADMINS($ADMINS)
{
$this->ADMINS = $ADMINS;
}
//FONCTION stringID
public function stringID()
{
return "[" . $this->ID_UTILISATEUR . "]";
}
//FONCTION __toString
public function __toString()
{
return $this->ID_UTILISATEUR . ", " . $this->NOM_UTILISATEUR . ", " . $this->MAIL_UTILISATEUR;
}
}
Then my DAO for getting the data of a user :
public function identifier(Utilisateur $u)
{
try
{
$n = $u->getMAIL_UTILISATEUR();
$p = $u->getMDP_UTILISATEUR();
$requete = $this->cnx->prepare("SELECT ID_UTILISATEUR, MDP_UTILISATEUR, MAIL_UTILISATEUR, ADMINS FROM utilisateur WHERE MAIL_UTILISATEUR=:mail and MDP_UTILISATEUR=SHA2(:pass,224)");
$requete->bindParam(':mail', $n);
$requete->bindParam(':pass', $p);
$requete->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Utilisateur');
// exécution et récupération des résultats
$data2 = $requete->execute();
$data2 = $requete->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Utilisateur');
return $data2;
}
catch (PDOException $e)
{
throw new BDDException ("Impossible d'identifier les données", 201);
}
}
and the main.php function identify users :
function identifierUtilisateur()
{
require_once dirname(__DIR__) . "/DAO/MySQLUtilisateurDAO.php";
require_once dirname(__DIR__) . "/views/VueLogin.php";
$login = new MySQLUtilisateurDAO();
$login1 = new Utilisateur("", $_POST['mdp'], $_POST['mail'], "", "", "");
$l = $login->identifier($login1);
if (empty($l)) {
echo 'utilisateur non identifié';
$url = $_SERVER["PHP_SELF"];
echo ('<a href=' . $url . '>return</a>');
}
else {
$_SESSION['identification'] = $l;
$mail = $_POST['mail'];
$mdp = $_POST['mdp'];
setcookie("MAIL_UTILISATEUR", $mail, time() + 864000, '/'); // expire après 10 jours
setcookie("MDP_UTILISATEUR", $mdp, time() + 864000, '/');
$url = $_SERVER["PHP_SELF"];
afficherAnnoncesUtilisateur();
}
}

Can't solve 'Invalid arguments passed'

Why do I get this error?
Warning: implode(): Invalid arguments passed in /Applications/XAMPP/xamppfiles/htdocs/basis/php/php.php on line 17
index.php:
<?php
require_once 'php.php';
$piet = new Persoon();
$piet->voornaam = 'Piet';
$piet->achternaam = 'Jansen';
echo "De naam is: " . $piet->showNaam();
$piet->addHobby('zeilen');
$piet->addHobby('hardlopen');
echo "<br/> De hobbies van {$piet->showNaam()} zijn: {$piet->showHobbies()}";
?>
php.php
<?php
class Persoon {
public $voornaam = '';
public $achternaam = '';
protected $adres;
protected $hobbies;
public function showNaam() {
return $this->voornaam . ' ' . $this->achternaam;
}
public function addHobby($hobby) {
$hobbies[] = $hobby;
}
public function showHobbies() {
echo implode(', ', $this->hobbies);
}
}
?>
In addHobby() method, you must be used $this->hobbies instead of $hobbies. It's better to initialize hobbies with empty array to prevent error.
<?php
class Persoon {
public $voornaam = '';
public $achternaam = '';
protected $adres;
protected $hobbies = array();
public function showNaam() {
return $this->voornaam . ' ' . $this->achternaam;
}
public function addHobby($hobby) {
$this->hobbies[] = $hobby;
}
public function showHobbies() {
echo implode(', ', $this->hobbies);
}
}
?>
Variable access is wrong.
<?php
class Persoon {
public $voornaam = '';
public $achternaam = '';
protected $adres;
protected $hobbies;
public function showNaam() {
return $this->voornaam . ' ' . $this->achternaam;
}
public function addHobby($hobby) {
$this->hobbies[] = $hobby; <--- change this
}
public function showHobbies() {
//echo implode(', ', $this->hobbies);// remove this
echo count($this->hobbies) ? implode(', ', $this->hobbies) : "";// this will avoid errors in future if your array is empty.
}
}
?>
Your code is creating a new array everytime you call addHobby($hobby) function , what you need to do is to access it correctly . Change
public function addHobby($hobby) {
$hobbies[] = $hobby;
}
to
public function addHobby($hobby) {
$this->hobbies[] = $hobby;
}

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;
}
}

Creating a PHP Class for only Known Variables

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.

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