1

我是 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;
    }
}
?>
4

1 回答 1

1

它不像这样工作。您无法$row在构造函数中访问,因为那里的变量未定义。$row['name']当您实际使用该字符串时,您希望 PHP 意识到它应该替换 etc. 的内容。然而,又何必呢?相反,PHP 根本找不到$row,一切都出错了。实际上,您应该得到一个错误,告诉您$row未定义。

总的来说,您的代码有很多错误。很抱歉这么说,但您根本没有掌握 OOP 的一般概念。在这里解释这一点需要很长时间。显然有些事情甚至与 OOP 无关,你不知道。例如范围以及关于何时何地存在变量。

于 2013-01-12T16:51:51.867 回答