|
Posted by news on 10/08/43 11:27
Carl wrote:
> news@celticbear.com wrote:
> > Well, I wrote my first PHP class today. Yeah!
> > But to get it to work, in each function within the class I have to
> > repeat the database connection lines, and that just seems redundant;
> > there has to be a better way that I'm just not bright enough to think
> > of.
> >
> > Any suggestions?
> My suggestions follow.
>
>
> Note however that although you did indeed create a class, this class
> functions more like a collection of functions than an object. I find
> that a good place to start when designing classes/objects is to assign
> it an object name, something that describes what it is in basic terms.
> Using this name you can then get an idea of the behavior (methods)
> expected of this object and the attributes (variable) that the object
> must have.
>
> You can also clean up the code quite a bit by moving the database
> specific code into the userDBconnect code. Ideally, this class should
> know as little about the database implementation as possible..
Holy mother of God! The power!
Inspired by your example, I turned that collection of functions (which
ended up being several of nearly the same function,) into this:
include('../Connections/userDBconnect.php');
$db = mysql_select_db("printing", $connection) or $error .= "Could not
select database1: " . mysql_error()."<p />";
$dbconn = $connection;
class class_pa_email {
function mail_sql($sql_n="0") {
$sql = "SELECT email FROM tbl_users WHERE active='1'";
if ($sql_n != "0") {
$sql .= " AND dept = '$sql_n'";
}
$result = @mysql_query($sql) or $error .= "Could not query1: " .
mysql_error()."<p />";
$count = mysql_num_rows($result) or $error .= "Could not select
query2: " . mysql_error()."<p />";
$result_array = array($result,$count);
return $result_array;
}
function get_pa_mail($dept) {
$result_array = $this->mail_sql($dept);
list($result_T, $count_T) = $result_array;
$i_T = 0;
while ($row_T = mysql_fetch_array($result_T)) {
$email_T = $row_T[email];
$techs .= $email_T;
$i_T++;
if ($i_T < $count_T) {
$techs .= ",";
}
}
echo $error;
return $techs;
}
}
$maillist = new class_pa_email();
echo $maillist->get_pa_mail('1'); // echos just dept 1
echo "<p />";
echo $maillist->get_pa_mail('2'); // echos just dept 2
echo "<p />";
echo $maillist->get_pa_mail('0'); // echos all departments
I had no idea that in the mysql_query function you only needed to give
it the variable containing the querey string; I learned it so you had
to provide it with both the querey and the connection variable. Your
way makes SQL queries inside functions so much easier!
OK, so now about making the class not "know" about the database...are
you meaning, take the SQL function outside of the class and putting the
function in the other PHP file that's included, and then call to it
from the function in the class?
And what do you mean exactly by the object name thing? Are you refering
to something different than what I finally did above?
If you of anygood Web sites you'd recommended for beginning PHP
objects, I'd be appreciative. =)
Thanks for the reply!
Liam
[Back to original message]
|