WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Templates, Menus & Design => Topic started by: Lotus on December 15, 2008, 04:00:58 PM

Title: PHP - execute certain code depending on ?lang=
Post by: Lotus on December 15, 2008, 04:00:58 PM
Hello, need som PHP-help with my template.php im trying to do multinlingual. I have two sections that i want to be global. But I want them to show depending on ?lang= set.

If lang=se
show section 10
else if lang=en
show section 11
else
show section 12

I have this code but out of ideas  :-D, anyone up to it?

Code: [Select]
        <?php
        $url 
$_SERVER[&#39;REQUEST_URI&#39;];
          if (strrpos($url,"hem.php"))  {
        
?>

            <img  src="<?php echo TEMPLATE_DIR?>/images/imagebild2.gif"  />
        <?php    
        } elseif (strrpos($url,"home.php")) {
         ?>

        <img  src="<?php echo TEMPLATE_DIR?>/images/imagebild2.gif"  />
        <?php
          
} else {
        
?>

        <img  src="<?php echo TEMPLATE_DIR?>/images/imagebild.gif"  />
    <?php ?>
       
Code: [Select]
        <?php
        $section_id 
9;             
        $query_sections 
$database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE section_id = &#39;$section_id&#39; ");
        if($query_sections->numRows() > 0) {
            $section $query_sections->fetchRow();
            $section_id $section[&#39;section_id&#39;];
            $module $section[&#39;module&#39;];
            require(WB_PATH.&#39;/modules/&#39;.$module.&#39;/view.php&#39;);
        
        ?>

Title: Re: PHP - execute certain code depending on ?lang=
Post by: kweitzel on December 15, 2008, 04:07:00 PM
You can just query the language directly, you don't need to go via the "Request_URI" ...

Have a look here: http://help.WebsiteBaker.org/pages/en/advanced-docu/developer-guide/variables-and-constants.php

cheers

Klaus
Title: Re: PHP - execute certain code depending on ?lang=
Post by: Lotus on December 15, 2008, 04:32:05 PM
That is great news, just dont know how to acually write the code. Believe me i would have if i knew. :-D
Title: Re: PHP - execute certain code depending on ?lang=
Post by: vyni on December 15, 2008, 06:06:35 PM
Hi Lotus,

try this code

Code: [Select]
<?php
if (LANGUAGE==SE) {
$section_id 10//change this section_id for Your needings
$query_sec $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE section_id = &#39;$section_id&#39; ");
if(
$query_sec->numRows() > 0) {
$section $query_sec->fetchRow();
$section_id $section[&#39;section_id&#39;];
$module $section[&#39;module&#39;];
require(WB_PATH.&#39;/modules/&#39;.$module.&#39;/view.php&#39;); //statt view.php die alternative eintragen!
}
} else  if (
LANGUAGE==EN) {
$section_id 11//change this section_id for Your needings
$query_sec $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE section_id = &#39;$section_id&#39; ");
if(
$query_sec->numRows() > 0) {
$section $query_sec->fetchRow();
$section_id $section[&#39;section_id&#39;];
$module $section[&#39;module&#39;];
require(WB_PATH.&#39;/modules/&#39;.$module.&#39;/view.php&#39;); //statt view.php die alternative eintragen!
}
} else {
$section_id 12//change this section_id for Your needings
$query_sec $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE section_id = &#39;$section_id&#39; ");
if(
$query_sec->numRows() > 0) {
$section $query_sec->fetchRow();
$section_id $section[&#39;section_id&#39;];
$module $section[&#39;module&#39;];
require(WB_PATH.&#39;/modules/&#39;.$module.&#39;/view.php&#39;); //statt view.php die alternative eintragen!
}
}
<?

maybe it is does what Your are looking for.

regards from Himberg, near Vienna
Title: Re: PHP - execute certain code depending on ?lang=
Post by: Lotus on December 16, 2008, 08:39:18 AM
maybe it is does what Your are looking for.

regards from Himberg, near Vienna


This code is so great I can´t believe my eyes  :-D, thanx Himberg! This opens up lots of possibilities when making templates, until now i solved all this issues with another template, but always thought it was "not quite right" doing so.

Regards Sweden!
Title: Re: PHP - execute certain code depending on ?lang=
Post by: pcwacht on December 16, 2008, 09:53:05 AM
Cleaned it up a bit for clarity and readablitity

Code: [Select]
<?php
if (LANGUAGE==SE) { 
    
$section_id 10;   //change this section_id for Your needings
} elseif (LANGUAGE==EN) {
    
$section_id 11//change this section_id for Your needings
} else {
    
$section_id 12//change this section_id for Your needings
}
if (isset(
$section_id)) { // Maker sure section isset
    
$query_sec $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE section_id = &#39;$section_id&#39; ");
    if(
$query_sec->numRows() > 0) {
        
$section $query_sec->fetchRow();
        
$section_id $section[&#39;section_id&#39;];
        
$module $section[&#39;module&#39;];
        
require(WB_PATH.&#39;/modules/&#39;.$module.&#39;/view.php&#39;); //statt view.php die alternative eintragen!
    
}
}
<?

Have fun,
John