Welcome Guest, Not a member yet? Register   Sign In
Problems with loading a set of views twice
#1

I have a big problem with loading a set of views twice. Doing that, the Stylesheets are not loaded.
 
What I want to do is:
 
On a successful login, the views ...
 
·     header (which contains the links for the stylesheets)
·     submit (which contains a form to be submitted by a button)
·     footer
 
... are loaded by the contoller. The stylesheets works fine.
 
When I click the button of view submit, the submitting  of the appropriate form calls the controller function get_data().
 
This function get_data() should finaly load the views..
 
·     header
·     submit
·     footer
 
... again and additional the content view (after the submit view). But this leads to my problem. The views are loaded but without the entire stylesheets.
 
Does someone have an idea to solve the problem ?

The Controller:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* User class.
*
* @extends CI_Controller
*/
   

   
class User extends CI_Controller {

    /**
     * __construct function.
     *
     * @access public
     * @return void
     */
   
   
    public function __construct() {
        
        parent::__construct();
        $this->load->library(array('session'));
        $this->load->helper(array('url','array'));
        $this->load->model('user_model');
       
    }
    
    
    public function index() {
        
       
    }
    

    public function login() {
    
        // create the data object
        $data = new stdClass();
        
        // load form helper and validation library
        $this->load->helper('form','array');
        $this->load->library('form_validation');
        
        // set validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
       $data->title = 'Login';
       
        if ($this->form_validation->run() == false) {
            
            // validation not ok, send validation errors to the view
            $this->load->view('header');
            $this->load->view('user/login/login');
            $this->load->view('footer');
            
        } else {
            
            // set variables from the form
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            if ($this->user_model->resolve_user_login($username, $password)) {
                
                $user_id = $this->user_model->get_user_id_from_username($username);
                $user    = $this->user_model->get_user($user_id);
                
                // user login ok
                $this->load->view('header');
               $this->load->view('user/login/submit');
                $this->load->view('footer');
                
            } else {
                
                // login failed
                $data->error = 'Wrong username or password.';
                
                // send error to the view
                $this->load->view('header');
                $this->load->view('user/login/login');
                $this->load->view('footer');
                
            }
            
        }
        
    }
    
    /**
     * logout function.
     *
     * @access public
     * @return void
     */
    public function logout() {
        
        // create the data object
        $data = new stdClass();
        
        if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
            
            // remove session datas
            foreach ($_SESSION as $key => $value) {
                unset($_SESSION[$key]);
            }
            
            // user logout ok
            $this->load->view('header');
            $this->load->view('user/logout/logout_success', $data);
            $this->load->view('footer');
            
        } else {
            
            // there user was not logged in, we cannot logged him out,
            // redirect him to site root
            redirect('/');
            
        }
        
    }
   
   
   public function delDataset($id) {

       echo "<br> Delete für ID : ".$id;

       $this->load->view('header');
       $this->load->view('user/login/submit');
       $this->load->view('user/login/content');
       $this->load->view('footer');

   }
   
   
   
   
   public function updDataset($id) {
       
       echo "<br> Update für ID : ".$id;

       $this->load->view('header');
       $this->load->view('user/login/submit');
       $this->load->view('user/login/content');
       $this->load->view('footer');
 

       
   }
   

   
   
   public function get_data() {
       
       $this->load->view('header');
       $this->load->view('user/login/submit');
       $this->load->view('user/login/content');
       $this->load->view('footer');
       
   }
   

}

The header view:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- meta name="viewport" content="width=device-width, initial-scale=1" -->
   <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
   <title>Site title</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="">

    <!-- css -->
   <link href="css/bootstrap.min.css" rel="stylesheet">
   <link href="turnover.css" rel="stylesheet">

    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
   <script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

    <header id="site-header">

       <div class="container div-narrow">

<!--
           <ul class="nav navbar-right">
               <li class="nav-item">
                   <a class="nav-link active" href="#">Active</a>
               </li>
               <li class="nav-item">
                   <a class="nav-link" href="#">Link</a>
               </li>
               <li class="nav-item">
                   <a class="nav-link" href="#">Link</a>
               </li>
               <li class="nav-item">
                   <a class="nav-link disabled" href="#">Disabled</a>
               </li>
           </ul>
       </div>

