LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2011, 09:27 AM   #1
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Rep: Reputation: 10
Loading multiple page versions with PHP and MySQL


Hello,
I am trying to resolve maybe little bit nonstandard problem. I have a page which is going to be internationalized, and available in more languages. It contains PHP scripts to load, let's say, current user's data from database and the internationalized content itself, like "Welcome user" message.

The problem for me is the fact, that internationalized content is not continual, and it's all over the page mixed with php scripts.

I don't want to use eval().
I've got 2 suggestions, they are, however, not good enough.

1. One file per language version, with scripts included - there will be many languages, so there would have to be many files with redundant data. Also if I wanted to change structure of script, I would have to change it in all pages.

2. Load international data from db, while scripts are on the page - not sure about good database structure I mean, how would I get the right content from database? (content would be split into rows, columns, or something?)


Thanks for help.

Last edited by SkyerSK; 06-18-2011 at 09:29 AM.
 
Old 06-18-2011, 11:10 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
One way of sorting it (I'm not sure if this is exactly what you want, and please tell me if it's not) is to use the define() function. It's kinda like your first solution, except there isn't really any duplication. So...:

index.php
Code:
<?php
require('lang/english.php');
//require('lang/german.php');
?>

<h1><?php echo TEXT_WELCOME_MESSAGE ?></h1>
english.php
Code:
define('TEXT_WELCOME_MESSAGE','Welcome!');
german.php
Code:
define('TEXT_WELCOME_MESSAGE','Wilkommen!');
So you detect which language should be used, and require() or include() it into the page, and that file then defines the text the language in question.

Hope this helps,
 
Old 06-18-2011, 01:17 PM   #3
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Thanks for your reply,
you are close to my exact point of interest, I'll try to explain on example:
Code:
/*welcome.php, literals in brackets are meant to be replaced*/
Welcome user [USER]
You have [MESSAGES] new messages.
Total number of your posts is [POSTS]
?>
Therefore, I can either make internationalized files with scripts, each for one language, or one file with scripts, which will load internationalized content. I would like to keep only one file for each page, so I expect I am going to load international content from database. The problem is, that it's mixed with scripts, so I can't simply load whole content from database. (Scripts would not be executed). That implicates the fact, that I have to split international content into parts, and load it separately, or parse it somehow.

(There's much better way to accomplish this probably, that's why I'm asking).

Thanks.

Last edited by SkyerSK; 06-18-2011 at 01:23 PM.
 
Old 06-19-2011, 06:17 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I'm not sure if I understand your issue correctly...

Quote:
Therefore, I can either make internationalized files with scripts, each for one language, or one file with scripts, which will load internationalized content.
I was suggesting the second of these - using the first one, if you change a bit of the coding logic or the webtext you want to include, you have to change loads of files; using the second one, you only need to alter loads of files if you change your webtext.

Quote:
I would like to keep only one file for each page, so I expect I am going to load international content from database. The problem is, that it's mixed with scripts, so I can't simply load whole content from database. (Scripts would not be executed). That implicates the fact, that I have to split international content into parts, and load it separately, or parse it somehow.
Again, this is essentially what I suggested - you would have one file per page and one file per language. It would just waste both your time and server time setting up a database and a set of functions to connect to it in order to retrieve international text. It also gets round the fact that you need to mix in content with scripts - you've already got the webtext split into functional components. Going back to the example you posted:

welcome.php
Code:
<?php
$language = database_function_to_get_user_language_preference($user_id);
$username = database_get_user_name($user_id);
$newmessages = database_count_new_messages($user_id);
$postcount = database_count_new_posts($user_id);
require("lang/" . $language . ".php");
/*welcome.php, literals in brackets are meant to be replaced*/
echo WELCOME_USER_MSG;
echo MESSAGE_COUNT_MSG;
echo POST_COUNT_MSG;
?>
lang/english.php
Code:
<?php
define('WELCOME_USER_MSG','Welcome user ' . $username . '!');
define('MESSAGE_COUNT_MSG','You have ' . $newmessages . ' new messages.');
define('POST_COUNT_MSG','Total number of your posts is ' . $postcount . '.');
?>
lang/spanish.php
Code:
<?php
define('WELCOME_USER_MSG','ˇBienvenido, ' . $username . '!');
define('MESSAGE_COUNT_MSG','Tienes ' . $newmessages . ' mensajes nuevos.');
define('POST_COUNT_MSG','Has hecho ' . $postcount . 'correos.');
?>
Note that I've loosely translated that last one, so you can see that you don't need to preserve the partitioning of the sentence around the variables. Also notice I included the language file after initialising the variables - it doesn't work the other way around.

If I have misunderstood something, then do let me know

Hope this helps,
 
1 members found this post helpful.
Old 06-19-2011, 06:57 AM   #5
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Thanks for your reply,
I'll try to use the solution you provided. The only "dirty" thing about it is the fact, that each page will have unique structure and content, so I would have to use separate files for every page (or one file with many constants). (For example, settings and messages).

Your solution is worth of trying however, thanks for sharing.

I could post back if I find better solution.

By the way, it's good to see that somewhere out there are people of similar age, who like Linux too.

(edit: and sorry for my English, it's not my national language)

Last edited by SkyerSK; 06-19-2011 at 06:58 AM.
 
Old 06-19-2011, 07:01 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Ahh... Yes, I see your problem. Again, in big websites you will again use the require() format for including template files - so you'd have an 'index.php' which creates the page header and sidebar, and probably the footer as well, and then require()s your template/messages.php file, which sets up the page-dependant content. I guess it all depends on how big you think your website will grow, and how much work you are willing to put in to it. Best of luck, and it's possible one of the more experienced PHPers on the forum will have a better idea

And yes, indeed always nice to see someone young on the forums.
 
Old 06-19-2011, 07:08 AM   #7
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Huh yes, it's pretty much done from "thinking point of view", the only resisting problem was this one. Hope you don't mind if I explain my way:

1. Get constant variables included, start session and all the other stuff.
2. Depending on page id from $_GET, get corresponding title, description, css file, and other things from database.
3. Here comes the file itself, defining it's own structure and all the things it needs.
4. Footers.

Login, error, and all other pages are made within this concept, that's why I like it.
 
Old 06-19-2011, 07:15 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Yes, that sounds generally good

When you say "css file", I presume that you're not storing the contents of it in the database, only the filename, or it will be a nightmare to edit but I don't see any reason why what you suggest wouldn't work
 
Old 06-19-2011, 07:20 AM   #9
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Yes,
for each page I only store number of columns it has, and corresponding files are loaded the standard way. It's all object oriented, and I have to say that I really recommend using objects.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't connect php to mysql on RHEL5.0 in phpinfo page --without mysql is there vinayakbane Linux - Newbie 1 02-14-2009 11:47 PM
How can i see apache, mysql, php versions? epamuk Linux - Server 1 01-04-2009 04:55 PM
Multiple Versions of PHP danpadams Slackware 1 07-21-2007 03:01 AM
php, iframes and loading a page poeta_boy Programming 3 03-15-2005 03:45 AM
Apache, PHP, MySQL versions? subnet_rx Linux - General 3 06-26-2004 01:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration