Solved

if/else

Posted on 2004-09-09
14
174 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:nenufarloganx
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
 
LVL 3

Expert Comment

by:nenufarloganx
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:
nenufarloganx 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:nenufarloganx
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:nenufarloganx
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:nenufarloganx
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:nenufarloganx
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now