My website is working with 2 languages.
I used session for that purpose. When a user clicks on a language icon the language is changed and everything is fine, but when the website is opening for the first time without choosing the language the default language is not loaded and the words do not appear.
This is my language.inc.php file
<?php
session_start();
$langForSelection = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en'; //default to english language
if(isset($_GET['langSelect'])){
//allow only 2 for now ( english / turkish)
$langForSelection = "";
switch($_GET['langSelect']){
case 'en':
$langForSelection = 'en';
break;
case 'tr':
$langForSelection = 'tr';
break;
default:
break;
}
if(isset($langForSelection)){
$langForSelection2=$_SESSION['lang']=$langForSelection;
// setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days
}
}
here is the icons where user choose the language
<div><img src="images/united-kingdom.png" class= "active" style="float: right; width: 24px;height:24px ;padding: 4px">
<img src="images/turkey.png" style="float: right; width: 24px;height:24px; padding: 4px">
here I print the translate of the words
this is an example
require_once('inc/languages.inc.php');
require_once('Languages/common.inc.php');
$langForSelection2=$_SESSION['lang'];
<li><?php echo $arrLang[$langForSelection2]['log_login']; ?></li>
Again my problem is reproduced when the web page is loaded and before a user clicks on language icon the words not appear but when he click on the language click everything works fine.
You can change your switch to default to English:
switch($_GET['langSelect']){
case 'en':
$langForSelection = 'en';
break;
case 'tr':
$langForSelection = 'tr';
break;
default:
$langForSelection = 'en';
break;
}
The reason of your problem probably is that your $_SESSION has a lang value, which is different from your expectations, like 'en-us'. If you want to know the exact cause, you can reproduce the problem with a new session and calling var_dump($_SESSION); If it has a lang, then it is the exact reason of your problem.
EDIT:
The solution shown here was not enough, since this part
if(isset($langForSelection)){
$langForSelection2=$_SESSION['lang']=$langForSelection;
// setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days
}
was inside the if. You will need to move this outside the if.
Related
As many, I am having problem staying on the same page when changing language.
I have read many questions and answers but they didn't work for me. I tried to work it out, but I can't.
I am a student and I started coding this year. I work with Twig and it confuses me more.
Here is my php file for detecting the language
<?php
if(isset($_GET['lng'])){
$lng = $_GET['lng'];
if($lng !== '' && ($lng === 'fr' || $lng === 'en'))
$_SESSION['langue'] = $lng;
}
if(isset($_SESSION['langue'])) $userLang = $_SESSION['langue'];
else $userLang = 'en';
?>
In the index.php I have written
$page = "index";
if( isset ($_GET['page']) ) $page = $_GET['page'];
$pagePHP = "home";
switch( $page )
{
case "whatwedo" : $pagePHP = "whatwedo"; $current = "whatwedo"; break;
default : $pagePHP = "home"; $current = "home"; break;
}
$view->setVar('currentPage', $page);
include "pages/$pagePHP.php";
And this is my layout.html.twig
<div id="menulangue">
<div class="btnlanguess">Français</div>
<div class="btnlangue">English</div>
</div>
I tried to change the a href's link in several ways. The last two I remember:
a href="index.php?page={{ currentPage }}?lng=fr"
a href="{{ currentPage }}?lng=fr"
But it doesn't work. My url of the index is index.php?lng=en and of another page(no matter if it is in English or French) is index.php?page=whatwedo.
In the above cases, when clicked on the change of language, not only it returns to the index page but the url of the index page become as follows
case 1. index.php?page=equipe?lng=fr and in that case if chosen another page it continues to the language by default.
case 2. whatwedo?lng=fr and the page is not found: Not Found
The requested URL /APL/whatwedo was not found on this server.
Posting this as a community wiki answer, seeing it was solved in comments.
Me:
"index.php?page=equipe?lng=fr - the second argument should use & and not ?"
OP:
"Wow, thank you very much, Fred! You are a genius-I have change in the first case the ? with & and it worked perfectly! Thank you very much!"
I have a cms admin section with the option comments; when i press comments on the admin menu it should take me to comments.php which includes the code below; by default it should load all my comments on a grid, however when i press comments its not displaying anything the page is blank ? My code:
<?php
if(isset($_GET['source'])){
$source1=$_GET['source'];
if(!empty($source1)){
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
}
}
?>
when u load comments.php the url doesnt sent any parameter via get, on your code you are checking if the source is not empty which on your case its empty at first. Then preform switch statment, in your case it will not preform the switch as the source is empty and thats why you are not seeing anything. You can fix that by adding an else on your if and include view_all_comments.php or like the code below:
<?php
$source1=isset($_GET['source']);
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
?>
I am creating my first PHP script that tests the user on cranial nerve association; specifically the name of the cranial nerve is displayed to the user and they are supposed to click on the correct number corresponding to the name of the cranial nerve.
I will post my code first and my question after:
<?php
$generated_nerve_number = mt_rand(1,12);
switch($generated_nerve_number) {
case '1':
echo "Olfactory";
break;
case '2':
echo "Optic";
break;
case '3':
echo "Oculomotor";
break;
case '4':
echo "Trochlear";
break;
case '5':
echo "Trigeminal";
break;
case '6':
echo "Abducens";
break;
case '7':
echo "Facial";
break;
case '8':
echo "Vestibulocochlear";
break;
case '9':
echo "Glossopharyngeal";
break;
case '10':
echo "Vagus";
break;
case '11':
echo "Accessory";
break;
case '12':
echo "Hypoglossal";
break;
}
?>
<html>
<head>
<title>Cranial Nerves Test</title>
</head>
<body>
<p>Select the cranial nerve number below associated with the name of the cranial nerve given above:</p>
<form action="cranial.php" method="POST">
<?php
echo "Cranial Number: ";
for($i = 1; $i <= 12; $i++) {
echo "<input type=\"submit\" name=\"nerve_$i\" class=\"nerve_number\" value=\"$i\">";
}
?>
<?php
$submit = (isset($_POST['nerve_' . $i])) ? $_POST['nerve_' . $i] : NULL;
if($submit) {
$selected_nerve_number = $_POST['nerve_' . $i];
if($generated_nerve_number == $selected_nerve_number) {
echo "That is correct!";
} else {
echo "Sorry that is incorrect.";
}
}
?>
</form>
</body></html>
A quick overview of my thought process: I am randomly generating a number between 1-12 and using that number in a switch statement to echo the name of the cranial nerve. I used a for loop to generate submit buttons with names that contain the number of the cranial nerve corresponding to its displayed value. Lastly, my plan on checking whether the answer is correct or not is to use an if statement comparing the randomly generated number to the number selected and, if this is true, output a message saying that they were correct.
This is where the problem comes in: when I click any of the buttons, whether its the correct answer or not, the page just refreshes without giving any feedback on whether the answer was right or wrong. Can someone please point out the flaw?
Additionally, if there is a more optimal way of doing something in this script please let me know.
Thanks in advance.
There is a slight problem with this approach. Your script is actually having two steps to consider: first, sending a page to the client with a randomly chosen cranial nerve, then comparing a choice with what was randomly specified.
If you wish your script to do this, you must add some logic so that it will know it have to react differently.
if (isset($_POST))
{/*compare the user choice with what you had sent.
you will have to do add an input to your form containing the random value AND the chosen value, or you eon't be able to compare them*/}
currently, you're trying to do all this at once, eg, you're comparing the return value of your user before you even receive it in your post! Understand that once sent to the client (browser) your page is not linked anymore to the php. So here:
if($generated_nerve_number == $selected_nerve_number) {
you're actually comparing the random number you just generated, with the answer you will have from your user in the FUTURE, since he has not even seen it on screen yet :)
Hello I have kind of a step-voting system, so when you click on continue, it goes to the next step.
I do not want to use sessions, therefore I am using POST to go to the next step and queries to call items.
if (isset($_POST['continue']))
{
if ($_POST['vote_page'] == 'site1')
{
$_POST['vote_page'] = 'site2';
}
if ($_POST['vote_page'] == 'site2')
{
$_POST['vote_page'] = 'site3';
}
}
if clicked continue and at site 1, then go to site 2, etc.
switch ($_POST['vote_page'])
{
case 'site1':
echo 'site1!';
break;
case 'site2':
echo 'site 2!!';
break;
case 'site3':
echo 'site 3!!!!!';
break;
}
But for some reason it loops through all of these posts, because the PHP is on the top of the script I think.
Problem:
From site1, it went to site3, ignoring site2...
How can I prevent this? or is there any other way to manage step-voting without sessions?
You can use cookies or hidden input tags to transmit data and you can use $_SERVER["PHP_SELF"] to know your current location, I see you're French so I'm going to say 'Bon chance!'
I have downloaded a backup copy of my cpanel webserver. I install WAMP on my local laptop.The database and php username and password have been set. Then I loaded the database into the local MySQL database. I verified the phpinfo.php is working. I setup dreamweaver manage site with the extracted website on my laptop. Now the back end admin pages on my laptop for my website are working. However the frontend is not completely loading and I can't figure out why.
Please go here to see what the page looks like since I can't post images on this post. dev.quicksalenetwork.com/images/frontend.png
Now if you go to the live development site you would see what is should look like.
Also here is the source code of the index.php page.
<?php
//redirect for install
if(!file_exists('Connections/myconn.php'))
header('Location:install.php');
//install file check
/*if(file_exists('install.php') || file_exists('install2.php') || file_exists('install3.php'))
print '<font color="red"><strong>Please delete install files { install.php , install1.php, install2.php }</strong></font><br>';
*/
//*******************************************************************************
//PHP Real Estate Classifieds
//All Rights reserved by Quick Sale Network inc.
//Do not modify, duplicate, resell with out direct permission from Quick Sale Network inc.
//Script is bound by terms listed in terms.html file
//*******************************************************************************
//fetch template header
include('header.php');
//define varable
define('go','');
//set index varable for switch statment
if (isset($_REQUEST['go']))
$go = $_REQUEST['go']; // requested page
else
$go = ''; // default
switch($go) {
case 'gmap':
include('gmap.php');
break;
case 'photos':
include('photos.php');
break;
case 'resource_center':
include('resource_center.php');
break;
case 'linkdir':
include('linkdir.php');
break;
case 'mailme':
include('formmail.php');
break;
case 'sell':
include('sell.php');
break;
case 'buy':
include('buy.php');
break;
case 'properties':
include('properties.php');
break;
case 'login' :
header("location: members/login.php");
break;
case 'register' :
include('register.php');
break;
case 'pricing' :
include('pricing.php');
break;
case 'calc' :
include('calc.php');
break;
case 'contact' :
include('contact.php');
break;
case 'faq' :
include('faq.php');
break;
case 'features' :
include('features.php');
break;
case 'member_login' :
include('member_login.php');
break;
case 'mailme' :
echo"<br><h4>";
include('formmail.php');
echo"</h4>";
break;
case 'detail' :
include('detail.php');
break;
case 'search' :
include('search.php');
break;
/*case 'home_gmap' :
include('home.php');*/
default:
include('home_gmap.php');
break;
}//end of switch statment
//fetch template footer
include('footer_city.php');
?>
Any ideas what I am doing wrong?
My guess:
your code uses php short open tags (<? instead of <?php) and you haven't enabled short tags in your wamp install..
There's a short_open_tag directive in your php.ini to enable it
I had the same thing happen to me. It happens when a file is corrupted. Just delete the file (Blanking it wont work) and copy all the code back into it.