Welcome Guest, Not a member yet? Register   Sign In
Passing the variable to view from another view
#1

[eluser]Martin_new[/eluser]
>> My controllerv - verifyregistration

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class verifyregistration extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->model('users_model','',TRUE);
    $this->load->library('email');
}

function index()
{
    //This method will have the credentials validation
    $this->load->library('form_validation');

    $this->form_validation->set_rules('Login', 'Login', 'trim|required|xss_clean|callback_check_database|min_length[6]|strtolower|callback_username_not_exists');
    $this->form_validation->set_rules('Haslo', 'Haslo', 'trim|required|xss_clean|min_length[6]');
    $this->form_validation->set_rules('Powtorz_Haslo', 'Powtorz haslo', 'trim|required|xss_clean|matches[Haslo]|min_length[6]');
    $this->form_validation->set_rules('Imie', 'Imie', 'trim|required|xss_clean|min_length[2]');
    $this->form_validation->set_rules('Nazwisko', 'Nazwisko', 'trim|required|xss_clean');
    $this->form_validation->set_rules('Email', 'Email', 'trim|required|xss_clean|valid_email|min_length[6]');
    $this->form_validation->set_rules('Data_urodzenia', 'Data urodzenia', 'trim|required|xss_clean');
    $this->form_validation->set_rules('Telefon', 'Telefon', 'trim|required|xss_clean|min_length[8]');
    $this->form_validation->set_rules('Miasto', 'Miasto', 'trim|required|xss_clean');
    $this->form_validation->set_rules('Ulica', 'Ulica', 'trim|required|xss_clean|min_length[6]');
    $this->form_validation->set_rules('Kod_pocztowy', 'Kod pocztowy', 'trim|required|xss_clean');
    $this->form_validation->set_error_delimiters('<span>', '</span>');
    $this->form_validation->set_error_delimiters('<li>', '</li>');
    $data['heading'] = "My Real Heading";  

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('register/register_open',$data);
    }
    else
    {
        return false;  //this is not important
    }
}
>> My view open - register_open, this file is in view folder
Code:
&lt;?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('register/register_view',$data);
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?&gt;

>>My view - register_view

Code:
&lt;?php echo form_open('verifyregistration/index'); ?&gt;
    &lt;form&gt;
        <label for="username">Login:</label>
        &lt;input type="text" size="25" id="Login" name="Login" set_value='Login' /&gt;
        .
        .
        .

        <legend>
            &lt;input type="checkbox" id="regulamin" name="Regulamin" /&gt;
            Akceptuję regulamin serwisu. </legend>

            <label>&nbsp;</label>
            &lt;input type="submit" name="Wyslij" value="Wyslij" disabled="disabled"/&gt;
            <label>&nbsp;</label>

        <legend>
            <&lt;?php echo $heading;?&gt;
        </legend>
    &lt;/form&gt;


And I have errors:

Severity: Notice Message: Undefined variable: data Filename: register/register_open.php Line Number: 9 this line-> $this->load->view('register/register_view',$data);

Severity: Notice Message: Undefined variable: heading Filename: register/register_view.php Line Number: 48 this line-> <&lt;?php echo $heading;?&gt;

How I can pass the data to another view?
#2

[eluser]Beginers[/eluser]
when you pass the variable
Code:
$data['heading']="My Real Heading";
to your register_open view the array name of your $data['heading'] will be converted to a variable to your view something like this :
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $heading?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

notice that it's not a $data anymore but $heading so when you pass it again to another view you should create another variable that will handle the value. Something like this:

Code:
$data['heading']=$heading;
$this->load->view('register/register_view',$data);

I don't know if that works. Just try it.
#3

[eluser]Martin_new[/eluser]
No, this isn`t work. Errors are still that same.
#4

[eluser]Otemu[/eluser]
Why not change your code to something like this
Code:
if($this->form_validation->run() == FALSE)
{redirect('register/register_open','refresh');
}


create the controller, then load your views and pass your variable in that controller
Code:
$data['heading'] = "My Real Heading";
#5

[eluser]jojo777[/eluser]
Looks like some code I've got

Well for example I´ve got a template view that loads header-content-footer. Content can be any view I pass.

I'm going to take your register_open and include it.

So in my example_controller I got this:

Code:
function index(){
  $data['view'] = 'register/register_open';
  $data['heading] = 'This is my heading LOL!;

  $this->load->view('_includes/template',$data);
}

So now the code is going to load my template and I've passed a $data array with view and header.
Template view:


Code:
&lt;?php
   $this->load->view('_includes/header.php');
   #...whatever...
   $this->load->view($view); // this is going to load your register/register_open view!!
   #...whatever...
   $this->load->view('_includes/footer.php');

And finally in your register/register_open view.
For example...

Code:
<h1>&lt;?php echo $heading;?&gt;</h1>

Should work!
#6

[eluser]Martin_new[/eluser]
I fix my problem. Thanks for help Smile
#7

[eluser]jojo777[/eluser]
Good!

What was the problem? And how did you fix it? So if any has the same problem can see the solution! Wink
#8

[eluser]Martin_new[/eluser]
In controller I add:

Code:
$data['todo_list'] = array(validation_errors());
$this->load->view('register/register_open',$data);

in open view:
Code:
&lt;?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('register/register_view');
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?&gt;

and in view:

Code:
&lt;?php foreach ($todo_list as $item):?&gt;
     &lt;?php echo $item;?&gt;
&lt;?php endforeach;?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB