Codeigniter - unable to load multiple views in controller - codeigniter

Hi everyone thank you for taking time to look at my question.
I have tried to run view (site_nav, site_header and site_footer) together only and it worked fine.
When I tried to run view (view_home) and the models it also worked fine.
However when I run all the views and models together, the view (site_nav, site_header and site_footer) does not work.
Could anyone please help?
public function home(){
$this->load->model("model_cms_home");
$data["results"] = $this->model_cms_home->getData("cms_home");
$this->load->view("site_nav");
$this->load->view("site_header");
$this->load->view("view_home", $data);
$this->load->view("site_footer");
}
VIEW("view_home")
<div id="home_hat1"> <img src="<?php echo base_url(); ?>pics/home_hat1.jpg"> </div>
<div id="content">
<div id="dinner">
<div class="home_title">
<?php
$query = $this->db->query("SELECT `title` , `text1` FROM `cms_home` WHERE `ID` =1");
if ($query->num_rows() > 0){
$row = $query->row_array();
echo $row['title'];
}
?>
</div>
<div class="home_content">
<?php
$query = $this->db->query("SELECT `title` , `text1` FROM `cms_home` WHERE `ID` =1");
if ($query->num_rows() > 0){
$row = $query->row_array();
echo $row['text1'];
}
?>
</div>
</div>

You cannot call multiple view in one controller function.
This can be done into view.
I suggest you that you should first create a templete and in that template call your views like this
templete.php
<html>
<head>
<body>
$this->load->view("site_nav");
$this->load->view("site_header");
<?php echo $content; ?>
$this->load->view("site_footer");
</body>
</head>
</html>

MY suggestion is to call other page in view_home using include();. You cant see other pages as the last page will be called according to your code. If you put an alert in each page you will know it has actually called all the pages.

Related

laravel rendering section no passing parameters data

