Welcome Guest, Not a member yet? Register   Sign In
load different views in one view
#1

[eluser]ramonskie[/eluser]
okay i'm still a beginner.. but we all need to start somewhere Smile

i have created different view files that have a div with forms like signup, login, etc etc

now i have a main view
that have on the left side a login view to begin with
and on the right side it has a small list that we just bought (its a shopping cart kind of thing)
now if i don't have a login yet i want to create a account
this form is located in a different viewfile
so when i press the button "register user" i want to show the registration form
but i want to keep the list of course
so what i want to achieve is to only change the login_form to the registration_form


i hope you understand my question its a bit complicated to explain with my crappy english
#2

[eluser]jdav3579[/eluser]
Hi With views there is an optional third parameter. $this->load->view(filename, data, [return]).
I would perhaps put a variable in your main file called $sidepanel for example.
I would then load the file which has the most appropriate view in (ie login or create) into a variable called $data['sidepanel] in your controller by setting the third parameter as true. Then simply in your main view output the contents of $sidepanel.
Code:
controller{

function index(){
  if($loggedin)
    $data['sidepanel']=$this->load->view('login',$whateverdatarequd,true);
  else
    $data['sidepanel']=$this->load->view('register',$whateverdatarequd,true);

  $this->load->view('main',$data);
}

}#end of controller

##in view

print $sidepanel

Of course if you want to have the two forms on the same page without reloading, then output them both and use Jquery etc to hide one of them!
#3

[eluser]ramonskie[/eluser]
yes i want to show the forms without reloading..
do you know a good tutorial for Jquery to hid/show the views?
#4

[eluser]jdav3579[/eluser]
I would do something like below - its ugly code and Im certain there are better ways, but gives the idea. Like you could just alter the text of the button on toggle.

Checkout:
http://api.jquery.com/toggle/

Code:
<form class='formtoggle'>
<!-register form--&gt;

&lt;/form&gt;

&lt;form  class='hide formtoggle'&gt;
<!-login form--&gt;
&lt;/form&gt;

<button class='toggleforms'>Login</button>
<button class='toggleforms hide'>Register</button>

&lt;!-- need to get jquery here --&gt;
<sc*ript type='text/javascript' >
  $('.hide').hide();
  $('.toggleforms').click(function(e){
    $('.formtoggle').toggle();
    $('.toggleforms').toggle();
    e.preventDefault();
  })


</sc*ript>
#5

[eluser]ramonskie[/eluser]
i still can't get it to work
i load 2 view files in 1 view file called checkout
&lt;?php echo $this->view('cart/register'); ?&gt;
&lt;?php echo $this->view('cart/login'); ?&gt;

and now i want to toggle these 2 views
is that possible
or maby create a tabbed view
#6

[eluser]jdav3579[/eluser]
Could you post a section of the controller which loads the two views and also your view file that pulls it all together. I am not entirely clear on how this section of code works:
Code:
&lt;?php echo $this->view(‘cart/login’); ?&gt;


The way I'd do it is in the controller, load each of the views into the variables:
Code:
$data['register']=$this->load->view('cart/register',null,true);#3rd parameter returns data as string
$data['login']=$this->load->view('cart/login',null,true);
#Load the data into the main view
$data['register']=$this->load->view('main',$data);#Don't set third parameter - just output it!

Then in 'main' view;
Code:
&lt;?=(isset($register))?$register:''?&gt;
&lt;?=(isset($login))?$login:''?&gt;

I dont think you can load views directly within views, i have created code for doing this myself, but it breaks the paradigm a bit I think..
#7

[eluser]jdav3579[/eluser]
Also post your forms and I will look at JQuery for them for you
#8

[eluser]ramonskie[/eluser]
check out view
Code:
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;title&gt;CodeIgniter Shopping Cart&lt;/title&gt;

    &lt;link href="&lt;?php echo base_url(); ?&gt;assets/css/core.css" media="screen" rel="stylesheet" type="text/css" /&gt;

    [removed][removed]
    [removed][removed]

&lt;/head&gt;

&lt;?php if(!$this->cart->contents()):
    echo 'You don\'t have any items yet.';
else:
?&gt;
<div id="wrap">

&lt;?php echo $this->view('cart/register'); ?&gt; //this needs to be register or login

    <div class="cart_list">
        <table width="100%" cellpadding="0" cellspacing="0">
            <thead>
            <h3>Your Order</h3>
            <tr>
                <td>Qty</td>
                <td>Item Description</td>
                <td>Item Price</td>
                <td>Sub-Total</td>
            </tr>
            </thead>
            <tbody>
                &lt;?php $i = 1; ?&gt;
                &lt;?php foreach($this->cart->contents() as $items): ?&gt;

                &lt;?php echo form_hidden('rowid[]', $items['rowid']); ?&gt;
                <tr &lt;?php if($i&1) { echo 'class="alt"';  }?&gt;>
                    <td>&lt;?php echo $items['qty']; ?&gt;</td>

                    <td>&lt;?php echo $items['name']; ?&gt;</td>

                    <td>&euro;&lt;?php echo $this->cart->format_number($items['price']); ?&gt;</td>
                    <td>&euro;&lt;?php echo $this->cart->format_number($items['subtotal']); ?&gt;</td>
                </tr>

                     &lt;?php $i++; ?&gt;
                     &lt;?php endforeach; ?&gt;

                <tr>
                    <td></td>
                    <td></td>
                    <td><strong>Total</strong></td>
                    <td>&euro;&lt;?php echo $this->cart->format_number($this->cart->total()); ?&gt;</td>
                </tr>
            </tbody>
        </table>
        &lt;?php echo form_submit('', 'Confirm Your Order');?&gt;
    </div>
</div>
&lt;?php endif; ?&gt;
#9

[eluser]ramonskie[/eluser]
what i want to achieve is that every view file becomes a kind of module
that i can load whenever i want on some page
and if i want to change layouts or something that i only need to edit one file
#10

[eluser]jdav3579[/eluser]
I cheat a little myself sometimes for this reason - i am sure I will probably get told off for it, but here goes...
Create file in helpers: general_helper.php - you will need to load in your controller:

Code:
function view($page,$data,$return=false){
  $CI=& get_instance();

  $CI->load->view($page,$data,$return);

}


Then in your view, this function will be accessible and you can load any view you like, just pass it the data and file name!




Theme © iAndrew 2016 - Forum software by © MyBB