Welcome Guest, Not a member yet? Register   Sign In
loading a view with an argument
#1

[eluser]garrettheel[/eluser]
Just wondering, how do you add an argument to a view when loading it? For example, in my edit function, the first argument is the id that is edited. When there is an error, I need to reload the same page they were on before.

But when I try
Code:
$this->load->view('admin/edit/1');
I get an error saying that it couldn't find admin/edit/1.php. How can i overcome this? Thanks
#2

[eluser]sophistry[/eluser]
Code:
redirect('admin/edit/1');
#3

[eluser]oddman[/eluser]
Or even better, assign values via an associative array to the view. Ie.:

Code:
$data = array('error' => $this_is_an_error_var);
$this->load->view('admin/edit', $data);

Now, in your view, simply do:

Code:
if (isset($error) {
  echo $error
}
#4

[eluser]garrettheel[/eluser]
The redirect doesn't work, because I'm using form validation and the validation errors don't work when you redirect to a new page. Any other suggestions?
#5

[eluser]oddman[/eluser]
[quote author="garrettheel" date="1229351777"]The redirect doesn't work, because I'm using form validation and the validation errors don't work when you redirect to a new page. Any other suggestions?[/quote]

O.o

Did you read my suggestion? It will work. I use it for my own error handling/validations.
#6

[eluser]xwero[/eluser]
What is wrong with adding the url to the form action attribute?
Code:
<form action="<?php echo current_url() ?>" method="post">
This keeps the form on the same page.

If you are going from one method to another you need use the redirect.
#7

[eluser]garrettheel[/eluser]
I got your suggestion but I'd rather use CI's built in form validation, which I can't do with that method.
#8

[eluser]garrettheel[/eluser]
I actually just realized what you were talking about xwero and appeneded the ID in the form action, but I was unaware of the current_url function. Thanks
#9

[eluser]Phil Sturgeon[/eluser]
You can sue sessions for this.

Put this code in the form validation complete statement.

Code:
$this->load->library('session');

$this->session->set_flashdata('error', $this_is_an_error_var);
redirect('admin/edit');

Then this code on the landing page (can be the same page if you like).

Code:
if($error = $this->session->flashdata('error')) {
    $data['error'] = $error;
}
$this->load->view('admin/complete', $data);




Theme © iAndrew 2016 - Forum software by © MyBB