FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php menus





ptolomeo
Hello, I am learning php and what most liked at first was the possibility of making only one menu for all the
pages of my site and put it with the include() php function.

I was traying now to make a multilingual site, but i dont
feel yet very comfortable with the usage of variables. Also I have the problem on how to make the different menus
to realize which lenguage is being used.

Does someone knows a good php tutorial on php. I have been surfing the internet but didnt find anything suitable (I found either too simple or too complex).

Thanks a lot for any help on that
Diablosblizz
Quote:
Also I have the problem on how to make the different menus
to realize which lenguage is being used.


Possibly you should start learning sessions or cookies to store the information. After the information is stored then you receive it and set the language for the user like that.

Or you could use a database with the users IP and then retrieve it. The only problem with this is not everybody has a static IP.

EDIT: You could also have a member system, and have it stored there. I would either do this, or the sessions and cookies.

Good luck with your site.
woodenbrick
Have a play around with this, see if you can get it to work how you want:
Code:

<?php
session_start();
if(!isset($_SESSION['lang'])){
   $_SESSION['lang']="en"; //set default language
}
$languages = array("en", "de", "fr", "bg");
foreach($languages as $lang){
   if($lang != $_SESSION['lang']){
      //show languages, except the one currently being used
      echo "<a href='changelang.php?lang=$lang'>$lang</a><br />";
   }
}
//the menu
//i named the menus like this -> menu-en.php for english menu-fr for french etc.
include("menu-".$_SESSION['lang'].".php");

For the actual page content it would be best to use a database, I named each item after the page it is on (PHP_SELF) and pull out the column of the select language and print.
Code:

mysql_connect("localhost", "woodenbrick", "*****");
mysql_select_db("woodenbrick_lang");
$result = mysql_fetch_row(mysql_query("SELECT ".$_SESSION['lang']." FROM data
WHERE item='".basename($_SERVER['PHP_SELF'])."'"))or die(mysql_error());
echo $result[0];
?>


Also the page that changes the language, changelang.php
it doesn't do much just sets the sesion variable and redirects back to where it came.
Code:

<?php
session_start();
header("Location:".$_SERVER['HTTP_REFERER']);
$_SESSION['lang'] = $_GET['lang'];
exit;
?>

If you need me to explain it more fully i'm happy to help.
Diablosblizz
Woodenbrick;

Code:
<?php
session_start();
if(!isset($_SESSION['lang'])){
   $_SESSION['lang']="en"; //set default language
}
$languages = array("en", "de", "fr", "bg");
foreach($languages as $lang){
   if($lang != $_SESSION['lang']){
      //show languages, except the one currently being used
      echo "<a href='changelang.php?lang=$lang'>$lang</a><br />";
   }
}
//the menu
//i named the menus like this -> menu-en.php for english menu-fr for french etc.
include("menu-".$_SESSION['lang'].".php");


I have a question about this, where is the $lang defined in the code? I believe that this will just display nothing because the $lang variable has yet to be defined.

Unless the foreach($languages as $lang) is defining the variable, I am not sure because I have never used the foreach command.
woodenbrick
Diablosblizz wrote:

Unless the foreach($languages as $lang) is defining the variable, I am not sure because I have never used the foreach command.

Yes, the for loop defines $lang as the array item in $languages currently being processed.
ptolomeo
woodenbrick, thank you very much for your help, it was very useful and clear for me. I will implement it in the next days. I am completely new to databases and not so sure of using it. Cheers
Diablosblizz
woodenbrick wrote:
Yes, the for loop defines $lang as the array item in $languages currently being processed.


Okay, thank you for clearing that up for me. Also, glad you got things sorted out for yourself ptolomeo. Good luck with your site.
woodenbrick
So ptolomeo what did you plan to use instead a database?
If you like I can help you set it up, but if you use phpMyAdmin you can set it up quite easily without having to write any SQL.
minty
LOL! I just thought of the best way ever to translate your site. Use the code that woodenbrick has compiled and configured, but change the URL it points to. So instead of this:
Code:
echo "<a href='changelang.php?lang=$lang'>$lang</a><br />";


You will impliment this:
Code:
echo "<a href=http://www.google.com/translate?langpair=$_SESSION['lang']|$lang&u=www.yoursitehere.com'>$lang</a><br />";


EDIT: Forgot to explain how this works....So the two vars that you need are the current language ($_SESSION['lang']) and the language to be translated to ($lang).
So since $_SESSION['lang'] is automatically defined as 'en', this is obliviously going to be the language translated from. The second var is the language the site needs to be translated to, which is why it is $lang since this is the language the user wants it to be translated to.

**Just ensure that your two character reference to a particular language meets googles languages.

This will use google's service to translate your page from english to whatever language that you want, as long as google supports it.

One issue that I can think of is that the users will be redirected from your site to Google's site. And it may mess up your layout a bit...

However, this will get you from using a DB since the page is translated on the fly by Google.

--minty
Related topics
http://tuvanonline.com/library/index.php
PHP-Nuke platium?
PHP Write to line
Anyone good with PHP-NUKE?
PHP safety?
req: PHP to list files in directory, and link to them
php admin and mysql admin console
Mysql And PHP HELP PLZ
Great php editor
Script php about gallery
how to make hover drop menus without flash?
Php script.
php page flickering, maybe caused by flash item
scripting languages
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.