[SOLVED] Loading multiple page versions with PHP and MySQL
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?)
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...:
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.
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).
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 . '.');
?>
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
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)
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.