if/else

Hello

This is my code :

<?php include('.includeEg/headermain.php'); ?>


<?php

 
$a_PagesEn = array('1' => '.includeEg/main.php', '2' => '.includeEg/contact.php', '3' => '.includeEg/skills.php', '4' => '.includeEg/about.php', '5' => '.includeEg/stats.php', 'mail' => '.includeEg/sendeail.php');
$a_PagesSw = array('1' => '.includeSw/main.php', '2' => '.includeSw/contact.php', '3' => '.includeSw/skills.php', '4' => '.includeSw/about.php', '5' => '.includeSw/stats.php', 'mail' => '.includeSw/sendeail.php');

$a_Pages = array('en' => $a_PagesEn, 'sv' => $a_PagesSw);

include($a_Pages[$_GET['lang']][$_GET['id']]);

?>


<?php include('.includeEg/footer.php'); ?>

This gives me an ouput /index.php?lang=en/sv&id=1/2/3  etc

If I have a link like: lang=somethingelse I will have the page shown but only with headeermain and footer.

How can I make with for example an if/else statement that if I use something else then se and en, or some other id then 1/2/3/4/5/mail I will be redirected to another  error.php page.


This is what I was trying but couldnt get it right,

<?php include('.includeEg/headermain.php'); ?>


<?php
$lang = $_GET["lang"];
 if ($lang != "sv" && $lang != "en") $lang = "default";{
if ($lang == "default") include("error.php");
}
else {
$a_PagesEn = array('1' => '.includeEg/main.php', '2' => '.includeEg/contact.php', '3' => '.includeEg/skills.php', '4' => '.includeEg/about.php', '5' => '.includeEg/stats.php', 'mail' => '.includeEg/sendeail.php');
$a_PagesSw = array('1' => '.includeSw/main.php', '2' => '.includeSw/contact.php', '3' => '.includeSw/skills.php', '4' => '.includeSw/about.php', '5' => '.includeSw/stats.php', 'mail' => '.includeSw/sendeail.php');

$a_Pages = array('en' => $a_PagesEn, 'sv' => $a_PagesSw);

include($a_Pages[$_GET['lang']][$_GET['id']]);
}
?>


<?php include('.includeEg/footer.php'); ?>

Maybe the brackets or something else is wrong ...
Id´s that are in arrays ...

