Welcome Guest, Not a member yet? Register   Sign In
how to validate and skip the parsing variable from controller to view
#1

[eluser]LordLaw[/eluser]
let said from controller i do like this

$data["var1"] = "var1";
$data["var2"] = "var2";

$this->load->view("myview",$data);

then in the myview.php if i want to display var1 and var2, i do like
echo $var1;
echo $var2;

that how it did right.

but how if i reuse the myview.php and from controller i only parse in var1, like this
$data["var1"] = "var1";

$this->load->view("myview",$data);

then in the myview.php, the echo var2 will be errors.
how to validate it and avoid it ? this will be lot of help in form resuse.

anyone have any idea ? thanks.
#2

[eluser]Randy Casburn[/eluser]
Great question. This is the topic in the documentaton page about Views that discusses "Creating Loops". Go back and review that section. I think it answers your question directly.

Randy
#3

[eluser]LordLaw[/eluser]
there is no others way?

cause when i get the data from database and display it in form for edit purpose.
i need to display it one by one...

maybe i will try to twist the reference guide to something else...
see if i can manage to find out something there.

thanks
#4

[eluser]Randy Casburn[/eluser]
maybe I've misunderstood.
the user guide says "load your data into an array then loop through the array and do what you want with it..."
You can test for $var1 and $var2 in the controller and only push existing elements onto the array.

Sorry I don't understand what your needs are.

Randy
#5

[eluser]LordLaw[/eluser]
i will have quick test on this and get back the result here.
hope there wont be any problem.
thanks
#6

[eluser]Randy Casburn[/eluser]
Here is your original post rewritten according to the docs...

=======
Code:
$data['vars'] = array(
               'var1' => 'var1',
               'var2' => 'var2',
          );

$this->load->view("myview",$data);

then in the myview.php if i want to display var1 and var2, i do like
echo $vars['var1'];
echo $vars['var2'];
Quote:but how if i reuse the myview.php and from controller i only parse in var1, like this
Code:
$data['vars'] = array(
               'var1' => 'var1',
          );


$this->load->view("myview",$data);

echo $vars['var1'];
=======

Does this make sense?

Randy
#7

[eluser]LordLaw[/eluser]
well, that not kinda like it, you forget to put var2 in myview.php which it suppose to be there.

i tested the array already, it does work if you are checking with array exist empty or not.

but in term of non-array variable, then it will produce error.

let say you get a select query result from the model and return it to controller and put it in to $data like this:

$data["row"] = $this->model->get_result(); // this is just example return col1, col2, col3

then you parse it into view

$this->load->view("edit_form", $data);

in the edit form you have 3 text box,
<form>
<text value=&lt;?=$row->col1?&gt;>
<text value=&lt;?=$row->col2?&gt;>
<text value=&lt;?=$row->col3?&gt;>
&lt;/form&gt;


so, from here as you can see, if there is a result been return from database, then in the view there is no problem, but if there isnt any data return from database, then in the there will be errors occurs.
but this can be solve with :
if(!$row) {//do something }

first part done ....
#8

[eluser]LordLaw[/eluser]
here is another case,
in the controller

$data["row"] = $this->model->get_result(); // this is just example return col1, col2, col3
$data["extra_var"] = "";
$this->load->view("edit_form", $data);

in the edit_form
&lt;form&gt;
<text value=&lt;?=$row->col1?&gt;>
<text value=&lt;?=$row->col2?&gt;>
<text value=&lt;?=$row->col3?&gt;>
&lt;? echo $extra_var; ?&gt;
&lt;/form&gt;

in this case it works,

but how if the controller doesnt define $data["extra_var"] at first, then there will be an error inside view.

you might ask me to just remove the $extra_var in the view,
but in this case as i mentioned earlier, this is reuseable view, so that $extra_var is necessary to be there for others purpose.

so how to get rid the errors?

for example if your post form some field is empty, you can use $this->input->post() to validate the variable exist or not.

so above case, any suggestion or idea ?
#9

[eluser]Mr. Ed[/eluser]
Maybe with if(isset($row->col1))echo "no var"; ??

That's what I use to validate vars that may or may not be seted for the view.
#10

[eluser]LordLaw[/eluser]
[quote author="Mr. Ed" date="1217337801"]Maybe with if(isset($row->col1))echo "no var"; ??

That's what I use to validate vars that may or may not be seted for the view.[/quote]

your idea is working....

thanks




Theme © iAndrew 2016 - Forum software by © MyBB