i have a problem passing variable during the rendering of only a section
All works good but array of data passed to the section('sidebar') view create an error ($data doesn't exist)
My blade files are
Default.blade.php
..other html code before..
<body>
#include('includes.header')
<div class="container-fluid">
<div class="row">
#yield('sidebar')
<!-- main content -->
#include('includes.main')
</div>
<footer class="row">
#include('includes.footer')
</footer>
</div>
</body>
..other code after..
home.blade.php
#extends('layouts.default')
#section('sidebar')
#include('includes.sidebar')
#stop
sidebar.blade.php
..other html code before..
<h2>The current UNIX timestamp is {{ time() }}.</h2>
<ul>
#isset($data)
#foreach ($data as $item)
<li class="nav-item"> {{$item->polizza}}</li>
#endforeach
#endisset
</ul>
..other html code after..
Controller Method search
public function search(Request $request){
if ($request->ajax()) {
$data = Customers::select('id','contr_nom','email','targa','cliente')
->where('polizza',request('polizza'))
->get();
return view('pages.home',$data)->renderSections()['sidebar'];
}
//return json_encode($data);
}
I know that array $data is good because i try return just JSON and i know that just sidebar refresh because timestamp change.
But $data is not passed to sidebar section refreshed!!
Why?
Thks a lot
You have the right idea, you just need to send the variable in a form that will be recognized. I'll break it out to an extreme, to help understand the parts, but you can easily recombine for shorter code.
$view = view('pages.home', compact('data')); // Compact with the text name ('data') sends the variable through
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
return $sections['sidebar']; // this will only return whats in the sidebar section of the view
Using compact() should get your where you need, and is the key.

How do I call the function to check in a view?

How do I check if my database has more than 9 entries, and if it has, I can advance to the next page.
Currently I have this code, but I don't know how to call that in my view:
Here in the controller I have already called the checklogin and see if it has more than 9 entries.
Controller:
function quizzes(){
$this->load->model('Quiz_Model');
$data['categorys'] = $this->Quiz_Model->SearchAllCategorys();
$this->load->view('headerLogged');
$this->load->view('quizzes', $data);
$this->load->view('footer');
}
function proses_quiz($id_category){
$this->load->view('headerLogged');
$checkquiz=$this->Quiz_Model->checkquiz($id_category);
if($checkquiz > 9){
redirect('User_Ctr/quiz/').$id_category;
}else{
$data['error_quiz']="MEH";
$this->load->view('quizzes', $data);
}
$this->load->view('footer');
}
And here it's just the code to make the count:
Model:
public function checkquiz($id_category){
$this->db->select('id_question, COUNT(id_question) as total');
$this->db->from('questions');
$this->db->where('id_category', $id_category);
$result = $this->db->get();
return $result->result();
}
View:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="container">
<h1 class="my-4 text-center text-lg-left">Quizzes</h1>
<div class="row text-center text-lg-left">
<?php foreach($categorys as $cat): ?>
<div class="col-lg-3 col-md-4 col-xs-6" >
<a href="<?php echo 'quiz/'.$cat->id_category ?>" class="d-block mb-4 h-100" >
<img class="img-fluid img-thumbnail" src="<?php echo $cat->img_category ?>" alt="" style="height: 125px; width: 400px">
<?php
if(isset($error_quiz)){
echo $error_quiz;
}
?>
</a>
</div>
<?php endforeach ?>
</div>
</div>
I will answer you question generally.
If you want to call something in the view you have many options like you have the variable already calculated in advance and already passed it to view waiting for a call, or you can click on a link that maybe reload the current view with the new variable done, or you can use ajax to go get the variable from the controller without reloading.
So if the variable has fixed value that doesn't depend on your action, go with the first option or else chose one of the others.

Load an HMVC module view within an Iframe in Codeigniter

I'm new at this and some help will be apprciated.
I'm building a sidebar in a CI-Boilerplate-Project which contains modules (widgets) that i got run with HMVC https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc.
In the sidebar i have a widget that display a friendslist with status online/offline.
The user has the ability to switch the widgets on/off in the managementsection.
In the Profileview:
<aside class="sidebox right">
<?php foreach ($boxes as $boxName => $boxSetting)
{
echo Modules::run($boxName, $boxSetting['box_visible']);
}
?>
</aside>
if box_visible == 1 the widget will be displayed.
Controller:
class Myfriends extends SM_Controller
{
function __construct()
{
parent::__construct();
}
public function index($visible = false)
{
$user = $this->session->userdata('user');
$myf = $this->widget_model->get_friends($user['user_id'], 5);
$data['friends'] = $myf;
if ($visible) $this->load->view('myfriends', $data);
}
}
View:
<html>
<head>
<meta http-equiv="refresh" content="5">
</head>
<body>
<div class="box friendsbox">
<div id="header"><h3><?=$boxTitle?></h3></div>
<div id="boxcontent">
<ul>
<?php foreach ($friends as $friend): ?>
<li>
<div id="thb_img">
<img src="<?=img_thumb($friend['file_path'], 50, 50) ?>" />
</div>
<div id="short_desc">
<a href="<?= site_url('widget_functions/show_user/' . $friend['uu_id']) ?>">
<?= ucfirst($friend['user_name']) . ' ' . ucfirst($friend['user_lastname']) . ' ' ?>
</a>
<?php if ($friend['is_online']): ?>
<span style="color: green">online</span>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="footer">ยป mehr</div>
</div>
</body>
</html>
Now, i need to update the friendslist every 1-2 min so i tryed to load the moduleview within an iframe:
<aside class="sidebox right">
<?php foreach ($boxes as $boxName => $boxSetting): ?>
<?php if ($boxName == 'myfriends' && $boxSetting['box_visible'] == 1) { ?>
<iframe src="<?php echo site_url('myfriends/index'); ?>" ></iframe>
<?php
}
else
{
echo Modules::run($boxName, $boxSetting['box_visible']);
}
?>
<?php endforeach; ?>
</aside>
BUT this dose not work! The place of the widget is emtpy.
Do you have any idea how to get that to work?
appreciate your help
I believe the main issue is with the way you initialize the index method. the index method is kinda tricky with parameters in Codeigniter. In my projects, the only way to get the vlues of arguments passed to the index parameters is by using the URI library method $this->uri->segment(n). In other words, I believe that the value of $visible is not properly passing to the index() body
Anyway, I think you should create another method in your MyFriends Class called render() for example, and call it instead of relaying on the index() method. now render() can play nicely with the $visible=false initialization trick.
Hope this helps

CodeIgniter passing data into views

I need my data to get passed all the way from my controller -> page (view) -> users (view). How can I pass the data from my controller to the most nested view?
In my controller, I get some data called $users.
In my controller, I load a view called "page".
In my view called "page", I load views called "users".
Here is an example of how I think I can do it. Is this smart or incorrect?
In my controller I can do:
$data['users'] = $array_of_user_objects;
$this->load->view('page', $data);
Then in my page (view) I can do:
<div>
<h1>Im a page!</h1>
<?php $this->load->view('users', $users); ?>
</div>
Then in my users (view) I can do:
<h1>Hi my name is: <?php echo $name; ?></h1>
if you are doing things in controller like
$data['users'] = $array_of_user_objects;
$this->load->view('page', $data);
then in page(view) you can do simply
<div>
<h1>Im a page!</h1>
<?php echo $users->name ?> //if its an object otherwise $users['name']
</div>
and in your users(view)
<h1>Hi my name is: <?php echo $users->name; ?></h1> //if its an object otherwise $users['name']
please let me know if you face any problem.
in controller
$users = $array_of_user_objects;
$data['users'] = $this->load->view('users', $users, true);
$this->load->view('page', $data);
in 'page' view
<div>
<h1>Im a page!</h1>
<?=$users ?>
</div>
you do not need use <?php $this->load->view('users', $users); ?>
but <?php $this->load->view('users'); ?>
and you need a loop in your users view like this
<?php foreach($users as $user): ?>
Hi my name is: <?php echo $user->name; ?>
<?php endforeach;?>

Codeigniter - dynamic right menu

i have a right menu in my ci blog site that contains categories and no. of post that is subjected to change, has the following format,
Categories:
Science(24)
education(32)
....
....
the number is brackets is the total amount of post in that category.
my template file is here:
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
And my right menu is in the footer file.
How could i achieve this?
you better have a main view divided into divs(header, main_content,footer,right_menu) always loaded and load each view in the appropriate div.
<html>
<body>
<div id="header">
<?php $this->load->view('header'); ?>
</div>
<div id="body">
<div id="top_menu">
<?php $this->load->view('top_menu'); ?>
</div>
<div id="main_content">
<?php $this->load->view('main_content'); ?>
</div>
</div>
<div id="footer">
<?php $this->load->view('footer'); ?>
</div>
</body>
</html>
You can also use this system for including header/footer etc:
<?php
/**
* /application/core/MY_Loader.php
*
*/
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
Then, in your controller, this is all you have to do:
<?php
$this->load->template('body');
Code if from user: landons

Resources