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.

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向