Jump to content

[SOLVED] Language Session Problem


SkyRanger

Recommended Posts

I had this in another thread but I had a couple of problems in there and decided to split them to clean things up.

 

If admin has a problem with me doing this, sorry.  I will go back to old thread.

 

Currently I am just trying to get the default language to load before I figure out how to make it change languages.

 

Currently my code is as follows:

 

get_language.php file

<?

// mysql
include "inc/dbinfo.inc.php";

$connection=mysql_connect ("$dblocation", "$dbusername", "$dbpassword") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$dbname");

$result = mysql_query( "SELECT * FROM languages WHERE lid = '" . $_SESSION['language']['lid'] . "'" ) or die(mysql_error());


if (empty($_SESSION['language']['lid'])) {
$_SESSION['language']['lid'] = '1';
}

?>

 

main.php file

<?
include "./inc/get_language.php";

include "./lang".$object->lpath."main.lang.php";
include "install/header.inc";

echo NAME;

include "install/footer.inc";
}
?>

 

This is my database info:

 

table = languages

 

lid    lpath

1    /english/

2    /french/

 

The error I am getting from main.php is:

Warning: include(./langmain.lang.php) [function.include]: failed to open stream: No such file or directory in /home/esshost/public_html/index.php on line 6

Warning: include() [function.include]: Failed opening './langmain.lang.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/esshost/public_html/main.php on line 6

 

I am still pretty new to PHP, I mostly still work with the basics.  I need to figure out why .$object->lpath. is not putting the /english/ as the default lang

Link to comment
Share on other sites

I think it could be your SQL query.

 

Here is your query:

SELECT * FROM languages WHERE lid = '" . $_SESSION['language']['lid'] . "'"

 

If you make echo "your query" you will get:

SELECT * FROM languages WHERE lid = '3'

 

You can't use character ' or " in your WHERE condition because it's numeric value (lid is number). Remove it and try again.

Link to comment
Share on other sites

Here is more....

 

Have you tryed using simple

echo

command to see what results you get from database?

That could be useful to show if you get well data from db. I don't know what your $object class return.

 

Try this:

$cnn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$cnn) {
   echo "Unable to connect to DB: " . mysql_error();
   exit;
}
 
if (!mysql_select_db("mydbname")) {
   echo "Unable to select mydbname: " . mysql_error();
   exit;
}

//your query (if it's okay; check it with echo command - echo $sql to see if it's good)
$sql = "SELECT * FROM languages WHERE lid = " . $_SESSION['language']['lid'];

$result = mysql_query($sql);

if (!$result) {
   echo "Could not successfully run query ($sql) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result) == 0) {
   echo "No rows found, nothing to print so am exiting";
   exit;
}

while ($row = mysql_fetch_assoc($result)) {
   echo "${row['lid']} ${row['lpath']}<br>";
}

mysql_free_result($result);
?> 

 

Now you can see if your query return any result.

 

If this solved your problem, please use Solved button - it's bottom left side.

 

Thanks,

 

Anthylon

Link to comment
Share on other sites

Ok, I unresolved this topic due to I have a question.

 

The above worked awsome, but I am a really BIG nOOb to sessions and php

 

I have my program working to read the language file, but I have no clue on how to change it, and was wondering if anybody had any idea.

 

Also I am trying to set it up so when the user installs my script he can choose a language other than the default of "1" which is english  ie: they choose "2" french or "3" spanish that will change the default:

 

if (empty($_SESSION['language']['lid'])) {
$_SESSION['language']['lid'] = '1';
}

 

So instead of ='1' it would read the variables table and choose the default language in the $lang row ie: 2,3,etc...  but also have it somehow setup for the visitor to choose his own language ie: 1,3,etc

Link to comment
Share on other sites

I take it, I never explained this very well.  If not, I will try and explaining better. 

 

I need the program to register the language in 2 ways.  One through the database and One by visitor choice.

 

Right now the default language is English.  I would like it so the installer of the program can choose the default language on install to anything he chooses and have it stored in the database.  Also when a visitor arrives at the site, I would like them to be able to choose the language they want through a session.

 

Can anybody help me on this?

Link to comment
Share on other sites

What I can't figure out is how to:

a] have the get_language.php file to read the variables(my table) $lang cell for the default lang. ie: $lang = '2';

b] how to get the visitor to change the language on there own.

 

languages are as follow:

lid = 1,2,3,etc...

lpath = english,french,spanish,etc....

 

Any input would be greatly appreciated.

Link to comment
Share on other sites

Well I think you need to use PHP.chm file more. It's usefull.

 

I'm using cookies for storing what language user choosed and some other commong informations on his PC. You can't use user's IP because it could be (mostly yes) dynamic.

 

In that case you don't need languages in your database. I would suggest to you rather to use some config.php file. What is some user come to your website and your database is offline? How can you say in his language that you have currently some technical... blah blah...

 

So, maybe you can define some array with languages: $arr_languages = array('eng', 'bos',...) etch. And when user come online check is there cookie on his PC. If not check what language is default on his PC... and than...

 

I hope this could be usefull for you.

 

Good luck,

 

Anthylon

Link to comment
Share on other sites

Thanks anthylon.  I got the first half of my problem fixed about the admin setting language default to his own.  Just have to figure out how to do the session part of it to get the visitor to choose his own language by clicking on either a link or flag, haven't decided which yet.

 

Can anybody point me in the right direction on how I may accomplish this?

Link to comment
Share on other sites

Ok, this is what I have so far:

 

<?
// mysql
include "inc/dbinfo.inc.php";

$connection=mysql_connect ("$dblocation", "$dbusername", "$dbpassword") or die ('I cannot connect to the database because: ' . mysql_error());
        mysql_select_db ("$dbname");

$sql1 = mysql_query("SELECT lang from variables") or die(mysql_error());
while($row1 = mysql_fetch_array($sql1))
  {
  $newlang = $row1['lang'];
  }
  
  

if (empty($_SESSION['language']['lid'])) {
$_SESSION['language']['lid'] = $newlang;
}

// mysql
include "inc/dbinfo.inc.php";


$cnn = mysql_connect ("$dblocation", "$dbusername", "$dbpassword") or die ('I cannot connect to the database because: ' . mysql_error());

if (!$cnn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("$dbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

//your query (if it's okay; check it with echo command - echo $sql to see if it's good)

$sql = "SELECT * FROM languages WHERE lid = " . $_SESSION['language']['lid'];

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while ($row = mysql_fetch_assoc($result)) {

$path = $row['lpath'];
}


mysql_free_result($result);
?> 

 

This is my Index.php page:

include "./inc/get_language.php";

include "./lang/".$path."/install.lang.php";

more php code here...

 

The problem I am having is how do I get the visitor to choose there own language using sessions and/or cookies.

 

I am new to this, that is why I am still lost.  Any help would be greatly appreciated.

Link to comment
Share on other sites

That's easy. You can use form or link. If you want use form make combo so user can choose lang. After it submit form you can get what id he selected with $_POST['combo_name'] and than record that language_id on his PC (using cookies).

 

If you use link make like this:

<a href='index.php?lang_id=1'>English</a>
<a href='index.php?lang_id=2'>Bosnian</a>
...

 

So every time when your index.php realoaded you can on the very top check if $_GET['lang_id'] has been set (has value). If $_GET['lang_id'] is not set that means user hasn't select language still so you have to provide him page with selecting language...

 

I would suggest use of php.chm user manual.

 

Best regards,

 

Anthylon

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.