Welcome Guest, Not a member yet? Register   Sign In
Variables are not transmitted to view? Validation not work.
#1

[eluser]v33s[/eluser]
Hi all. It`s my method in Controller:

Code:
function news($param = null)
{
if($param == 'edit')
            {
                $query = $this->sa->_editNewsById($this->uri->segment(4));
                foreach ($query->result() as $row)
                {
                    $resarray['title'] = $row->title;
                    $resarray['excerpt'] = $row->excerpt;
                    $resarray['content'] = $row->content;
                }
                

                $rules['title']   = 'required|max_length[40]';
                $rules['excerpt'] = 'required|max_length[255]';
                $rules['content'] = 'required';
                $this->validation->set_rules($rules);
                $this->validation->set_error_delimiters('<font color="red">', '</font>');

                if ($this->validation->run() == FALSE)
                {
                    $this->load->view('backend/sp_backend_news_edit', $resarray);
                }
                else
                {
                    $this->sa->_updateNews(
                        $this->uri->segment(4),
                        $this->input->post('title'),
                        $this->input->post('excerpt'),
                        $this->input->post('content'));
                    $this->load->view('backend/sp_backend_news_edit_success');
                }
            }
}

And this is my view:

Code:
&lt;?php
echo form_open('backend/news/edit');
echo form_label('Title:', 'title');
echo '<br />';
echo '<h6>'.$this->validation->title_error.'</h6>';
echo form_input(array('name' => 'title', 'value' => $title, 'maxlength' => 50, 'style' => 'width:34%'));
echo '<br />';
echo form_label('Excerpt:', 'excerpt');
echo '<br />';
echo '<h6>'.$this->validation->excerpt_error.'</h6>';
echo form_textarea(array('name' => 'excerpt', 'value' => $excerpt, 'rows' => 6, 'cols' => 35));
echo '<br />';
echo form_label('Content:', 'content');
echo '<br />';
echo '<h6>'.$this->validation->content_error.'</h6>';
echo form_textarea(array('name' => 'content', 'value' => $content, 'rows' => 12, 'cols' => 70));
echo '<br /><br />';
echo form_submit('submit', 'Sumbit');
echo form_close();

?&gt;
This code above are getting news from database (by ActiveRecords) and putting it into form that user can edit these values. There arent any errors, but if I submit form without one notfilled field, then I get errors:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: resarray

Filename: controllers/backend.php

Line Number: 205

And below after each field:

Quote:Severity: Notice

Message: Undefined variable: title ->> this variable is changing each field

Filename: backend/sp_backend_news_edit.php

Line Number: 227


Can someone explain me how to repair it?
I will be very thankfull Wink
#2

[eluser]gtech[/eluser]
if your database query does not return anything then resarray is not being initialised.

in the first line of your function you can do
Code:
$resarray = array('title'=>'', 'excerpt'=>'', 'content'=>'');

I think thats the problems anyway, good luck!
#3

[eluser]v33s[/eluser]
Hi gtech. Thanks for fast reply.

I forgot to delete comments in my view.

If I put the
Code:
$resarray = array('title'=>'', 'excerpt'=>'', 'content'=>'');


There are errors now too:

Quote:Severity: Notice

Message: Undefined property: CI_Validation::$title_error

Filename: backend/sp_backend_news_edit.php

Line Number: 226

CI_Validation::$title_error

For each field the validation error is the same, like CI_Validation::$title_error, CI_Validation::$excerpt_error, CI_Validation::$content_error

Please give me some advice Wink I am standing with it since 4 hours ;/
#4

[eluser]gtech[/eluser]
I seem to remember from experience the form code uses the set_fields function to build the error variables.

add the bit after the comment to your code.
Code:
...
$rules['title']   = 'required|max_length[40]';
$rules['excerpt'] = 'required|max_length[255]';
$rules['content'] = 'required';
$this->validation->set_rules($rules);

//use set fields so error messages get displayed in friendly manner
$fields['title']    = 'Title';
$fields['excerpt']    = 'Excerpt';
$fields['content']    = 'Content';

$this->validation->set_fields($fields);
...

let me know if it works Smile.
#5

[eluser]v33s[/eluser]
Thanks gtech, the aren`t any errors now even if I not fill fields Wink
But... database is not updating ;( Please one last advice Wink

Code:
function news($param = null)
{
if($param == 'edit')
            {
                $resarray = array('title'=>'', 'excerpt'=>'', 'content'=>'');
                $query = $this->sa->_editNewsById($this->uri->segment(4));
                foreach ($query->result() as $row)
                {
                    $resarray['title'] = $row->title;
                    $resarray['excerpt'] = $row->excerpt;
                    $resarray['content'] = $row->content;
                }
                

                $rules['title']   = 'required|max_length[40]';
                $rules['excerpt'] = 'required|max_length[255]';
                $rules['content'] = 'required';
                $this->validation->set_rules($rules);
                $fields['title'] = 'title';
                $fields['excerpt'] = 'excerpt';
                $fields['content'] = 'content';
                $this->validation->set_fields($fields);
                $this->validation->set_error_delimiters('<font color="red">', '</font>');

                if ($this->validation->run() == FALSE)
                {
                    $this->load->view('backend/sp_backend_news_edit', $resarray);
                }
                else
                {
                    $this->sa->_updateNews(
                        $this->uri->segment(4),
                        $this->input->post('title'),
                        $this->input->post('excerpt'),
                        $this->input->post('content'));
                    $this->load->view('backend/sp_backend_news_edit_success');
                }
            }
}
#6

[eluser]gtech[/eluser]
you may have to debug that one as I have no idea what sa->updateNews does... put some echos in for debug, and then take it out when you have it working.
Code:
else
{
  echo "Well I got in the else condition<br>";
  echo "URI Segment 4 = ".$this->uri->segment(4)."<br>";
  $this->sa->_updateNews(
    $this->uri->segment(4),
    $this->input->post('title'),
    $this->input->post('excerpt'),
    $this->input->post('content'));
    $this->load->view('backend/sp_backend_news_edit_success');

}

Is db_debug set to true.. if so you should get an error message if the update is happening. Put some temporary echo information in your model to see how far it is getting through your code, then remove the echos when you have it working. use print_r instead of echo when you want to print an array or object.. eg print_r($arrayname);
#7

[eluser]v33s[/eluser]
Thanks a lot gtech Wink

URI Segment 4 does not return any variable Wink In my view I add this:
Code:
echo form_open('backend/news/edit/'.$id);
And in Controller:
Code:
$resarray['id'] = $row->id;

And it`s work fine Wink Thanks a lot Wink You were very helpful, applauses for you Wink
#8

[eluser]gtech[/eluser]
No probs.. debugs your friend Smile




Theme © iAndrew 2016 - Forum software by © MyBB