Passing a DB object around... help please
| From: | Alan T. Miller | Date: | Thu, 26 Dec 2002 17:38:09 +0000 |
| Subject: | Passing a DB object around... help please | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-11835@lists.php.net to get a copy of this message | ||
I am a bit confused on trying to impliment DB in a project I am working on,
any help would be appreciated.
I am trying to set up something, where I utilize three classes. They are
included below. The classes are "DB_Custom", "Page", and "Form". I
want to
be able to create an instance of the "DB_Custom" class called simply '$db'
that is instantiated when the "Page" class is called. I would the result of
this instantiation (a database connection object) to be an object of the
"Page" class. On a page where I am building a form, I first instantiate the
"Page" class. On that "Page" class, I then Instantiate the "Form"
class. I
am confused about how I can go about accessing the database connection which
is inside the "Page" class from inside the "Form" class. The code I currenty
have is included below. The current code produces an error stating that
Query() is an undefined function.
Any help or suggestions would be greatly appreciated. Thanks in advance.
Alan
Code Follows:
>>>
<?php
include("class.page.inc");
$page = new Page;
// use the database for this page
$page->database = 1;
$page->begin();
include("class.form.inc");
$form = new Form;
$country_select_field = $form->buildCountriesSelect($page->db,
$_POST['COUNTRY'], "1"));
print($country_select_field);
// class definitions
require_once 'DB.php';
class DB_Custom extends DB {
// {{{
function DB_Custom()
{
$db_host = 'somehost';
$db_user = 'someuser';
$db_pass = 'somepass';
$db_name = 'somedb';
$this->db = DB::connect(
"mysql://$db_user:$db_pass@$db_host/$db_name" );
if (DB::isError($db)) {
die ($this->db->getDebugInfo());
}
$this->db->setFetchMode(DB_FETCHMODE_OBJECT);
Return $this->db;
}
// }}}
}
class Page {
var $database;
var $db;
// {{{
function Page()
{
//constructor
include("config.general.inc");
}
// }}}
// {{{
function begin()
{
if($this->database) {
include("class.db.mysql.inc");
$this->db = new DB_Custom;
}
// begin output
include("template.header.inc");
}
// }}}
// {{{
function close()
{
include("template.footer.inc");
if($this->database) {
$this->db->disconnect();
}
}
// }}}
}
class Form {
// {{{
function Form() {
}
// }}}
// {{{
function buildCountriesSelect(&$db, $COUNTRY, $required="")
{
$sql = "SELECT * FROM country";
$res = $db->query($sql);
if (DB::isError($res)) {
die ($res->getMessage());
}
if($required == 1) {
$str .= "Country *<br>\n";
} else {
$str .= "Country<br>\n";
}
$html .= "<select name=\"COUNTRY\" size=\"1\">\n";
if ($COUNTRY == "NULL" || empty($COUNTRY) ) {
$str .= "<option value=\"NULL\" SELECTED>Please
Select</option>\n";
} else {
$str .= "<option value=\"NULL\">Please
Select</option>\n";
}
while ($r = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
if ($COUNTRY == trim($r->COUNTRY)) {
$selected = " SELECTED";
} else {
$selected = "";
}
$str .= "<option
value=\"$r->COUNTRY\"$selected>$r->COUNTRY</option>\n";
}
$str .= "\t\t</select>\n";
Return $str;
}
// }}}
}
?>