dongtui9168 2013-01-12 16:46
浏览 49
已采纳

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

I'm new to OOP (and programming for that matter, started a few months ago) so classes and objects are still a bit of a foreign concept to me. Anyhow, I'm attempting to store a mix of strings and variables in an array for later processing. See method drawtable(). drawtable() prints the tableheaders just fine, but tableheaders is just an array of strings. The issue comes when I try to print the tablelink, and text properties, which I've attempted to include variables in. What am I doing wrong here? Note: the session variables output fine.

I would also appreciate any general critiques of the code below, as I'm new and attempting to nip any bad habits in the bud. Many thanks.

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

Update:Okay well this works, but I'm sure it's still has an air of noob sloppiness. Suggestions? Thanks

<?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条回答 默认 最新

  • du2229 2013-01-12 16:51
    关注

    It doesn't work like this. You can't access $row in the constructor as over there the variable is undefined. You expect PHP to realize that it should replace the content of $row['name'] etc. later when you actually use the string. However, why should it? Instead PHP can't find $row at all and everything goes wrong. Actually, you should get an error telling you that $row is undefined.

    Overall there is quite a lot wrong with your code. Sorry to say so, but you do not yet grasp the general concept of OOP at all. And it would take way too long to explain this here. And apparently there are some thing not even related to OOP, which you don't know. Scopes for example and about when and where variables exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算