Re: Passing a DB object around... help please
| From: | Alan T. Miller | Date: | Thu, 26 Dec 2002 20:18:45 +0000 |
| Subject: | Re: Passing a DB object around... help please | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-11836@lists.php.net to get a copy of this message | ||
Aha, I came up with a fix to the dilemma for those interested to know.....
or for those who would like to comment on the solution and make a
suggestion.
I changed the DB_Custom class to:
<?php
require_once 'DB.php';
class DB_Custom extends DB {
// {{{
function DB_Custom()
{
$host = 'localhost';
if($_SERVER["HTTP_HOST"] == "localhost:4444") {
$user = 'root';
$pass = 'hollywood';
} else {
$user = 'vhomes';
$pass = 'veehomes69';
}
$name = 'vhomes';
$this = new DB;
$this = DB::connect( "mysql://$user:$pass@$host/$name" );
if (DB::isError($this)) {
die ($this->getDebugInfo());
}
$this->setFetchMode(DB_FETCHMODE_OBJECT);
Return;
}
// }}}
}
?>
and now call it with from indie the "Page" class with the following method.
>>> snipped from the page class
// {{{
function begin()
{
include("config.general.inc");
if($this->database) {
$this->timer->setMarker('database begin');
include("class.db.mysql.inc");
$this->db =& new DB_Custom;
// print_r($this->db);
$this->timer->setMarker('database end');
}
// begin output
include("template.header.inc");
include("template.navbar.inc");
}
// }}}
----- Original Message -----
From: "Alan T. Miller" <amiller@hollywood101.com>
To: <pear-dev@lists.php.net>
Sent: Thursday, December 26, 2002 9:38 AM
Subject: [PEAR-DEV] Passing a DB object around... help please
> 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 o
f
> 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;
> }
> // }}}
> }
> ?>
>
>
> --
> PEAR Development Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>