我想在我們的網站上列出我的投資組合,我寫了一個課程,客戶端設置和獲取有關每個客戶的基本知識的參數,即:客戶名稱,網站,它的特色,是否發佈,爲他們做,等等。PHP類是上市內容的有效解決方案嗎?
這是相當原始的,但我正確地認爲最好爲每個客戶端創建一個對象(並保持每個數據在一起),而不是爲每個參數創建數組,並從這些數組中調用相應的元素每次?
這是我的課,我知道它仍然是原始的,但我也可能創建一些其他功能,所以現在我只是使用基本的getter和setter來獲取或設置每個客戶端的參數。
<?php
class Client {
private $name = "";
private $items = array();
private $website = "";
private $featured = false;
private $published = false;
private $type = array();
private $filename = null;
private $extension = null;
public function __set($name, $value) {
//echo "Setting ".$name." to value: ".$value."\n";
$this->{$name} = $value;
}
public function __get($name) {
//echo "Getting ".$name.": ".$this->{$name};
return $this->{$name};
}
public function __isset($name) {
return isset($this->{$name});
}
public function __unset($name) {
unset($this->{$name});
}
}
?>
我仍在計劃我可能想要添加到課程中的功能,但現在就是這樣。
這裏是我的其他代碼我使用創建每個客戶對象:
<?php
// create Client object for every client
$files = array();
// files to take out of file listing, I'm developing on Mac, i.e. ._DS_Store file
$bad_files = array(".","..","._DS_Store");
$portfolio = "portfolio";
$images = "images";
$details = "details";
$thumbs = "thumbs";
// get all *.txt files listed in portfolio, so a client will not be added to portfolio without the necessary details.
if (is_dir("$images/$portfolio")) {
if (is_dir("$images/$portfolio/$details")) {
$files = scandir("$images/$portfolio/$details");
sort($files);
}
}
$files = array_diff($files, $bad_files);
sort($files);
// keeps a list of all clients
$clients = array();
foreach ($files as $file) {
$value = file_get_contents("$images/$portfolio/$details/$file");
$newClient = new Client();
$filename = explode(".",$file);
$newClient->filename = $filename[0];
$client_image = glob("$images/$portfolio/$images/".$newClient->filename.".*");
$newClient->image = $client_image[0];
$client_thumb = glob("$images/$portfolio/$thumbs/".$newClient->filename.".*");
$newClient->thumb = $client_thumb[0];
$client_items = array();
$client_type = array();
// gets variables from text file contents and explode string to array [key=value] values
$content = explode("&",$value);
for ($j=0; $j<count($content); $j++) {
$client = explode("=", $content[$j]);
if (strpos($client[0],"name") !== false) $newClient->name = $client[1];
if (strpos($client[0],"items") !== false) $client_items = $client[1];
if (strpos($client[0],"website") !== false) $newClient->website = $client[1];
if (strpos($client[0],"featured") !== false) $newClient->featured = $client[1]; // show on frontpage
if (strpos($client[0],"published") !== false) $newClient->published = $client[1]; // show at all
if (strpos($client[0],"type1") !== false) $client_type[] = $client[1]; // show for specific type, eg. business card, website
if (strpos($client[0],"type2") !== false) $client_type[] = $client[1]; // show for specific type, eg. business card, website
}
// these parameters need array type values
$newClient->type = $client_type;
$newClient->items = explode(", ",$client_items);
// adds client to list of clients
$clients[] = $newClient;
}
?>
這裏是我用來輸出每個客戶的旗幟和細節代碼:
<div id="banner_content">
<?
foreach ($clients as $client) {
// client must be published to show at all
if ((($page == "home" && $client->featured) || $page != "home") && $client->published) {
?>
<div class="banner_container"><img src="<? echo $client->image; ?>" width="809" height="324" alt="<? echo $client_name; ?>" title="<? echo $client_name; ?>" />
<div class="banner_details">
<div class="client_name">Client: <b><? echo (!empty($client->name) ? $client->name : "Unknown"); ?></b></div>
<div class="client_items"><? echo (!empty($client->items) ? "Items: <b>".join(", ",$client->items)."</b>" : ""); ?></div>
<div class="client_website"><? echo (!empty($client->website) ? "Website: <b><a href=\"http://".strtolower($client->website)."\">".$client->website."</a></b>" : ""); ?></div>
</div>
</div>
<?
}
}
?>
</div>
任何幫助或建議將不勝感激。提前致謝。
//編輯
我忘了提,我居然寫了類,因爲會有一個投資組合頁面,其中將包含有關客戶的不僅僅是上述信息的詳細信息。我知道一個班級只是在橫幅中列出圖片是有點矯枉過正的。
Aaahhh,dankie Bernhard! – 2010-08-05 13:34:31
可以通過數組跟蹤多個Client對象嗎? – 2010-08-05 13:41:00