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 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目
  • ¥15 根据时间在调用出列表
  • ¥15 R 包chipseeker 安装失败
  • ¥15 Veeam Backup & Replication 9.5 还原问题
  • ¥15 vue-print-nb
  • ¥15 winfrom的datagridview下拉框变成了黑色,渲染不成功
  • ¥20 利用ntfy实现短信推送