Codeigniter if controller - php

First of all sorry if its a noob question.
But is it posibble to do this in codeingiter, like if i have a sidebar but i only want to load it in 2 pages
if(controller == 'blog') {
//load sidebar
}
just like in wordpress if is_page

Use $this->router->fetch_class()
if($this->router->fetch_class() == 'blog') {
//load sidebar
}
Also $this->uri->segment(2) will work in most cases, but in some cases like mod_rewrite or when using subfolder or route it may fail.

More simply you can do like this.
$controller_name = $this->CI->router->fetch_class();
if($controller_name === "your_controller_name")
{
//your logic
}

Related

WordPress - PHP conditional logic

I have a really simple question about PHP conditional logic. I have some code where I want to load a different header file based on the post category type. The code below works:
if(in_category('news')) {
get_header('tertiary');
}
elseif(in_category('events')) {
get_header('tertiary');
}
else {
get_header('secondary');
}
But when I try to simplify the code to:
if(in_category('news' || 'events)) {
get_header('tertiary');
}
else {
get_header('secondary');
}
The tertiary header file for the events is not loading, it is showing the secondary header file. I'm using similar code elsewhere in my theme and it is working with no issues. So I'm not sure why it is not working here. I'm not getting an errors in my PHP console.
There may be two ways you can do this. Check in_category individually (this will always work)
if(in_category('news') || in_category('events')) {
Or in_category can take an array, in some versions of Wordpress:
if(in_category(['news', 'events'])) {
You need to read the documentation and instead of
if(in_category('news' || 'events)) {
...
do
if(in_category(['news', 'events'])) {
...
You need to read the documentation
if ( in_category( Array( 'news', 'events' ) ) ) {
get_header('tertiary');
} else {
get_header('secondary');
}

How to best implement dynamic url/controller name which is database driven?

I have a website that is written in PHP on codeigniter. The scenario I am looking to cope with is the following:
When a user signs up, Their personalised homepage should appear at www.mysite.com/username. Other urls are of the form www.mysite.com/username/pictures & www.mysite.com/username/videos.
So basically what I need is www.mysite.com/username/method (where username is database driven) should always hit the same controller, if the username exists in the database.
Currently, what I am doing is I am using the custom 404 controller to do this. If a URL reaches the custom 404 controller, I check if the controller name matches a username in the data base, then checking the method name and calling the required method. Like so:
$this->load->model('school_model');
$this->load->model('event_model');
$this->load->helper('apps_helper');
$controllerName = $this->uri->segment(CONTROLLER);
$controllerMatch = $this->school_model->findSchoolByUid($controllerName);
$this->data['controller'] = $controllerMatch;
if($controllerMatch != false){
$methodName = $this->uri->segment(METHOD);
if($methodName === "something"){
//Do something
}
if($methodName === "something else"){
//Do something else
}
if($methodName === "another thing"){
//Do another thing
}
if($methodName === "last thing"){
//Do last thing
}
...
}
else{
//Load 404 page
$this->output->set_status_header('404');
$data['content404'] = true;
$this->load->view("common/header", $data);
$this->load->view('frontend/index404', $this->data);
$this->load->view("common/footer");
}
My query is, is this the best way to go about what I need ? How can I improve this ? I have heard of the HMVC modular extension for codeiginiter which may be a good bet ? Just looking for some advice.
You can do this by writing this code in your config > routs.php file
$route['(:any)/pictures'] = 'yourcontrollername/functionName/$1';
now in this if any one write username/pictures url than it will automatically goes to your controller.

cannot get drupal 7 to recognize a page template for a content type

I have a drupal 7 site that I want to make secondary front pages on. the problem with this is the "page--front.tpl.php" is a two column layout and the "page.tpl.php" is a one column layout. if i use a node template it shoves it in the body of the one column.
the theme name is "egress" the machine name for the content type is "landing" but when i try to hook the page--landing the same way i do the node--landing nothing happens. nothing.
i am clearing the cache and hard refreshing the page with every change of the template files.
one code i have tried in the of the "page.tpl.php"
function egress_preprocess_page(&$vars) {
global $node;
if ($node->type == 'landing') {
$vars['theme_hook_suggestions'] = array('page__landing');
}
}
another
function egress_preprocess_page(&$vars) {
if ($vars['node']->type == "landing_page") {
$vars['template_files'][] = 'page--landing';
}
}
anyone ideas?
The following should go to your theme's template.php rather than page.tpl.php
function egress_preprocess_page(&$vars) {
// For page--(node-type).tpl.php
if (isset($vars['node'])) {
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}

Wordpress personalized permalink

Bonjour!
I'm using a translation plugin on my web site that doesn't display on mobile display (column where the plugin is displayed is not visible on mobile).
When I put my article in english, the url becomes:
frenchyincali.com/my-article/**?lang=en**
Thus, I'd like to include a link in my code that is very basic: Getting the permalink and add the suffix "?lang=en" for the english link and just the default permalink for the default language (French).
I guess it'd be something like <?php the_permalink();>... But then I don't know what to put to add the suffix. Can you help me please?
I tried to find something elsewhere but I can't find the answer; Thanks.
Displays the URL for the permalink to the post currently being processed in The Loop.
English Article
Add functions.php
function lang() {
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if($lang == "en") {
$suffix = "?lang=en";
} elseif($lang == "tr") {
$suffix = "?lang=tr";
} else {
$suffix = "";
}
return $suffix;
}
Use like this:
Article
I think this way is simple.

Codeigniter--add “active” css class, how to apply on a link?

i have a url like this
http://localhost/bestbookfinder.com/viewallbooks/books/pgn/grid/6
where:
bestbookfinder.com: Project name
viewallbooks : class name
books :function
pgn :Constant Parameter
grid :view type
6 :pagination current page number
so now i am trying to read the above shown url and to apply the CSS class with the following code, but unfortunately my code is not working.
I think i am making mistake when i tried to read the url.
Please help me to solve this problem
<?php
if ( $this->uri->uri_string() == '/books/pgn/grid' )//i think mistake is here
{
echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => 'gridactive'))."";
}
?>
From the codeigniter manual - uri_string() returns a string with the complete URI. So in your example that would be "viewallbooks/books/pgn/grid/6".
Therefore "viewallbooks/books/pgn/grid/6" != '/books/pgn/grid'
Just do something like this:
if ($this->uri->segment(4) == "grid")
{
// do something
}
if($this->uri->segment(4) == 'grid'){
//carry on

Categories