Shall I make something like:
$id = $_GET["id"];
if ($id != "1" && $id != "2" and so on??? I dont think its right) $id = "default";{


Ids , I don t really understand ... but the language ... ?

Another Q:

If I have for example another langueage, shall I make :
if ($lang != "sv" && $lang != "en" && $lang="pl") $lang = "default";

best regards
/Artur
thtranceAsked:
Who is Participating?
 
LoganConnect With a Mentor Web DeveloperCommented:
Or better than:

<?
$lang = $_GET["lang"];
$aPages = array("main", "contact", "skills", "about", "stats", "sendeail"); // sendeail or sendemail ?
$aDirs = array("sv"=>"Sv", "en"=>"Eg");
if(array_key_exists($lang, $aDirs)){
      for($i = 0; $i < count($aPages); $i++){
            require_once($aDirs[$lang]."/".$aPages[$i].".php");
      }
}
else{
      $lang = "default";
      require_once("error.php");
}
?>
0
 
AlanJDMCommented:
if($lang != "sv" && $lang != "en")
{
  $lang = "default";
  include("error.php");
}
else
{
  $a_PagesEn = array('1' => '.includeEg/main.php', '2' => '.includeEg/contact.php', '3' => '.includeEg/skills.php', '4' => '.includeEg/about.php', '5' => '.includeEg/stats.php', 'mail' => '.includeEg/sendeail.php');
  $a_PagesSw = array('1' => '.includeSw/main.php', '2' => '.includeSw/contact.php', '3' => '.includeSw/skills.php', '4' => '.includeSw/about.php', '5' => '.includeSw/stats.php', 'mail' => '.includeSw/sendeail.php');
  $a_Pages = array('en' => $a_PagesEn, 'sv' => $a_PagesSw);
  include($a_Pages[$_GET['lang']][$_GET['id']]);
}


Alan
0
 
LoganWeb DeveloperCommented:
Hi thtrance,

I think u must change this line:

if($lang != "sv" && $lang != "en"){

By this one:

if($lang != "sv" || $lang != "en"){


If u want a more flexible script (and so add many options/languages) try this:

<?
$lang = $_GET["lang"];

$aPages = array("main", "contact", "skills", "about", "stats", "sendeail"); // sendeail or sendemail ?
$aDirs = array("sv"=>"Sv", "en"=>Eg);

if( $lang != "sv" || $lang != "en"){
   $lang = "default";
   include("error.php");
}
else{
   for($i = 0; $i < count($aPages); $i++){ require_once($aDirs[lang]."/".$aPages[$i].".php"); }
}
?>

Hope that helps :)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
AlanJDMCommented:
if( $lang != "sv" || $lang != "en")

This is wrong... it can only be evaluated to true no matter the value of $lang, it will never be false as you are using an OR and $lang can only have one value. $lang will always be either != "sv" or != "en"


Alan
0
 
LoganWeb DeveloperCommented:
In previous comment i've make a mistake,

This line is right (as Alan said)
 if($lang != "sv" && $lang != "en")

So the script i proposed might be:

<?
$lang = $_GET["lang"];

$aPages = array("main", "contact", "skills", "about", "stats", "sendeail"); // sendeail or sendemail ?
$aDirs = array("sv"=>"Sv", "en"=>Eg);

if( $lang != "sv" && $lang != "en"){
   $lang = "default";
   include("error.php");
}
else{
   for($i = 0; $i < count($aPages); $i++){ require_once($aDirs[lang]."/".$aPages[$i].".php"); }
}
?>
0
 
thtranceAuthor Commented:
<?
$lang = $_GET["lang"];

$aPages = array("main", "contact", "skills", "about", "stats", "sendeail");
$aDirs = array("sv"=>"path/to/folder", "en"=>"path/to/folder"); //path to folder?

if( $lang != "sv" || $lang != "en"){
   $lang = "default";
   include("error.php");
}
else{
   for($i = 0; $i < count($aPages); $i++){ require_once($aDirs[lang]."/".$aPages[$i].".php"); }
}
?>

That means that I can have multiply languages in diff. folders as in $aDirs but files as in $aPages must stay with the same name.

Is this a solution for if a user hit lang=se&id=doeasnt exist:

for($i = 0; $i < count($aPages); $i++){ require_once($aDirs[lang]."/".$aPages[$i].".php"); }// <--- should the bracket be there?
0
 
LoganWeb DeveloperCommented:
for($i = 0; $i < count($aPages); $i++){ require_once($aDirs[lang]."/".$aPages[$i].".php"); }

is tha same that

for($i = 0; $i < count($aPages); $i++){
   require_once($aDirs[lang]."/".$aPages[$i].".php");
}
0
 
LoganWeb DeveloperCommented:
I think the code i've posted before ( Date: 09/09/2004 03:57PM ) should work well

Try it and ask what you doubt

(and excuse my lack of english)

Good Luck :)
0
 
thtranceAuthor Commented:
Thanx for support.

I just want to understand the code here (Date: 09/09/2004 06:57AM PDT)


<?
$lang = $_GET["lang"];
$aPages = array("main", "contact", "skills", "about", "stats", "sendeail");
// these are the files. For diff. languages I have them in separate folders and they have to have the same name, right? Thats what I understand.
$aDirs = array("sv"=>"Sv", "en"=>"Eg");
// HEre you have array => "Sv". Is Sv the folder, e.g. .path/to/folder?
if(array_key_exists($lang, $aDirs)){
     for($i = 0; $i < count($aPages); $i++){
          require_once($aDirs[$lang]."/".$aPages[$i].".php");
     }
}
else{
     $lang = "default";
     require_once("error.php");
}
?>
0
 
LoganWeb DeveloperCommented:
<?
$lang = $_GET["lang"];
$aPages = array("main", "contact", "skills", "about", "stats", "sendeail");
// these are the files. For diff. languages I have them in separate folders and they have to have the same name, right? Thats what I understand.
// You're right


$aDirs = array("sv"=>"Sv", "en"=>"Eg");
// HEre you have array => "Sv". Is Sv the folder, e.g. .path/to/folder?
// You're right again. the key (sv) is the lang and the value (Sv) the path/to/folderLang

if(array_key_exists($lang, $aDirs)){  // Here we check if that $lang exists in our array ($aDirs)
     for($i = 0; $i < count($aPages); $i++){
          require_once($aDirs[$lang]."/".$aPages[$i].".php");  //And here we require each file ($aPages) inside path/to/folderLang
     }
}
else{  // If $lang doesn't exists, we show an error page
     $lang = "default";
     require_once("error.php");
}
?>

:)
0
 
thtranceAuthor Commented:
Thank you.
I will try that one out.

Thanx for having patient with me duuude =)
0
 
thtranceAuthor Commented:
By the way ...
Is there a way to check if the ID is okej.
FO instance the user can make lang=se&id12345 whicj does not exist.
Will this code prevent it and redirect to error.php?
0
 
thtranceAuthor Commented:
What it does you can see here:

http://www.technoheart.com/index75.php?lang=en&id=1

It includes all english pages.

This is my code.

<?php include('.includeEg/headermain.php'); ?>
<?
$lang = $_GET["lang"];
$aPages = array("main", "contact", "skills", "about", "stats", "sendeail");

$aDirs = array("sv"=>".includeSw", "en"=>".includeEg");

if(array_key_exists($lang, $aDirs)){
     for($i = 0; $i < count($aPages); $i++){
          require_once($aDirs[$lang]."/".$aPages[$i].".php");
     }
}
else{
     $lang = "default";
     require_once("error.php");
}
?>

<?php include('.includeEg/footer.php'); ?>


http://www.technoheart.com/index75.php?lang=en&id=76  "id = 76" does not exist and makes the same output.
0
 
LoganWeb DeveloperCommented:
Yup

Since we aren't using ID now you only need  lang=XX

If this array is setup
$aDirs = array("sv"=>"Sv", "en"=>"Eg");

this line will check if lang exists
if(array_key_exists($lang, $aDirs)){

<?
// if a user pass to the script "land=ck" ->

$aDirs = array("sv"=>"Sv", "en"=>"Eg"); //ck isn't in array

//so this is false

if(array_key_exists($lang, $aDirs)){
?>

You don't need var id anymore :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.