1

在我的控制器内部,我有一行需要将 $content['pass_check'] 传递给视图。它位于检查验证的 if 语句中。我发现这会导致它破裂。一旦我将 $content['pass_check'] 移到任何 if 语句之外,它就可以很好地传递给视图。传递所有其他值(帐户、费用帐户、供应商、条款)。我必须做什么才能让它在这个 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();
}
4

1 回答 1

0

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

只需在条件检查之前创建初始值为 0 或任何值的 $content['pass_check'] 即可。

function create() {

    ...snip...

    $content['pass_check'] = 0;

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

    ...snip...
}

请让我知道这是否有效。

于 2013-08-01T23:23:19.547 回答