I want the user can switch languages without changing the current page by clicking the language link.
I found a code like the one below. It works but cannot find the page because the page names are different. For example: When I change the language while on "../en/about.php" page, it goes to "../tr/about.php" page. The page that should go is "../tr/hakkimizda.php".
How can I solve this problem?
<?php
//URL path. eg: index-en/job.php
$path = trim($_SERVER['REQUEST_URI'],'/');
//language from URL. eg: index-en
$lang = explode('/',$path)[0];
//Paths in other languages: eg: 'tr' => 'index-cn/job.php'
$langs = [
'en'=>preg_replace("/$lang/",'../en',$path,1),
'tr'=>preg_replace("/$lang/",'../tr',$path,1),
];
?>
<ul class="dropdown-menu">
<li>
Türkçe
</li>
<li>
English
</li>
</ul>
You can use global arrays for each language and search current path into the corresponding array when you should get the key of this path. Then you can use the array for another language to search the path for this key, for example, you have:
$EN = array('about' => 'about.php');
$TR = array('about' => 'hakkimizda.php');
FIRST:
search the key for 'about.php' in the array $EN;
THEN:
use this key (which would be about) to search corresponding entry in the $TR.
I suggest, that these arrays should be global
I don't know php language but I solved the problem with javascript.
Solution:
<a class="nav-link" href="#" id="LangRedirect">English</a>
JavaScript
$("#LangRedirect").click(function LangRedirect(){
var lang,en,tr;
lang=window.location.pathname;
en="/en/";
tr="/tr/";
switch (lang){
// About
case en+"about.php": location.assign(tr+"hakkimizda.php");
break;
case tr+"hakkimizda.php": location.assign(en+"about.php");
break;
default: window.location.pathname=(en+"404.php");
}});
Related
I have a situation here. In my project the menu section contains some anchor tags, it will work perfectly when we in index page, but moving to other pages i want to give the real links there, so my question is how to check which page is viewing or how to check the the site viewer is not in index page
<li>Home</li>
<li>About Us</li>
I want to change href conditionally, for example when I'm in index the above href attribute is OK, and when I'm in another page, for example register, then the href attribute change to index.php/site/index#home
Thanks in advance
UPDATE :
thank you uttara,I found a solution with the help of her
<?php
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$href = ($filename=='root_directory' || $filename=='index' || $filename=='site')?'':Yii::app()->request->baseUrl;
?>
Home
You can use the CMenu widget, and this takes care of the highlights, appearance, etc. Plus you can use custom CSS afterwards.
<?php
$this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=> 'YOUR_URL#home'),
array('label'=>'About Us', 'url'=>'YOUR_URL#about_us)'
)
);
?>
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$filename will give you the name of current page being viewed
and you can check for
if($filename != 'index')
{
echo '<li>Home</li>
<li>About Us</li>';
}
else
{
echo '<li>Home</li>
<li>About Us</li>';
}
remember $filename gives you just the filename without extension
first give the id to anchor tag and then put condition like below
<?php
// the php code
$flag=strpos('index.php',$_SERVER['PHP_SELF'])
{
?>
<script>
$(document).ready(function(){
$("#idofanchortg").attr('href','hrefyou want to add');
});
</script>
<?php } ?>
you can use CMenu widget provided by Yii
http://www.yiiframework.com/wiki/211/creating-a-css-driven-drop-down-menu-using-cmenu/
First of all I prefer to leave responsibility in just one place.
<li>Home</li>
<li>About Us</li>
I prefer this way 'couse code is more clean. Well. Now we know that all our controllers extends Controller class (/protected/components/Controller.php). In this place we can add
public function getHomeHrefLink() {
// when I'm in index page the href attribute is #home
// when I'm in register page the href attribute is index.php/site/index#home
}
So come on: We can you $this->action (for controller name) and $this->action->id (for action name) to understand where we are:
public function getHomeHrefLink() {
return $this->createUrl('index/site', array());
// when I'm in register page the href attribute is index.php/site/index#home
return '#home;
}
I am new to CodeIgniter so I'm just trying to create a pretty basic site. I have 4 controllers/pages that I want to load, and a possibility to add a few more.
I have an array of items in my /applications/config/site.php file (which is autoloaded) as shown:
$site['MenuItems']['Home'] = "http://mysite.com/site/home";
$site['MenuItems']['Network Info'] = "http://mysite.com/site/info";
$site['MenuItems']['Staff'] = "http://mysite.com/site/staff";
$site['MenuItems']['Support'] = "http://mysite.com/site/support";
$config['site'] = $site;
I want to be able to take the $site['MenuItems'] array and echo out key/value pairs to place ultimately into my view page so that they are displayed as links on my site in the header. I want to be able to add and subtract items from this $site['MenuItems'] array as I need to to create more links in my header.
For example, in my view if I were to echo out the 'Home' => "http://mysite.com/site/home" key value pair:
<li>
Home
</li>
I'm not sure if I use $this->config->load('site','MenuItems') to do this...or what?
Thanks for any help you can provide me. Let me know if I'm missing something. It's probably something incredibly easy and I just can't grasp it right now :(
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('site');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks['MenuItems'] as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>
try this
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('MenuItems');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>
I have a small php website that has a menu that is included via include_once. I need an HTML class added to the current menu item for each page.
So if a user clicks on a link and goes to that page the new page will load with the new menu item with class="current". How can I go about doing this?
Let's say you write your menu like this:
<ul>
<li> Link1 </li>
<li> Link2 </li>
<li> Link3 </li>
</ul>
Here is a good old php4 style solution :-p
<?php
//you get which link was clicked
if(isset($_GET['menu']) $menu = $_GET['menu'];
else $menu = 1;
$1 = $2 = $3 = ""; //you create 3 empty strings
switch($menu){ //you assign the correct string with your css class name
case "1": $1 = 'class="current"';break;
case "2": $2 = 'class="current"';break;
case "3": $3 = 'class="current"';break;
}
?>
//echo the variables into the menu anchors ( <?=$1?> is equivalent to <? echo $1; ?> )
<ul>
<li><a href="link1.com?menu=1" <?=$1?> >Link1</a></li>
<li><a href="link2.com?menu=2" <?=$2?> >Link2</a></li>
<li><a href="link3.com?menu=3" <?=$3?> >Link3</a></li>
</ul>
You can also place the <?=$1?> into the <li> or anywhere else wanted...
If you're using javascript in your project, you could also give an id to your anchors (like Link1), get the menu value like above with $_GET, and then use javascript to add the class to the desired link.
I gave you this answer because from what you're writing I guess you're not using any PHP framework but coding you app the old way... there's plenty of nice PHP framework around here that will have pre-made solution for this... might be hard at the beginning but it's worth it...
Good luck!
I have a page that displays a lot of data, including Zend_Paginator.
The page action is /po/fetch?id=someID.
what i want to do is to pass the "id" parameter to zend paginationControl so the pagination links will be something like /po/fetch?id=someID&page=somePage. unfortunally i can't find anywhere explanation on how i can pass that parameter to the paginationControl.
my call to paginationControl:
echo $view->paginationControl($paginator, 'Sliding',$control, $params);
where $params = array('id' => someID
and my pagination partial is:
First
< Previous
<?php
foreach($this->pagesInRange as $page) {
?>
.$page. | ;
<?php
}
?>
Next >
Last
and I want $url to be of the specified form.
$this->id
Hey Try this, It will surely work.....
<a href="<?php echo $this->url(array('page' => $page,'id'=>'1')); ?>">
<?php echo $page; ?>
</a>
Here 1 is given as the id you have to pass the id you want.......
Like
<a href="<?php echo $this->url(array('page' => $page,'id'=>$param['id'])); ?>">
<?php echo $page; ?>
</a>
Your example code doesn't show how $url is populated, but you really should be using the URL ViewHelper.
So, for example - your previous link would become:
< Previous
This will return a proper URL to the current page with the page parameter set to $this->previous. So if the current url is /users/view?foo=bar&page=5, the above code would output /users/view?foo=bar&page=4 for the previous link. Notice how any query parameters that are already present are preserved.
So, if the id parameter is already present on the URL showing your paginator, the above code will "just work". However, if you still need to add the id parameter, you can do so like this:
< Previous
To continue from our previous example, this code would output the following url: /users/view?foo=bar&page=4&id={someId}
Here is the reference documentation for the URL ViewHelper:
url($urlOptions, $name, $reset):
Creates a URL string based on a named
route. $urlOptions should be an
associative array of key/value pairs
used by the particular route.
One last note - the $reset (third) parameter of the URL ViewHelper will come in very handy. The default behavior is to preserve any query parameters of the current request but calling $this->url(array(), 'default', true) with true for the $reset parameter will basically remove all parameters except for the ones you specify in $urlOptions.
I have gone through the same issue so I have used code given below in partial paginator.
I have created a function in paginator partial view file(control.phtml) or may be different.
function queryStr($p){
$params = $array = Zend_Controller_Front::getInstance()->getRequest()->getQuery();
$params['page']=$p;
$str='?';
foreach($params as $k=>$v){
if($v)
$str .= $k.'='.$v.'&';
}
return $str;
}
Now for links I am using code given below.
<a href="<?php echo queryStr($page); ?>">
Instead of
<a href="<?php echo $this->url(array('page' => $page)); ?>">
I am sure it will be helpful to others as well.
I normally use a GET variable i n the URL to determined what data to show in my template.
Today I am not passing a variable. I am including a file named inludes/leftmenu.php on every page but depending on the page name I want to show different data.
leftmenu.php looks like so:
$page = $_SERVER["REQUEST_URI"];
$page_name = get_page_name($_SERVER["REQUEST_URI"]);
if ($page_name == "classes.php")
{
<h2 class="left_h2">Class Schedules & Info</h2>
<ul class="left_ul">
<li>Immersion Programs</li>
<li>Afternoon Programs</li>
<li>Immersion Schedule</li>
<li>Afternoon Schedule</li>
</ul>
}else if($page_name == "about.php")
{
<h2 class="left_h2">About Us</h2>
<ul class="left_ul">
<li>About Us</li>
<li>Photo Scrapbook</li>
</ul>
}else if ($page_name == "news.php")
{
<h2 class="left_h2">News & Events</h2>
<ul class="left_ul">
<li>News</li>
<li>Events</li>
</ul>
}
You could use a PHP array:
$pages = array(
'/events.php' => 'includes/events_menu.php',
'/news.php' => 'includes/news_menu.php'
);
// lookup the appropriate include file
$uri = $_SERVER['REQUEST_URI'];
$include = $pages[$uri];
// produce a default page if the URI wasn't recognised
if (!isset($include)) {
$include = 'includes/default.php';
}
include($include);
I don't know if it's faster, but there are many ways to do this.
E.g. with an array:
$page_name = get_page_name($_SERVER['REQUEST_URI']);
$includes = array(
'events.php' => 'includes/events_menu.php',
'news.php' => 'includes/news_menu.php'
);
if(isset($includes[$page_name])) include($includes[$page_name]);
else die('this page does not exist');
PHP polulates $_GET with variables passed in the URL. Though I do not recommend having a user-passed variable as a function of what your code includes.
an array is a better method, ofcourse, but you may also look into realpath() and make sure it's in DOCUMENT_ROOT /includes
You can create array with paths and then use in_array function, which will reduce if statements.
If there's a menu for every single page, you could go with
include('includes/'.basename($page_name, ".php").'_menu.php');
You can use REQUEST_URI, but you have to keep in mind that it always starts with a / slash. Unless you apply basename() you therefore would have to write:
if ($page_name == "/events.php")
The if statements are a workable approach if you only need a few page names. Otherwise use an switch or an url->filename map (as an array).
If you are using a RewriteRule to map all incoming URLs to a single PHP script, you could also think about using PATH_INFO instead of REQUEST_URI.