Jump to content

Hmmm. I'm stumped.


N1CK3RS0N

Recommended Posts

Hey guys.

 

I'm working on a script for my CMS and I'm having a bit of difficulties. I'm doing the step 1 of the installation which is the "languages". I currently have it working so it auto-detects the browsers language settings and selects the appropriate language file if a browser language setting was found, otherwise it displays the default language. They can manually change the language as well by selecting an option from a drop down menu.

 

My problem is this. The way the script is constructed now, it just echo's out that drop down menu right in the middle of the content area. I wanted to be able to use some sort of variable so I could insert it into a template file exactly where I want it. I cannot figure out a way to do this though.

 

<?php

include('templates/default/header.tpl');

function languages()
{
// Detect the browsers default language settings.

$default_lang_name = "English";
$default_lang_file = "languages/English.php";

$languages = array(  
	'zh' => 'Mandarin', 
	'hi' => 'Hindustani', 
	'es' => 'Spanish',
	'en' => 'English', 
	'ar' => 'Arabic', 
	'pt' => 'Portuguese', 
	'bn' => 'Bengali', 
	'ru' => 'Russian', 
	'ja' => 'Japanese',
	'de' => 'German', 
	'pa' => 'Punjabi', 
	'te' => 'Telugu', 
	'mr' => 'Marathi', 
	'vi' => 'Vietnamese', 
	'ko' => 'Korean', 
	'ta' => 'Tamil', 
	'fr' => 'French', 
	'it' => 'Italian'
); 

if ( isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) )
{
	$language = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
	$language = substr( $language, 0, 2 );

	if ( array_key_exists($language, $languages) ) 
	{
		$value = $languages["$language"];
		$lang_name = $value;
		$lang_detected = $value;
		include("languages/$value.php");
		$results = 'supported';
	} else {
		$lang_name = $default_lang_name;
		$lang_detected = $default_lang_name;
		include("$default_lang_file");
		$results = 'not_supported';
	}
} else {
	$lang_name = $default_lang_name;
	$lang_detected = $default_lang_name;
	include("$default_lang_file");
	$results = 'not_detected';
}

if ( isset($_GET['language']) )
{
	$set_lang = $_GET['language'];
	$lang_name = $set_lang;
	include("languages/$set_lang.php");
}

// Create the drop down menu for language selection.

asort ($languages);

echo "<form action=\"test2.php\">\n<select name=\"language\" onchange='this.form.submit()'>\n";

foreach ( $languages as $key => $value ) 
{
	if ( $lang_name == $value ) 
	{ 
		$selected = 'selected=""';
	} else {
		$selected = '';
	}

	echo "<option value=\"$value\" $selected>$value</option>\n";
}

echo "</select>\n</form>\n";
}

switch ($_GET['step'])
{
default:
echo languages();
include('templates/default/step1.tpl');
break;
case 1:
echo languages();
include('templates/default/step1.tpl');
break;
case 2:
include('templates/default/step2.tpl');
break;
case 3:
include('templates/default/step3.tpl');
break;
case 4:
include('templates/default/step4.tpl');
break;
case 5:
include('templates/default/step5.tpl');
break;
case 6:
include('templates/default/step6.tpl');
break;
case 7:
include('templates/default/step7.tpl');
break;
}

include('templates/default/footer.tpl');

?>

 

The script is basically 2 parts:

 

1. Detect the browsers default language settings.

2. Generate a drop down menu to select languages.

 

If I call part 2 into a function, it leaves out part 1, and doesn't function properly.

 

I'm still new to PHP, so don't be too harsh if my script is bad ^^.

 

Any ideas or suggestions?

Link to comment
https://forums.phpfreaks.com/topic/141820-hmmm-im-stumped/
Share on other sites

Change the echo(s) in your language function so the function returns your select box as a variable...

 

 

change this...

 

   echo "<form action=\"test2.php\">\n<select name=\"language\" onchange='this.form.submit()'>\n";
      
   foreach ( $languages as $key => $value ) 
   {
      if ( $lang_name == $value ) 
      { 
         $selected = 'selected=""';
      } else {
         $selected = '';
      }
         
      echo "<option value=\"$value\" $selected>$value</option>\n";
   }

   echo "</select>\n</form>\n";

 

 

to this...

 

   $data = "<form action=\"test2.php\">\n<select name=\"language\" onchange='this.form.submit()'>\n";
      
   foreach ( $languages as $key => $value ) 
   {
      if ( $lang_name == $value ) 
      { 
         $selected = 'selected=""';
      } else {
         $selected = '';
      }
         
      $data .= "<option value=\"$value\" $selected>$value</option>\n";
   }

   return $data . "</select>\n</form>\n";

 

 

Then in your switch() statement, change all those

 

   echo languages();

 

to...

 

$selectbox = languages();

 

Then echo that variable $selectbox wherever you want the select box to be displayed in your template or output page!

Link to comment
https://forums.phpfreaks.com/topic/141820-hmmm-im-stumped/#findComment-742526
Share on other sites

Change the echo(s) in your language function so the function returns your select box as a variable...

 

 

change this...

 

   echo "<form action=\"test2.php\">\n<select name=\"language\" onchange='this.form.submit()'>\n";
      
   foreach ( $languages as $key => $value ) 
   {
      if ( $lang_name == $value ) 
      { 
         $selected = 'selected=""';
      } else {
         $selected = '';
      }
         
      echo "<option value=\"$value\" $selected>$value</option>\n";
   }

   echo "</select>\n</form>\n";

 

 

to this...

 

   $data = "<form action=\"test2.php\">\n<select name=\"language\" onchange='this.form.submit()'>\n";
      
   foreach ( $languages as $key => $value ) 
   {
      if ( $lang_name == $value ) 
      { 
         $selected = 'selected=""';
      } else {
         $selected = '';
      }
         
      $data .= "<option value=\"$value\" $selected>$value</option>\n";
   }

   return $data . "</select>\n</form>\n";

 

 

Then in your switch() statement, change all those

 

   echo languages();

 

to...

 

$selectbox = languages();

 

Then echo that variable $selectbox wherever you want the select box to be displayed in your template or output page!

 

Will you marry me? :)

 

Thanks man

Link to comment
https://forums.phpfreaks.com/topic/141820-hmmm-im-stumped/#findComment-742560
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.