-->


        <nav class="navbar navbar-default" role="navigation">
            <div class="container">
                <div class="navbar-header">
                   <!--
                   <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                   -->
                    <a class="navbar-brand" href="<?= base_url() ?>"> </a>
                </div>


                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav navbar-right">
                        <?php if (isset($_SESSION['username']) && $_SESSION['logged_in'] === true) : ?>
                            <li><a href="<?= base_url('logout') ?>">Logout</a></li>
                        <?php else : ?>
                            <li><a href="<?= base_url('index.php/User/register') ?>">Register</a></li>
                            <li><a href="<?= base_url('index.php/User/login') ?>">Login</a></li>
                        <?php endif; ?>
                    </ul>
                </div><!-- .navbar-collapse -->
            </div><!-- .container-fluid -->
        </nav><!-- .navbar -->

    </header><!-- #site-header -->

The submit view:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div class="container">



   <div class='filter' id='bereich'>

       <br>
       Umsaetze filtern nach...
       <br><br>

       <form method="post" action="user/get_data">


       <button type='submit' class='btn btn-default btn-sm' id='findData' type='button' value='suchen'><span class='glyphicon glyphicon-search'></span> Daten suchen</button>
       <br>
       </form>
   </div><!--- bereich -->
   <br>

The content view:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div id='bereich' class='table-responsive data container'>

   <input Type='hidden' name='action' id='action'>
   <input type='hidden' name='selectedID' id='selectedID'>

   <table class="table table-condensed table-hover">

       <tr>
       <td>01.10.2018</td>
       <td class='cell-left'>Automatengeld</td>
       <td class='text-right'>100,00</td>
       <td class='text-right'></td>
       <td class='text-right'>
       <form method='post' action="user/delDataset/1000">
       <button type='submit' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-trash'></span> </button>
       </form>
       <form method='post' action="user/updDataset/1000">
       <button type='submit' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-pencil'></span> </button>
       </form>
       </td>
       </tr>
       <tr>
       <td>02.10.2018</td>
       <td class='cell-left'>Überweisung an Simone</td>
       <td class='text-right'></td>
       <td class='text-right'>51,50</td>
       <td class='text-right'>
       <form method='post' action="user/delDataset/1001">
       <button type='submit' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-trash'></span> </button>
       </form>
       <form method='post' action="user/updDataset/1001">
       <button type='submit' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-pencil'></span> </button>
       </form>
       </td>
       </tr>
       <tr>
       <td>03.10.2018</td>
       <td class='cell-left'>Überweisubg an Simone</td>
       <td class='text-right'>123,45</td>
       <td class='text-right'></td>
       <td class='text-right'>
       <form method='post' action="user/delDataset/1002">
       <button type='submit' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-trash'></span> </button>
       </form>
       <form method='post' action="user/updDataset/1002">
       <button type='submit' class='btn btn-default btn-xs'><span class='glyphicon glyphicon-pencil'></span> </button>
       </form>
       </td>
       </tr>
   </table>

</div><!--- bereich -->
 
The footer view:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

</div><!-- .container -->

    <footer id="site-footer" role="contentinfo">
    </footer><!-- #site-footer -->

    <!-- js -->
    <!-- script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script -->
    <!-- script src="<?= base_url('assets/js/bootstrap.min.js') ?>"></script -->
    <!-- script src="<?= base_url('assets/js/script.js') ?>"></script -->

   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   <!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
   <!-- script src="<?= base_url('assets/js/JS_Umsatz.js') ?>"></script -->


</body>
</html>
Reply
#2

(This post was last modified: 02-03-2018, 10:49 AM by InsiteFX. Edit Reason: spelling ERROR )

There are several undocumented methods that are used with views.

For more information on this open up ./system/core/loader.php and view it.

PHP Code:
// Clear out the view variables.
$this->load->clear_vars();

// Add view variables.
$newData = ['key' => 'myData'];
$this->load->vars($newData);

// Get a variable
$myVar $this->load->get_var($key);

// Get all variables.
$data $this->load->get_vars(); 


Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

I tried this already, but it doesn't help...
Reply
#4

I don't fully understand the flow of your application, but I suggest to redirect to another url after a succesful login.
Otherwise, the url will remain user/login, and I wonder if that's what you want.
In general, on succesful login, a user is redirected to the home page, or some other page that tells the user he is logged in now.

Also consider using some kind of rendering function in a MY_Controller class.
Example:

