我对OOP和PHP相当新。我一直在通过Kevin Yank的书,并希望继续推进一些OOP概念。但我有点失落,我需要帮助。
我正在尝试使用供应商类从数据库生成供应商列表。 城市和国家等字段使用ID(使用城市和国家/地区表的关系数据库),因此在我在html输出文件中发布完整列表之前,我还需要能够获取这些名称值。
我可以在程序上完成所有这些工作,但在搬到OOP时努力寻找合适的结构。
我按照Kevin的说明为我的PDO数据库连接创建了一个db.inc.php文件:
<?php
//db connection using PDO class object
try {
//create new PDO class object
$pdo = new
PDO('mysql:host=122.2.2.2;port=3307;dbname=mydbname','dbuser','password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
//if unable to connect, tell us why
$output = 'Unable to connect to the database server 2.'.$e->getMessage();
include 'output.html.php';
exit();
}
我创建了一个供应商类。这个类将包含操纵供应商的所有方法:getVendors(),getVendor($ id),deleteVendor($ id),updateVendor($ id)等。
<?php
class Vendor {
protected $name;
protected $address;
protected $city;
protected $state;
protected $postal;
protected $country;
protected $tel;
protected $email;
protected $web;
public function __construct(){
//typically first function to appear in a class
// Constructor using MagicMethod
$this->name = $name;
$this->address = $address;
$this->city = $city;
$this->state = $state;
$this->postal = $postal;
$this->country = $country;
$this->tel= $tel;
$this->email = $email;
$this->web = $web;
}
public function getVendors($pdo){
//get all vendor data from db
try
{
//prepares a statement for execution and returns a statement object
$query = $pdo->prepare("SELECT * FROM vendor ORDER BY name");
$query->execute();
$vendors = array();
foreach ($query->fetchAll() as $row){
//create array of member objects
$vendors[] = new Vendor($row);
}
//return member objects
//not sure this is the right thing to do here
return $vendors;
}
catch (PDOException $e)
{
//handle any errors
$output = 'Error fetching vendors: ' . $e->getMessage();
include 'output.html.php';
exit();
}//end try catch
}//end function
//setters
public function setName( $name ){
$this->name = $name;
}
public function setAddress( $address ){
$this->address = $address;
}
public function setCity( $city ){
$this->city = $city;
}
public function setState( $state ){
$this->state = $state;
}
public function setPostal( $postal ){
$this->postal = $postal;
}
public function setCountry( $country ){
$this->country = $country;
}
public function setTel( $tel ){
$this->tel = $tel;
}
public function setEmail( $email ){
$this->email = $email;
}
public function setWeb( $web ){
$this->web = $web;
}
//getters
public function getName(){
return $this->name;
}
public function getAddress(){
return $this->address;
}
public function getCity(){
return $this->city;
}
public function getState(){
return $this->state;
}
public function getPostal(){
return $this->postal;
}
public function getCountry(){
return $this->country;
}
public function getTel(){
return $this->tel;
}
public function getEmail(){
return $this->email;
}
public function getWeb(){
return $this->web;
}
}
我有一个调用供应商列表的index.php文件
<?php
//get all vendors
include_once $_SERVER['DOCUMENT_ROOT'] .'/db.inc.php';
include_once $_SERVER['DOCUMENT_ROOT'] .'/includes/Vendor.Class.php';
//instantiate class
$vendor = new Vendor();
//calling my function to get a list of all vendors
//not sure if passing in $pdo object is a good idea/practice
$vendors = $vendor->getVendors($pdo);
//test print out vendors
echo $vendors;
print_r($vendors);
//output to template
include 'output.html.php';
然后想法使用模板/输出html文件输出数据:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php foreach ($vendors as $vendor){
//if my city is an id value, at what point do I call the City Class to look up
that city name? I should not be calling DB from my view. And is it OK to have
html code in my controller?
$name = $vendor->getName();
$address2 = $vendor->getAddress2();
$city = $vendor->getCity();
$zip = $vendor->getZip();
$web = $vendor->getWeb();
$phone = $vendor->getTel();
?>
</body>
</html>
我的问题:
非常感谢任何帮助。
答案 0 :(得分:0)
我不认为你应该制作单独的类来获取城市名称和国家名称,只需在数据库中创建一个视图,选择供应商的详细信息,用名称替换id并从中选择* ... 只是为了简单起见,将所有属性放入数组中是一个更好的选择 只需将返回的数组从sql保存到数组中......