无法从codeigniter控制器传递数组值到视图

时间:2013-08-01 22:30:45

标签: php

在我的控制器中,我有一行需要将$ content ['pass_check']传递给视图。它位于if语句中,用于检查验证。我发现这导致它破裂。一旦我将$ content ['pass_check']移到任何if语句之外,它就可以正常传递给视图。传递所有其他值(accounts,expense_accounts,供应商,条款)。我该怎么做才能让它在if语句中传递。我甚至尝试将它移到验证之外,它仍然不会设置。

function create() {
    require_permission("INVOICE_EDIT");

    $this->load->library("form_validation");
    $this->form_validation->set_rules("invoice_number", "Invoice Number", "required");

    if($this->form_validation->run() !== false) {
        $post = $this->input->post();

        $this->session->set_userdata("create_invoice_vendor", $post['vendor_id']);
        $this->session->set_userdata("create_invoice_date", $post['invoice_date']);

        $invoice_number_exists = $this->invoices->count(array("invoice_number" => $post['invoice_number'])) > 0;

        $post['invoice_date'] = date("Y-m-d", strtotime($post['invoice_date']));
        $post['due_date'] = date("Y-m-d", strtotime($post['due_date']));
        $post['date_entered'] = "now()";

        $id = $this->invoices->insert_invoice($post);

        $this->load->model("vendors");

        if(isset($post['invoice_number'])){
            $string_check= $post['invoice_number'];
            $string_check= preg_replace('/\d/', '#', $string_check);
            $string_check= preg_replace('/\w/', '#', $string_check);

            $invoice_pattern=array();
            $invoice_pattern = $this->db->select("invoice_pattern")->where("vendor_id",
                                        $post['vendor_id'])->get("vendors")->result();
            $invoice_pattern=$invoice_pattern[0]->invoice_pattern;

* ////这就是我需要帮助的地方///////

                    if($invoice_pattern == $string_check){
                        ***$content['post_check'] = 1;***
                        $this->invoices->flag_invoice($id);
                    };

                };



                $history = array(
                    "type"      => "invoice_entered",
                    "comments"  => "Invoice was entered",
                    "link"      => $id,
                    "admin_id"  => $this->user->admin_id,
                    "date"      => "now()",
                    );
                $this->vendors->insert_history($post['vendor_id'], $history);

                if($post['flagged'] == 1) {
                    $this->invoices->flag_invoice($id);
                }

                if($invoice_number_exists) {
                    redirect("invoices/confirm_invoice/".$id);
                } else {
                    // redirect("invoices/view/".$id);
                    redirect("invoices/create");
                }
        }

            $content['accounts'] = $this->db->get("acct_chart_of_accounts")->result();
            $content['expense_accounts'] = $this->db->get("invoice_expense_accounts")->result();
            $content['vendors'] = $this->db->select("vendor_id, name, terms, override, invoice_pattern")
                          ->order_by("name ASC")->get("vendors")->result();
            $content['terms'] = $this->db->query("SELECT DISTINCT(terms) FROM vendors")->result();
        }
    }

    $this->template['sub_heading'] = "Create";
    $this->template['content'] = $this->load->view("invoices/create", $content, true);
    $this->template['sidebar'] = $this->load->view("invoices/sidebar", array(), true);
    $this->template['scripts'] = array("codeigniter/javascript/invoices/create.js");
    $this->template['styles'][] = "codeigniter/styles/invoices/create.css";

    $this->display();
}

1 个答案:

答案 0 :(得分:0)

显然,如果条件不匹配,它不会将它传递给视图,因为你只是在条件中声明变量匹配。

首先创建$ content ['pass_check'],初始值为0,或者先进行条件检查。

function create() {

    ...snip...

    $content['pass_check'] = 0;

    if($invoice_pattern == $string_check) {
       $content['post_check'] = 1;
       $this->invoices->flag_invoice($id);
    };

    ...snip...
}

请告诉我这是否有效。