Welcome Guest, Not a member yet? Register   Sign In
little help with redirecting logic
#1

[eluser]knucklehead[/eluser]
Hello!

I am trying to create a codeigniter app but have some small problems with redirecting...

What I've created so far is a 'main' page with a form where you choose one of items from dropdown menu. Form is submitted via POST method, and on next page user should be able to modify that item's data (let's call this 'item' page). Data on 'item' page is strictly dependent on POST data.
Then, I try to create some add/edit/delete item controllers but I have a problem with redirecting to the item's page after adding, editing or deleting items. So... I guide user through few steps, and after last step I should redirect to 'item' page (which is relying on POST data) but I'm not sure how to send POST data back again...

If it is relevant in any case, I'm using 'Most simple template engine' in my app to show all the data... (http://maestric.com/doc/php/codeigniter_template)

Hope I didn't complicated too much with my explanations Smile

Thanks for any help in advance!
#2

[eluser]sketchynix[/eluser]
Could you post some code or provide a link for your problem? The problem seems solvable, but it is rather vague right now and any answer provided would likely be just as vague without some code to work with
#3

[eluser]knucklehead[/eluser]
Hey sketchynix, thanks for help, I will try to summarize my code in a few words Smile

First, I set up template system, and in each controller i am sending some data to my template;
Code:
<?php

class Main extends Controller {

    function __construct() {
        parent::Controller();    
    }
    
    function index() {
        $this->load->model('shows_model');
        $get_shows = $this->shows_model->get_shows();
        
        $shows['all_shows'] = array();
        foreach ($get_shows as $show) {
            $id = $show['id'];
            $shows['all_shows'][$id]['show_id'] = $show['id'];
            $shows['all_shows'][$id]['show_name'] = $show['show_name'];
        }
        
        $tmp = $this->load->view('main', $shows, true);
        $this->template->load('template', 'main');
    }
}

/* EOF */

So, in my Main view, I am showing the shows list and user selects one and goes to Insert controller:
Code:
<?php

class Insert extends Controller {

    function __construct() {
        parent::Controller();
    }
    
    function index() {
        $this->load->model('shows_model');
        $this->load->model('items_model');
        // load some other models
        
        // here I select all items within a selected show...
        $get_items = $this->items_model->get_items_for_show($_POST['show']);
        
        // $data = formatted items data for my view...
        
        $temp = $this->load->view('insert', $data, true);
        $this->template->load('template', 'insert');
    }
}

/* EOF */

Here user can modify data within a 'show'... Let's say the user wants to delete some item from the list, he clicks on a delete button and my Delete controller looks like this:
Code:
<?php

class Delete extends Controller {

    function __construct() {
        parent::Controller();
    }
    
    function item($id, $pl_id) {
        $this->load->model('items_model');
        
        $this->items_model->delete_item($id);
        
        $get_items = $this->items_model->get_items_for_playlist($pl_id, 'order');
        
        $i = 1;
        foreach ($get_items as $item) {
            $this->items_model->sort_item_order($item['id'], $i);
            $i++;
        }
        
        // here, after I deleted stuff from tables and sort all other items,
        // I should redirect user back to Insert page, where he can see the list
        // of all items within a show...
        // Of course, Insert controller expects POST data...
    }
}

/* EOF */

And I'm stuck here... I don't understand how to send data back to Insert controller because he expects that I send some form, right?

Thanks for help!
Cheers!
#4

[eluser]SPeed_FANat1c[/eluser]
Not sure if it will help but I think you can make a parameter to your index function in Insert controller and pass some data.

And before the line
$get_items = $this->items_model->get_items_for_show($_POST['show']);

check if index function has data (not default data) in parameter, if it hasn't then use data from $_POST.

for example your index function would be

Code:
function index ($var = 'default')
{
  if($var != 'default')
  {
    //use $var
  }
  else
  {
    //use post data
  }
}
#5

[eluser]kaejiavo[/eluser]
On your form put the Show ID in a hidden field. Then you can redirect to
Insert/index/Show_id

Change your Insert/index method to
Code:
function index($show_id = 0) {

  if (!empty($_POST['show'])) {
     $show = $_POST['show'];
  }
  else
  {
    $show = $show_id;
  }
  ...
  $get_items = $this->items_model->get_items_for_show($show);
  ...
}

This should maybe work, but i would strongly suggest you to read the user_guide Form_validation_Class, Input_And_Security_Class...
I am not even sure if you can access the POST values in CI with $_POST['value'].

Marco
#6

[eluser]knucklehead[/eluser]
Hello guys and thanks for helping me with this one Smile

I had an idea to change my index function like you said, but the problem is that I should send more than only 'show_id' back to my Insert controller... I'm thinking to somehow send a variable which will hold all the data from Insert controller, through all steps and in last step I will just pass it back to Insert controller... Do you think that's a good idea?

@mawi27
Ah yes, I know about Input class etc, but I had some small problems with it, so I accidentally left $_POST lines... I will fix that right now Smile

Thanks one more time Smile
#7

[eluser]kaejiavo[/eluser]
I think it is no good idea. You should only transfer the id and get all related data from the database.

Think of the situation that some guy has tamper data installed as firefox extension (we as developers all have it, for sure). He looks at your form with this huge POST array and thinks - now, i would like to see what the app makes of this obscure input...
You would have to sanitize and validate each of your POST array fields to make this working.
I would definitely not do that.

Marco
#8

[eluser]knucklehead[/eluser]
Hmmm, interesting Smile

Ok, I will try to redesign my app a bit, so I could easily fetch all the data I need in both cases; if user selects a show at the very beginning, or after redirecting back to Input controller from any other page... I think that would be the best thing to do in my case, right?
#9

[eluser]kaejiavo[/eluser]
a little example:
Code:
function edit($id = 0) {

        // if form is submitted or reloaded use hidden field eid as record id
        if ($this->input->post('eid'))
            $id = $this->input->post('eid');

        // no funny business
        if (!(int) $id) {
            $this->session->set_flashdata('message', 'dont try to be funny');
            redirect('controller');
        }

        // get record data from db
        $record = $this->Model->get_by_id($id);

        // no record with this id existing
        if (empty($record)) {
            $this->session->set_flashdata('message', 'invalid id');
            redirect('controller');
        }

        // set validation rules for form processing
        $this->form_validation->set_rules('eid', 'eid', 'max_length[11]');
        $this->form_validation->set_rules('name', 'name', 'required|max_length[45]');

        // validation not passed
        if ($this->form_validation->run() == FALSE) {
            // build the form
        }
        else
        {
            // process the form data
        }
}
#10

[eluser]knucklehead[/eluser]
Ok, problem solved, thanks for all your help!




Theme © iAndrew 2016 - Forum software by © MyBB