开发者

CodeIgniter - showing original URL of index function?

开发者 https://www.devze.com 2023-02-06 12:15 出处:网络
I\'m not sure if I\'m approaching this fundamentally wrong or if I\'m just missing something. I have a controller and within it an index function that is, obviously, the default loaded when that cont

I'm not sure if I'm approaching this fundamentally wrong or if I'm just missing something.

I have a controller and within it an index function that is, obviously, the default loaded when that controller is called:

function index($showMessage = false) {
    $currentEmployee = $this->getCurrentEmployee();
    $data['currentEmp'] = $currentEmployee; 
    $data['callList'] = $currentEmployee->getDirectReports();
    $data['showMessage'] = $showMessage;
    $this->load->view('main', $data);
}

I have another function within that controller that does a bulk update. After the updates are complete, I want the original page to show again with the message showing, so I tried this:

/**
* Will save all employee information and return to the call sheet page
*/
function bulkSave() {
    //update each employee
    for ($x = 0; $x < sizeof($_POST['id']); $x++) {
        $success = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
    }

    $this->index($success);                
}

What is happening is that the original page is accessed using: localh开发者_运维知识库ost/myApp/myController

after the bulk update it is showing as: localhost/myApp/myController/bulkSave

when I really want it to show the url as the index page again, meaning that the user never really sees the /bulkSave portion of the URL. This would also mean that if the user were to refresh the page it would call the index() function in the controller and not the bulkSave() function.

Thanks in advance.

Is this possible?


You are calling your index() funciton directly, within bulkUpdate() hence the uri does not change back to index because you are not making a new server request, you are just navigating within your controller class.

I usually use the same Controller function for tasks like this, directing traffic based on whether or not $_POST data has been passed or not like this...

function index() {

    if($_POST) {

        //process posted data
        for ($x = 0; $x < sizeof($_POST['id']); $x++) {
            $data['showMessage'] = Employee::updateEmployeeManualData($_POST['id'][$x], $_POST['ext'][$x], $_POST['pager'][$x], $_POST['cell'][$x], $_POST['other'][$x], $_POST['notes'][$x]);
        }            
    }
    else {

        //show page normally
        $data['showMessage'] = FALSE;

    }

    //continue to load page
    $currentEmployee = $this->getCurrentEmployee();
    $data['currentEmp'] = $currentEmployee; 
    $data['callList'] = $currentEmployee->getDirectReports();
    $this->load->view('main', $data);

}

Then if it is a form that you are submitting, just point the form at itself in your view like this...

<?= form_open($this->uri->uri_string()) ?>

This points the form back at index, and because you are posting form data via $_POST it will process the data.


I usually do a redirect to the previous page as it prevent users to refresh (and submit twice) their data.

You can use the redirect() helper function of CI.

http://codeigniter.com/user_guide/helpers/url_helper.html (at the bottom)

0

精彩评论

暂无评论...
验证码 换一张
取 消