我是 OOP 的新手(和这方面的编程,几个月前开始),所以类和对象对我来说仍然是一个陌生的概念。无论如何,我试图将字符串和变量的混合存储在一个数组中以供以后处理。见方法drawtable()
。drawtable()
打印tableheaders
就好了,但tableheaders
只是一个字符串数组。当我尝试打印我试图在其中包含变量的tablelink
, 和text
属性时,问题就出现了。我在这里做错了什么?注意:会话变量输出正常。
我也会感谢对下面代码的任何一般批评,因为我是新手,并试图将任何坏习惯扼杀在萌芽状态。非常感谢。
<?php
class table{
function __construct($type){
if($type == "submittals"){
$this->tableheaders = array('Specification Section', 'Submittal #', 'Description', 'Vendor or Manufacturer', 'Date Received By GC', 'File', 'Date Submitted', 'File', 'Date Requested for Review', 'Date Returned to GC', 'File', 'Status', 'Date Returned to Subcontractor or Vendor');
$this->query = "***SELECT Query***";
$this->tablelink = array("/pd/upload/" . $_SESSION['current_project'] . "/s/" . $row['file'], "edit_submittal.php?submittal_id=", "edit_submittal.php?submittal_id=" . $row['submittal_id']);
$this->text = array($row['number'] . " - " . $row['name'], $this->row['submittal_num']);
}
}
function drawtable() {
$this->numheaders = count($this->tableheaders);
$this->db = mysqli_connect(***db connection info***);
$this->results = mysqli_query($this->db, $this->query);
$this->num_results = mysqli_num_rows($this->results);
echo " <table border=\"1\">
<tr>";
for ($i = 0; $i < $this->numheaders; $i++) {
echo "<th>" . $this->tableheaders[$i] . "</td>";
}
echo " </tr>";
for ($j=0; $j < $this->num_results; $j++) {
$row = mysqli_fetch_assoc($this->results);
echo " <tr>";
for($k = 0; $k < $this->numheaders; $k++) {
echo " <td><a href=\"" . $this->tablelink[$k] . "\">" . $this->text[$k] . "xxx</a></td>";
}
}
echo " </tr>
</table>";
}
}
?>
更新:好吧,这行得通,但我敢肯定它仍然有一种菜鸟马虎的气氛。建议?谢谢
<?php
class Table{
function __construct($type){
if($type == "submittals"){
$this->table_headers = array('Specification Section', 'Submittal #', 'Description', 'Vendor or Manufacturer', 'Date Received By GC', 'File', 'Date Submitted', 'File', 'Date Requested for Review', 'Date Returned to GC', 'File', 'Status', 'Date Returned to Subcontractor or Vendor');
$this->query = "***SELECT query***";
}
}
function draw_table($type) {
$this->num_headers = count($this->table_headers);
$this->db = mysqli_connect(***db connection***);
$this->results = mysqli_query($this->db, $this->query);
$this->num_results = mysqli_num_rows($this->results);
echo " <table border=\"1\">
<tr>";
for ($i = 0; $i < $this->num_headers; $i++) {
echo "<th>" . $this->table_headers[$i] . "</td>";
}
echo " </tr>";
for ($j=0; $j < $this->num_results; $j++) {
$row = mysqli_fetch_assoc($this->results);
if($type == "submittals") {
$this->table_link = array("/pd/upload/" . $_SESSION['current_project'] . "/s/" . $row['file'], "edit_submittal.php?submittal_id=", "edit_submittal.php?submittal_id=" . $row['submittal_id']);
$this->text = array($row['number'] . " - " . $row['name'], $row['submittal_num'], $row['description']);
}
echo " <tr>";
for($k = 0; $k < $this->num_headers; $k++) {
echo " <td><a href=\"" . $this->table_link[$k] . "\">" . $this->text[$k] . "</a></td>";
}
}
echo " </tr>
</table>";
}
function get_table_headers() {
echo $this->table_headers;
}
function get_query() {
echo $this->query;
}
function get_table_link() {
echo $this->table_link;
}
function get_text() {
echo $this->text;
}
}
?>