PHP Code:
protected function render_page($view,$data=array())
{
    
$this->load->view('theme/header');
    
$this->load->view($view,$data);
    
$this->load->view('theme/footer');        


Now, in your controller, you just need this code to load a specific view:
PHP Code:
$this->render_page('login'); 

This will load the header view, then the login view, and finally the footer view.
Reply
#5

(02-04-2018, 10:52 AM)Wouter60 Wrote: I don't fully understand the flow of your application, but I suggest to redirect to another url after a succesful login.
Otherwise, the url will remain user/login, and I wonder if that's what you want.
In general, on succesful login, a user is redirected to the home page, or some other page that tells the user he is logged in now.

Also consider using some kind of rendering function in a MY_Controller class.
Example:

PHP Code:
protected function render_page($view,$data=array())
{
    
$this->load->view('theme/header');
    
$this->load->view($view,$data);
    
$this->load->view('theme/footer');        


Now, in your controller, you just need this code to load a specific view:
PHP Code:
$this->render_page('login'); 

This will load the header view, then the login view, and finally the footer view.

My code is only an example how I understand CodeIgniter. I attached a graphic, which shows my idea how the MVC of CodeIgniter works. Hope you understand my intensions. When I am wrong, please tell me

Attached Files
.pdf   Sample_Workflow.pdf (Size: 37.2 KB / Downloads: 24)
Reply
#6

You really should be using a MY_Controller because you can separate
your backend form your frontend etc;

Save as: ./application/core/MY_Controller.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/12/2018
 * Time     : 7:40 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     ci3admin
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

/**
 * Class BaseController
 */
class BaseController extends CI_Controller
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */


    /**
     * Data variable for views.
     *
     * @var  array
     */
    
public $data = [];

    
/**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
log_message('debug'"Base Controller Class Initialized");
    }

 
   /**
     * render ()
     * -------------------------------------------------------------------
     *
     * @param string     $page
     * @param array|null $params
     */
 
   public function render(string $page, array $params NULL)
 
   {
 
       if ($params !== NULL)
 
       {
 
           $this->data['data'] = $params;
 
       }

 
       $this->data['page'] = $page;

 
       $this->load->view($page$this->data);
 
   }

 
   // -----------------------------------------------------------------------

  // End of Base Controller Class.

/**
 * Class Backend
 */
class Backend extends BaseController
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
log_message('debug'"Backend Controller Class Initialized");
    }

    
// -----------------------------------------------------------------------

  // End of Backend Controller Class.

/**
 * Class Frontend
 */
class Frontend extends BaseController
{
    
/**
     * Class properties - public, private, protected and static.
     * -------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
log_message('debug'"Frontend Controller Class Initialized");
    }

    
// -------------------------------------------------------------------

  // End of Frontend Controller Class.

// End of MY_Controller Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/controllers/MY_Controller.php
 * -----------------------------------------------------------------------
 */ 

Then extend all of your other controllers with either ( Backend or Frontend )

Like so:
Save as: ./application/controllers/admin/Users.php
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 2/3/2018
 * Time     : 7:34 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        Users
 *
 * @project     ci3admin
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

class Users extends Backend
{
 
   /**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

 
   /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
 
   public function __construct()
 
   {
 
       parent::__construct();

 
       // restrict this controller to admins only
 
       $this->auth->restrict('admin');

 
       log_message('debug'"Users Controller Class Initialized");
 
   }

 
   /**
     * index ()
     * -----------------------------------------------------------------------
     *
     */
 
   public function index()
 
   {

 
   }

 
   /**
     * add ()
     * -------------------------------------------------------------------
     *
     *
     */
 
   public function add()
 
   {

 
   }

 
   /**
     * edit ()
     * -------------------------------------------------------------------
     *
     *
     */
 
   public function edit()
 
   {

 
   }

 
   /**
     * retrieve ()
     * -------------------------------------------------------------------
     *
     *
     */
 
   public function retrieve()
 
   {

 
   }

 
   /**
     * delete ()
     * -------------------------------------------------------------------
     *
     * @param int $id
     */
 
   public function delete(int $id)
 
   {
 
       $this->db->where('id'$id)
 
                ->delete('users');

 
       $this->render('users/delete_success');
 
   }

 
   /**
     * manage ()
     * -------------------------------------------------------------------
     *
     * Manage users in the users database table.
     *
     */
 
   public function manage()
 
   {
 
       $this->load->library('table');

 
       $data $this->db->get('users');

 
       $result $data->result_array();

 
       // Setting headings for the table
 
       $this->table->set_heading('Username''Email''Actions');

 
       foreach ($result as $value => $key)
 
       {
 
           // Build actions links
 
           $actions anchor("admin/users/edit/".$key['id']."/""Edit").
 
                      anchor("admin/users/delete/".$key['id']."/""Delete");

 
           // Adding row to table
 
           $this->table->add_row($key['username'], $key['email'], $actions);
 
       }

 
       // Load the view
 
       $this->render('users/manage');
 
   }

 
   // -----------------------------------------------------------------------

  // End of Users Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: Users.php
 * Location: ./application/controllers/admin/Users.php
 * -----------------------------------------------------------------------
 */ 

That should give you a start.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB