Solved

if/else

Posted on 2004-09-09
14
176 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:thtrance
  • 7
  • 5
  • 2
14 Comments
 
LVL 9

Expert Comment

by:AlanJDM
ID: 12015566
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
 
LVL 3

Expert Comment

by:Logan
ID: 12016193
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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 12016229
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 3

Expert Comment

by:Logan
ID: 12016265
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
 
LVL 3

Accepted Solution

by:
Logan earned 500 total points
ID: 12016519
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
 

Author Comment

by:thtrance
ID: 12016533
<?
$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
 
LVL 3

Expert Comment

by:Logan
ID: 12016606
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
 
LVL 3

Expert Comment

by:Logan
ID: 12016651
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
 

Author Comment

by:thtrance
ID: 12016742
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
 
LVL 3

Expert Comment

by:Logan
ID: 12016826
<?
$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
 

Author Comment

by:thtrance
ID: 12016881
Thank you.
I will try that one out.

Thanx for having patient with me duuude =)
0
 

Author Comment

by:thtrance
ID: 12016911
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
 

Author Comment

by:thtrance
ID: 12017013
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
 
LVL 3

Expert Comment

by:Logan
ID: 12017098
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question