I have several index pages
And I put the header page separately
I want the name of each page to be sent to the header
How can I do it, thank you?
model::setnameisset('home');
and model.php page
function setnameisset($name)
{
return $name;
}
Now I want it to be saved in the header page instead of the title tag
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<?= URL ?>">
<meta charset="utf-8">
<!-- <meta http-equiv="Content-Type" content="text/html"> -->
<title></title>
</head>
Now how do I call that function?
See I have several pages like contact page or home page
And I put the header in a page and included it in these pages
The structure is m v c
I just want to send the name of the page or the keyword tag to the header so that it opens with that name during execution.
For example, my index or contact file
<?php model::nametag('homepage'); ?>
<body id="page-top">
</body>
Now I have a model file in the core folder
My controller file is like this
class Controller
{
function __construct()
{
}
function view($viewurl,$data=[],$noincludeheader = '',$noincludefooter = ''){
if ($noincludeheader =='') {
require_once ('views/share-base/header.php'); //panel header
}
require_once ('views/'.$viewurl.'.php'); //panel main page
if ($noincludefooter =='')
{
require_once ('views/share-base/footer.php'); // panel footer
}
}
function model($modelUrl){
require_once ('models/model_'.$modelUrl.'.php');
$classname='model_'.$modelUrl;
$this->model = new $classname;
}
}
And my model file is like this
public function nametag($name)
{
return $name;
}
It's so easy, I just want to be placed in any file, a name should be passed to the header page, I don't know how to use the function.
Related
please help me out, I've been stuck on this snag for a WHILE now. Basically I am loading views in codeigniter and for some odd reason, I get some white-space and an apostrophe. It makes the whole page look weird.
This is what I get when I view source in the browser
'
<!DOCTYPE html>
<html lang="en">
<head>
<title>Propercade Property</title>
<meta charset="UTF-8">
<meta name="description" content="WEBLINERZ Landing Page Template">
<meta name="keywords" content="WEBLINERZ, unique, creative, html">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Favicon -->
<link href="application/views/img/favicon.ico" rel="shortcut icon"/>
This is the header file:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Propercade Property</title>
<meta charset="UTF-8">
<meta name="description" content="WEBLINERZ Landing Page Template">
<meta name="keywords" content="WEBLINERZ, unique, creative, html">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Favicon -->
<link href="application/views/img/favicon.ico" rel="shortcut icon"/>
I am seriously stumped. This is how I load the page:
$this->load->view('header-index');
$this->load->view('index',$send);
$this->load->view('footer');
Visually, this is what I see:
screenshot
Please let me know if you have encountered a similar issue and how might I resolve this
edit
controller function:
public function index(){
$this->load->model('Listing_model');
$data1 = $this->Listing_model->get_recent_listing();
$data2 = $this->Listing_model->get_featured_listing(6);//number of listings to get ~in case there are too many featured listings
$send = array(
"recents" => $data1,
"listings" => $data2
);
$this->session->set_userdata("page_name", "Index");
$this->load->view('header-index');
$this->load->view('index',$send);
$this->load->view('footer');
}
Turns out my output buffer was causing problems. Just needed to clean it.
ob_clean();
Funny how little things like this can cause so much issues
How to keep it DRY and make it much easier to debug, first of all you need to create a BASE_Controller which in CodeIgniter prefixed by default with MY_, anyways, in your application/core create a new file called MY_Controller and create a class in it an let it extend CI_Controller and here is the basic code you need to add to it:
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
// Your own constructor code
}
// now lets create our own render view
public function render_view($view)
{
$this->load->view('templates/header', $this->data);
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
Now in every other controller let it extend MY_Controller and use render_view() to load the view and you got your header and footer loaded for you and you don't need to repeat yourelf.
class Welcome extends MY_Controller
{
public function index()
{
// note that you already have global data array from my_controller, now you don't need to pass it here to the view, its already passed in render_view
$this->data['title'] = 'Hello World';
$this->render_view('your_view');
}
}
Of course there are way better approaches than this, you can create templates and use different layouts etc, but lets keep it simple for now, that's all you need.
Now in you view you can use $title like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title><?= $title ?></title>
...
I want to change the function name in the url, i searched for this a lot but unable to find any solution, can any one help me out what should i do
consider for example i have a url like
http://localhost/codeIgniter_try/index.php/Blog/blogvieww ,
so here "Blog" is the controller name and "blogvieww" is the function name so if i want to change the function name from "blogvieww" to "blogvieww_all" what can i do?
Blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
$this->load->view('blogview');
}
public function blogvieww()
{
$this->load->view('blogvieww');
}
}
?>
blogview.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
blogvieww.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 2nd Blog!</h1>
</div>
<div>
<h1>Welcome to my 3rd Blog!</h1>
</div>
</body>
</html>
Hope this will help you :
Add the below line of code in your route.php
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
Your anchor should be like this :
<a href=<?=site_url('Blog/blogvieww_all');?>
You can set it through routes of CodeIgniter.
Your path for routes will be application/config/routes.php
See this below may help.
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
For more detail: https://www.codeigniter.com/userguide3/general/routing.html
1) You can use URI routes. In routes.php, You can specify like below:
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
Check here for more info.
2) Write same function again in controller with the name of 'blogvieww_all' .
Change this in your controller:
public function blogvieww()
{
$this->load->view('blogvieww');
}
to
public function blogviewW_all()
{
$this->load->view('blogvieww');
}
You just need to change the name of the function in your controller, to the name you want to display on ur url.
Or
As Pradeep has commented, you can change to routing also. But the best way is to change the function name, unless it is being referenced or called from somewhere else.
I'm a bit confused on including a controller / view within another controller/view. I'm doing the following and getting funny rendering issues:
//CONTROLLER About
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function about(){ //the standalone about page
$data['about_inner'] = $this->about_inner();
$this->load->view('pages/about',$data);
}
public function about_inner(){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
$this->load->view('pages/about-inner',$data);
}
}
//view about.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container about" data-page="about">
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
</body>
</html>
The issue I'm getting is that $about_inner does not end up loading inside of the "scroll-container" - it loads and renders before everything else, as shown in the screenshot.
What's the best way to get the about_inner view and all of its associated data to load within the about view? I need all the content ($this->about_model->get_content()) to come from about_inner since it can also be loaded by other pages via ajax.
SOLUTION
public function about(){ //the standalone about page
$data['nav'] = $this->load->view('templates/nav', NULL, TRUE);
$data['about_inner'] = $this->about_inner(true);
$this->load->view('pages/about',$data);
}
public function about_inner($print =false){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
return $this->load->view('pages/about-inner',$data, $print);
}
//HTML
//view about:
<div class="container about" data-page="about">
<?=$nav; ?>
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
You just need to tell the about_inner function to return the data and not print the data. That's the 3rd argument to function view($template, $data, $return);
$this->load->view('pages/about-inner',$data, true);
If you need to do either or, just set a boolean flag as the argument to the function
function about_inner($print = false){
...
$this->load->view('pages/about-inner', $data, $print);
}
Then when you call the function you can simply pass true to the function in order to get it to return the HTML instead of printing the HTML.
$this->about_inner(true)
if your JS folder is in root you have to define it with base_url() function. So correct all the path in get_content method
Ex
<script src ="<?php echo base_url() ?>path/to/your/folder/aaa.js" .....
And i think you can keep one controller method and remove one.(You can keep about and remove about_inner)
New to classes, and I'm having an issue dynamically changing a page's title. I think I may know what the issue is, but I don't really know what to do in order to fix the problem. I have two pages, admin.php (where the class is housed) and display-admin.php (displays what the user sees). Within admin.php this is what I have:
class Admin {
public $title;
public function login() {
require("login-form.php");
return $this->title = "Login";
}
...
}
$o = new Admin();
This is what I have within display-admin.php:
<?php
include_once("admin.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
if(isset($o->title)) {
echo "<title>My Site | " . $o->title . "</title>";
} else {
echo "<title>My Site</title>";
}
?>
</head>
<body>
<?php
...
switch($action) {
case "login":
$o->login();
break;
default:
$o->displayPages();
}
...
?>
</body>
</html>
The title always stays as My Site. It never adds the title needed. Now, when I tested echoing $o->title at the end (before the <\body> tag), it displayed the string I passed. Is this because I am trying to echo the variable before calling the function? If so, how do I fix in order to display the title, and have my content displayed within the body?
Thanks in advance for the any/all help/suggestions.
In your case it isnt work because $o->title doesnt exist.
It is created in your "login()" method, and this is after your
if(isset($o->title)) {
So you can do this:
class Admin {
public $title;
public function __construct() {
$this->title = "Admin";
}
}
You can do it per example in the constructor of your Admin Class.
The constructur is a magic method. It will be automatically called directly if you created your class.
Hope this will help you.
If you create an object from class Admin the title will be change into 'Admin'
In your Admin Page you can do this:
$o = new Admin();
echo "<title>".$o->title."</title>";
I have an opencart site, and I'm trying to configure facebook share options of my products.
Since everything is loaded as a separate module, I can't set the facebook meta tags like this (header.tpl):
<meta property="og:description" content="<?php echo $description; ?>" />
Because the $description doesn't exist while the header module is loading.It is created in the controller of product module. So I tried to change the content value dynamically (product.tpl):
$("meta[property='og:description']").attr('content','<?php echo $description; ?>');
And it worked, I can see that the value is changed (in the page source), then I debugged my page but I couldn't get the value.. I think I know the reason, I have to set the value before the page load but I'm not sure how can I do that.. do you have any idea ?
You could use the Document class to add those facebook tags (as many as you want). Just add two extra methods setFacebookDescription and getFacebookDescription, so you have to add the followings:
<?php
class Document {
private $facebook_description;
public function getFacebookDescription() {
return $this->facebook_description;
}
public function setFacebookDescription($facebook_description) {
$this->facebook_description = $facebook_description;
}
}
On each controller, you will find at the end of each method, a call which loads the header of Opencart, something like this $data['header'] = $this->load->controller('common/header'); (example). Note that it might differ from yours, it depends on Opencart version.
Now, in header.php controller you add:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['facebook_description'] = $this->document->getFacebookDescription();
}
}
this will get the facebook_description variable and pass it to the view header.tpl. Next, add the facebook tags between your <head> tags in header.tpl file:
<!DOCTYPE html>
<head>
<?php if ($facebook_description != '') { ?><meta property="og:description" content="<?php echo $facebook_description; ?>" /><?php } ?>
</head>
Finally, you can set facebook_description in each controller, by calling $this->document->setFacebookDescription('my description');.
Example: in product.php controller you add
<?php
class ControllerProductProduct extends Controller {
public function index() {
// code...
$this->document->setTitle($product_info['meta_title']);
$this->document->setDescription($product_info['meta_description']);
$this->document->setFacebookDescription($product_info['meta_description']);
// the rest of the code...
}
}
here you set the $product_info['meta_description'] as facebook description tag, however you could also use $product_info['name'] or other variable.
Final note: you can change all the Opencart system classes with the vqmod.