I have two flags, where you can click on to change the language.
But the weird thing is, it works but I have to click twice to stay in the language. (The session variable dissapears too if I go to a other page)
Like if I click one time on the flag, it doesn't stay with the session variable.
<?php
if(!isset($_SESSION['lang']))
{
$_SESSION['lang'] = "NL";
}
if($_SESSION['lang'] === "EN")
{
?>
<div class="language-selector">
<a href="page.php?id=1&lang=NL">
<img src="img/nlnon.png" onmouseover="this.src='img/nlact.png'" onmouseout="this.src='img/nlnon.png'"/>
</a>
<a href="page.php?id=1&lang=EN">
<img src="img/enact.png" onmouseover="this.src='img/enact.png'" onmouseout="this.src='img/enact.png'"/>
</a>
</div>
<?php
}
if($_SESSION['lang'] === "NL")
{
?>
<div class="language-selector">
<a href="page.php?id=1&lang=NL">
<img src="img/nlact.png" onmouseover="this.src='img/nlact.png'" onmouseout="this.src='img/nlact.png'"/>
</a>
<a href="page.php?id=1&lang=EN">
<img src="img/ennon.png" onmouseover="this.src='img/enact.png'" onmouseout="this.src='img/ennon.png'"/>
</a>
</div>
<?php
}
?>
This is the code to show the correct language for each page (only one right now)
<?php if($page_id == 1){
if(isset($_GET['lang']))
{
$language = $_GET['lang'];
if($language == "EN")
{
$_SESSION['lang'] = "EN";
include('includes/EN/onskantoorEN.php');
}
else
{
$_SESSION['lang'] = "NL";
include('includes/NL/onskantoorNL.php');
}
}
else
{
$_SESSION['lang'] = "NL";
include('includes/NL/onskantoorNL.php');
}
?>
To sum it up:
I want to click on <img src="img/nlnon.png>
or <img src="img/nlennon.png>
and let it immediately show the correct language, instead of clicking it twice.
I've wrapped the session variable around the link, so I'm not sure what's going wrong.
From your comments your first snippet is executing before you properly assign your session.
Try including header.php file after code where you evaluate and assign the language in session variables.
You can also include the code for initializing proper session variable for language in header.php file. Just make sure it gets executed first.
I think the second code snippet should be like this:
<?php
if ($page_id == 1) {
if (isset($_GET['lang'])) {
$language = $_GET['lang'];
$_SESSION['lang'] = $language; // todo: validate the GET parameter
} else {
$language = $_SESSION['lang'];
}
if ($language == "EN") {
include('includes/EN/onskantoorEN.php');
} else {
$_SESSION['lang'] = "NL";
include('includes/NL/onskantoorNL.php');
}
}
?>
Related
So prior to being introduced to CakePHP, I'd highlight the appropriate navigation tab according to the url with the following (rather sloppy) code I wrote (fyi absolute_url was a function I wrote to get the absolute path) :
$page = $_SERVER['PHP_SELF'];
// add all possible states for the navigation to an array
$checkNav = array(
"index" => "index",
"new" => "new",
"random" => "random",
"submit" => "submit"
);
$compareAgainst = strpos($page, $checkNav['index']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Popular</span></li>\n";
} else {
echo "<li>Popular</li>\n";
}
$compareAgainst = strpos($page, $checkNav['new']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">New</span></li>\n";
} else {
echo "<li>New</li>\n";
}
$compareAgainst = strpos($page, $checkNav['random']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Random</span></li>\n";
} else {
echo "<li>Random</li>\n";
}
$compareAgainst = strpos($page, $checkNav['submit']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">+ Submit a Link</span></li>\n";
} else {
echo "<li>+ Submit a Link</li>\n";
}
Now, I've noticed that in Cake, to determine the relative path, I can just go:
<?= $this->here; ?>
Is there a better way to do this, or should I just implement this (new) method with the old code?
You can do the following
Add this to app_helper.php if you need it in multiple pages. You feed this function with the controller and the action you want to check you want to compare against. The function compares it with the current page and return true if they match.
function isActive($controller, $actions = array())
{
foreach ($actions as $action)
{
if ($controller == $this->params['controller'] && $action == $this->params['action'])
{
return true;
}
}
return false;
}
And then generate your links like so:
<ul class="left">
<li <?php if($html->isActive('controller_name', array('index'))) { echo 'class="active"'; } ?>><?php echo $html->link('Index', '/index'); ?></li>
<li <?php if($html->isActive('controller_name', array('new'))) { echo 'class="active"'; } ?>><?php echo $html->link('New', '/new'); ?></li>
<li <?php if($html->isActive('controller_name', array('random'))) { echo 'class="active"'; } ?>><?php echo $html->link('Random', '/random'); ?></li>
<li <?php if($html->isActive('controller_name', array('submit'))) { echo 'class="active"'; } ?>><?php echo $html->link('Submit', '/submit'); ?></li>
</ul>
If the function returns true, the link will have class="active". Adapt it to your needs.
The way I've always done this is to give your body tag an id, and use css to target it. If your views are all separate then you can hard code the body id. If you are using some sort of template that adds in the header, content, footer etc., then just pass the id as a variable to the header view or wherever the body tag is (really any outer container/div that will be on every view and contain your navigation tabs). Also you will need to give your navigation tab id's to target each one.
Then just some css like this:
#homepage a#hometab,
#aboutpage a#abouttab,
#productpage a#productstab,
#contactpage a#contacttab
{
special active styling here
}
I want to show a DIV ONLY when on /../mahjong.php. So even if I go to /../mahjong.php?layout it should hide the div (since it's not the same url)
I have tried the following:
// We're NOT on the home page
if (strpos($_SERVER['REQUEST_URI'], "/games/mahjong/mahjong.php") >= 0) {
$style = "display: none";
}
else {
$style = "display: inline";
}
And my div ofcourse:
<div class="menu" id="menu" style="<?php echo $style; ?>">
But if I go to /games/mahjong/mahjong.php?layout it doesn't change the style. I've echoed:
echo $_SERVER['REQUEST_URI'];
and it changes to /games/mahjong/mahjong.php?layout, so why isn't the style set to inline?
if (strpos($_SERVER['REQUEST_URI'], "/games/mahjong/mahjong.php") === false) {
Didn't work either. (this wil show the div and never hide it) What am I missing?
Many thanks,
Maurice
Check if the $_GET array has been populated or not:
<?php if (empty($_GET)): ?>
<div>
...
</div>
<?php endif; ?>
Should be sufficient if you're not manually adding to the $_GET array, which would be very silly.
Change your condition to:
strrpos($_SERVER['REQUEST_URI'], '/games/mahjong/mahjong.php') === strlen($_SERVER['REQUEST_URI']) - strlen('/games/mahjong/mahjong.php')
This will make sure the request uri ends with that string.
strpos(); only search for that string, and in both cases, string is found
strpos(); isn'T exact search !
// We're NOT on the home page
if (strpos($_SERVER['REQUEST_URI'], "/games/mahjong/mahjong.php?layout") >= 0) {
$style = "display: inline";
}
else {
$style = "display: none";
}
or you can use
// We're NOT on the home page
if (isset($_GET['layout'])) {
$style = "display: inline";
}
else {
$style = "display: none";
}
this might help
Check whether PATH_INFO AND QUERY_STRING are empty...if not, then it's not the page you want.
What my current code does is, while checking the DB if the versions (FR and EN) are either True or False, display the proper content and if both exist to display a link so that users can switch languages. If only one language exists, the content is shown in that language and there is no link displayed.
the 2 functions in javascript are like this, here`s the FR one:
function makeVisibleFR()
{
document.getElementById('bbqc_contentFR').style.display="inline";
document.getElementById('bbqc_contentEN').style.display='none';
document.getElementById('vFrancais').style.display='none';
document.getElementById('vAnglais').style.display="inline";
}
What i`d like to add to this is the option of memorizing the user's choice and displaying the following pages with the same language version.
I imagine i'd need to create a $_SESSION['language'] variable and store in it either "FR" or "EN" but i`m not sure how to go about implementing that within my current code.
<?php
if($versionFR == true)
{
if($versionEN == true)
{
?>
Version Anglaise
<div id="bbqc_contentFR">
<h2><?php echo $titleFR; ?></h2>
<?php echo $contentFR; ?>
</div>
Version Française
<div style="display:none" id="bbqc_contentEN">
<h2><?php echo $titleEN; ?></h2>
<?php echo $contentEN; ?>
</div>
<?php
}
else
{
?>
<div id="bbqc_contentFR">
<h2><?php echo $titleFR; ?></h2>
<?php echo $contentFR; ?>
</div>
<?php
}
}
else
{
if($versionEN == true)
{
?>
<div id="bbqc_contentEN">
<h2><?php echo $titleEN; ?></h2>
<?php echo $contentEN; ?>
</div>
<?php
}
else
{
?>
<h2>Erreur, il n`y a aucun texte</h2>
<?php
}
}
?>
Here's a simplistic example:
// assuming that databaseHas() queries available languages
session_start();
$langs = array('ENG', 'FR');
$showlang = '';
if (databaseHas($_SESSION['lang']))
{
$showlang = $_SESSION['lang'];
}
else
{
foreach ($langs as $l)
{
if (databaseHas($l))
{
$showlang = $l;
break;
}
}
}
if ($showlang == '')
{
die('No languages found!');
}
echo databaseContent($showlang);
// print links to alternate languages
foreach ($langs as $l)
{
if (databaseHas($l) && $l != $showlang)
{
// print link to this language
}
}
put session_start() at the top of your file then:
if($versionFR == true) $_SESSION['lang'] = 'FR';
else $_SESSION['lang'] = 'ENG';
//later on (could be in a whole other page with session_start() on top)
if($_SESSION['lang'] == 'FR'){/*display FR stuff*/}
elseif($_SESSION['lang'] == 'ENG'){/*display ENG stuff*/}
Something like that should work well for ya ^_^
How would i go about adding that if 1)
both versions exist 2) lang is ENG 3)
english part is display:none by
default
1) You'd just add the clause: if($_SESSION['lang'] == 'FR' && $_SESSION['lang'] == 'ENG')
2) Not sure what you mean here, Neal explained it well from what I can see
3) If ENG is display:none by default, you'd want to fire off a javascript function to toggle it back on.
But let's take a step back here, consider this: Make two language files that define each piece of content. So for your english.php you might have variables such as $GREETING = 'Hello'; $YES = 'YES'; and then in your french.php you'd define these variables as $GREETING = 'Bonjour'; $YES = 'WEE'; (I'm not even sure if wee means yes, but you get the idea!). So now you can choose to include the appropriate language file based on the user's language, and you make it easy to add another language down the road. Be flexible!
I change the language in my site using PHP arrays and lang?=. When the user clicks a link to change the language of the site I want this link to keep "pressed" or change to a different color, so the user knows in which version of the site he/she is. How can I activate a CSS property in this situation?
common.php:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
case 'tw':
$lang_file = 'lang.tw.php';
break;
case 'cn':
$lang_file = 'lang.cn.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
lang.en.php:
<?php
$lang = array(
'h1' => 'Hello World',
);
?>
index.php:
<ul id="lang">
<li>English</li>
<li>Español</li>
<li>中文(繁體)</li>
<li>中文(简体)</li>
</ul>
You could test the value of $lang in the portion of code that generates the HTML output, and add a CSS-class on the link which corresponds to that language :
<ul id="lang">
<li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current_language"';} ?>>English</a></li>
<li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current_language"';} ?>>Español</a></li>
<li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current_language"';} ?>>中文(繁體)</a></li>
<li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current_language"';} ?>>中文(简体)</a></li>
</ul>
Depending on the value of $lang, one of the four links would have the CSS class current_language. Up to you to set it in your CSS file so it highlights the link which has it.
The generated HTML will then look like this (when $lang is 'en') :
<ul id="lang">
<li>English</li>
<li><a href="index.php?lang=es" >Español</a></li>
<li><a href="index.php?lang=tw" >中文(繁體)</a></li>
<li><a href="index.php?lang=cn" >中文(简体)</a></li>
</ul>
(Of course, you'll have to make sure the $lang variable is visible from the portion of code that generates the HTML output)
Just set an 'active' class for the link to the language which is currently in use.
<li><a href="index.php?lang=en" <?php if ($_GET['lang'] == 'english') echo 'class="active"' ?>>English</a></li>
Then in your CSS, have
#lang .active {
font-weight: bold;
}
Or however you wish to style it.
CSS:
#current
{
backgroiund-color:#00ff00;
}
html and php
<?php
$lang = $_GET['lang'];
?>
<ul id="lang">
<li><a href="index.php?lang=en" <?php if($lang == 'en') {echo 'id="current"';} ?>>English</a></li>
<li><a href="index.php?lang=es" <?php if($lang == 'es') {echo 'id="current"';} ?>>Español</a></li>
and so on.........
How do I get this to pull my 2nd variable? (I already have a switch setup)
<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">
I have a switch statement that pulls the pages from
index.php?page=#####
and I have just added this part to my switch:
index.php?page=####§ion=#####
Right now, if I am on page=photos, my code ends up being:
<body id="photos">
I need to make it so that if any link has the "sections" variable on it like this page=photos§ion=cars it uses the same ID:
<body id="photos">
First of all, a HTML element can only have one id. So if you want to create a hybrid (e.g. page-section) you can do something like this:
<body id="<?php echo isset($_GET['page']) ? $_GET['page'] : "home"; echo isset($_GET['section']) ? ("-".$_GET['section']) : ''; ?>">
For more information on Ternary Operators in PHP (the ? and : I used in the echo statement) see http://php.net/manual/en/language.operators.comparison.php
I am not entirely sure I understand your question, but where you're doing:
$_GET['page']; echo $page;
What do you think is happening? You're echoing a variable that has no definition. If you want to echo the value passed in the url, just do:
echo $_GET['page'];
GET doesnt mean your getting the varible, its the method by which the variable was passed to he page. The possible methods are get (in the url) or post (not).
Wouldn't that be an if to find out it if the section was defined? i.e.
if(isset($_GET['section'])){
//create div
} elseif(isset($_GET['page']){
//create fallback div
}
Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID = $_GET['section'];
} else if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
?>
...
<body id="<?php echo $bodyID; ?>">
Alternatively,
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
$bodyID='home';
foreach (array('section', 'home') as $key) {
if (isset($_GET[$key]) && isValidID($_GET[$key])) {
$bodyID = $_GET[$key];
break;
}
}
?>
...
<body id="<?php echo $bodyID; ?>">
In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.
If you decide you want both page & section in the ID, you can try something like:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID .= '_' . $_GET['section'];
}
?>
...
<body id="<?php echo $bodyID; ?>">