OOP新手 - 如何在数组中存储变量?

时间:2013-01-12 16:46:10

标签: php

我是OOP的新手(以及几个月前开始的编程)所以类和对象对我来说仍然是一个外国概念。无论如何,我试图在数组中存储字符串和变量的混合,以便以后处理。请参阅方法drawtable()drawtable()打印tableheaders就好了,但tableheaders只是一个字符串数组。当我尝试打印tablelinktext属性时出现问题,我试图将变量包含在其中。我在这里做错了什么?注意:会话变量输出正常。

我也很感激对下面代码的任何一般批评,因为我是新手,并试图将任何坏习惯扼杀在萌芽状态。非常感谢。

<?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;
    }
}
?>

1 个答案:

答案 0 :(得分:1)

它不像这样工作。您无法在构造函数中访问$row,因为变量未定义。您希望PHP在以后实际使用字符串时意识到应该替换$row['name']等内容。但是,为什么要呢?相反,PHP根本找不到$row,一切都出错了。实际上,您应该收到一条错误消息,告诉您$row未定义。

总的来说,你的代码存在很多问题。很抱歉这样说,但你还没有掌握OOP的一般概念。在这里解释这个问题需要花费太长时间。显然有一些事情甚至与OOP无关,你不知道。示例范围以及变量存在的时间和位置。