New(relatively) to code igniter : some basic questions

Go To StackoverFlow.com

0

I'm typing this on an iPad so forgive me if being concise is a bit rude. My question is:

Is it ever ok to have simple logic inside a view? For instance,

<HTML>
<!-- ... Stuff
-->
<?php if($this->session->userdata('authorized'): ?>
<p>You are authorized</p>
<?php else: ?>
<p>You are not authorized</p>
<?php endif; ?>  // Question 1, is this the proper use of an endif:?
<!-- .. Stuff
->>
</HTML>

/* Starting to type the rest of message on my laptop.  Big thank yous to the coders on
   this site   who made my pc login transfer my unsaved, half typed iPad post */

The above was just a leftover comment in the code that would have made the limited php use look ugly. I wanted to let it be seen though.

Anyways, on to question #2:

Is it even proper to use a simple conditional like this in a view?

Thanks for reading, and hello again.

2012-04-05 19:57
by STONEYFTW


1

In answer to the first question: That is the proper use of an endif, all is valid and recommended way by codeigniter.

In terms of the second question, this method can be used in a view file; however I would recommend using it in the $data array passed to the page meaning it will be accessed as $authorised; I say this as it will make more sense to a front end designer.

Information on the $data array can be found here, just navigate to "Adding logic to the controller".

I hope this is of help to you.

2012-04-05 20:08
by Daniel
Thanks for the answers. A definite help - STONEYFTW 2012-04-05 20:42


1

Use the Language class to store your 'You are authorized' and 'You are not authorized' text. Do your session check in the controller, and pass the correct language value to the view in the data array.

Edit: Additional question from STONYFTW:

What approach should one take with a bit of more complex code, such as:?

    <?php if(!$this->session->userdata('isLoggedIn')): ?>
          <div id="login_form">
              <?php echo form_open('login/validateCredentials'); ?>
              <?php echo form_input('username', 'Username'); ?>
              <?php echo form_password('password', 'Password'); ?>
              <?php echo form_submit('submit', 'Log In'); ?>
              <div id="login_form_link_container">
                  <?php echo anchor('login/register', 'Register')." ".anchor('login/recover','Forgot Pass?'); ?>
              </div>
          </div>
   <?php endif; ?>
2012-04-05 20:26
by AndrewR
Same approach? I have never used the language class - STONEYFTW 2012-04-05 20:44
In that case, I think it would be better to go with the separate view suggestion in porquero's answer - AndrewR 2012-04-05 21:46
Thanks for the continued help. I appreciate it - STONEYFTW 2012-04-05 23:36


1

I recommend you to use differents views in the controller for each case:

// In the controller
if($this->session->userdata('authorized')
  $this->load->view('not_autorized.php');
else
  $this->load->view('view.php');

So you get clean code an views.

2012-04-05 21:14
by porquero
Would you then suggest having a view for each piece of "visual" on the page? I mean, is it acceptable to call lots of different views at the same time - STONEYFTW 2012-04-05 21:54
+1. You'll probably want to return the content of the view, and pass it into another view as a variable holding html content (i.e. <?= $auth_view; ?>) that you can output blindly - landons 2012-04-06 02:32
Yes is acceptable to call multiple views. You must think that a view is not necessary a html page. It can be any file - porquero 2012-04-10 15:08
Ads