$data['main_content'] to load view in subfolder in CodeIgniter - codeigniter

Here is my function:
function single_resonator() {
$data['title'] = 'Single Resonator Operating Parameters';
$data['main_content'] = 'products/single_res_view';
$this->load->view('templates/main.php', $data);
}
But the single_res_view doesn't load if it is in the products folder in the views folder. How do I accomplish this? I tried a file called MY_router.php from a post elsewhere on this site and it didn't help. CI will only load the single_res_view if it is in the root of the views folder.
Here is templates/main.php
<?php $this->load->view('includes/header.php'); ?>
<?php $this->load->view('includes/nav.php'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer.php'); ?>

Have you tried this:
$data['main_content'] = 'products/single_res_view.php';
If not, can you triple check that the path and file names are correct? Otherwise, can you echo the value of $main_content just in case you're overwriting it somewhere?

Related

CodeIgniter access base_url() inside error_404.php

When 404 Page Not Found error occurs application/errors/error_404.php template is used in CodeIgniter. I need to access the base_url() function inside that template. Any suggestion on how base_url() or site_url() functions can be accessed inside error_404.php will be really helpful.
you might want to use this simple trick. Just change the code below:
<?php echo base_url() ?>
to:
<?php echo config_item(‘base_url’); ?>
credit for https://www.petanikode.com/codeigniter-base-url-404/
this work for my case
Try following in application/errors/error_404.php
$CI =& get_instance();
if( ! isset($CI))
{
$CI = new CI_Controller();
}
$CI->load->helper('url');
echo $CI->base_url();
Try this in top of your 404 page:
$CI =& get_instance();
if( ! isset($CI))
{
$CI = new CI_Controller();
}
$CI->load->helper('url');
echo base_url();
// below here base_url(), site_url() will work
Work for all pages under views/errors/html folder
you can directly use these functions, because these are global functions.
eg: you are in 404 page template
<html>
<head>
<title> 404 Error </title>
</head>
<body>
<?php echo base_url('/your path'); ?>
<?php echo site_url('/your path'); ?>
</body>
</html>
Edited
goto your application/config/autoload.php file and add url in autoload array
$autoload['helper'] = array('url', 'form');
after this you can directly use <?php echo base_url('/your path'); ?> everywhere
Edit your /application/config/routes.php
$route['404_override'] = 'your_controller/error';
Add function into your_controller.php
function error(){
show_404();
}
You are ready to go.
refer https://stackoverflow.com/a/48033542/7840849
or do followings
$autoload['helper'] = array('url'); in application\config\autoload.php file
Then simply use echo config_item('base_url'); instead of echo base_url(); in codeigniter default error pages
$autoload['helper'] = array('url'); in application\config\autoload.php file
Then simply use echo config_item('base_url'); instead of echo base_url(); in codeigniter default error pages
this work for my case, try this method
Change the function base_url() to config_item('base_url')
<?php echo config_item('base_url'); ?>
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';
**Then**
u can use like this..
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />

codeigniter - Flashdata not clearing

i'm new with codeigniter and use flashdata to display message from controller to view. However, flashdata is not cleared automatically after i refresh the view or move to the other pages and back it still remain. Please help.
Here is my code in controller:
$this->session->set_flashdata('_flash_message', 'Thanks for your subscription.');
redirect(site_url('cp/subscribe'), 'refresh');
In view:
<?php echo $this->session->flashdata('_flash_message'); ?>
I used XAMPP for localhost, already turn off cache mode.
Try
On Controller
$this->session->set_flashdata('flash_message', 'Thanks for your subscription.');
redirect(base_url('cp/subscribe'));
On View
<?php if ($this->session->flashdata('flash_message') {
<?php echo $this->session->flashdata('flash_message');?>
<?php }?>
In the view you need to add an if statement to check if the flashdata is set. When refreshing the browser it will not be set so the message will not reappear.
<?php if($this->session->flashdata('_flash_message')) : ?>
<?php echo '<p>' .$this->session->flashdata('_flash_message'). '</p>'; ?>
<?php endif; ?>

dynamically page load using codeigniter

helow, I am a newbie in codeigniter. I want to load my pages into my view dynamically. That means, Using a function.
For example i have 4 page home, about, contact, info. In my my view folder I have created a base layout where i include header and footer which is same for all those pages. now i want when i click those page link it just load into content body without loading full page. Using CI normal procedure i can do it. which is
function home(){
$data['main_content'] = 'home';
$this->load->view('template/layout', $data);
}
function about(){
$data['main_content'] = 'about';
$this->load->view('template/layout', $data);
}
but i want to create a function which call this pages into a single function for example,
function pageload($page){
}
this function check that requested file either exits or not exist. if exist it load the page otherwise load "page not found" 404 error page. How can I make it...plz help.
My view file is:
<nav>
<ul class="sf-menu">
<li class="current"><?php echo anchor('home/index', 'Home');?></li>
<li><?php echo anchor('home/amenities', 'Room & Amenitis');?></li>
<li><?php echo anchor('home/reservations', 'Reservations');?></li>
<li><?php echo anchor('home/photos', 'Photographs');?></li>
<li><?php echo anchor('home/pakages', 'Pakages');?></li>
<li><?php echo anchor('home/explore', 'Explore');?></li>
<li><?php echo anchor('home/contact', 'Contact');?></li>
</ul>
</nav>
And Controller:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller{
function __construct(){
parent::__construct();
}
function pageload($page){
$data['main_content'] = $page;
$this->load->view('template/layout', $data);
}
}
?>
You can do something lilke this
function pageload($page){
$data['main_content'] = $page;
$this->load->view('template/layout', $data);
}
if u dont want to show controller_name/pageload in url then in application/config/routes.php
add an array
$route['(home|about_us|other_page|another_page)'] = 'controller_name/pageload/$1';
and to load your 404 page just go to the application/config/routes.php
find this line $route['404_override'] and set its value to snything u want like
$route['404_override'] = 'page_not_found';
then in your controller just add a controller named page_not_found.php and load ur desired 404 custom view from that controller.
Try this function i think this will help you
function build_view($flake) {
$this->data['content'] = $this->load->view($flake, $this->data, TRUE);
$this->load->view('header', $this->data);
}
after you add this you need to add to other function like in index function
function index(){
$this->$data['main_content'] = $page;
$this->build_view('template/layout.php')
}

codeigniter: modify value in view from controller

I am trying to modified value from controller to view. I would like to achieve that when a user is signing up, and submited, I can return feedback from model to controller to view.
my view: main_view
<?php include('header.php'); ?>
<?php echo $sign_up_results ?>
<?php include('forms/forms.php'); ?>
<?php include('footer.php'); ?>
my controller
function __construct(){
parent::__construct();
$this->load->helper('url');
$template = $this->load->View('main_view');
}
function form_sign_up_controller(){
$this->load->model("form_sign_up");
$database_insert_results = $this->form_sign_up->insert_user_detail_into_db();
$data['sign_up_results']=$database_insert_results;
$template = $this->load->View('main', $data);
}
The problem is when the view is loaded, the value "$sign_up_results" is not yet defined. Is there anyway that I can define the value and then change the value according to the results return from model to controller.
just use is set to check if the parameter is defined or not
<?php if(isset($sign_up_results)) echo $sign_up_results ?>

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

Resources