Looking for solution to avoid eval() - php

I need to create some sort of knowledge data base for updates (using PHP and MySQL).
Im using a database to store every update in HTML format.
People that are using the system work with different languages (information about users is stored in the DB).
Sometime the updates contain a text that should be translated and displayed taking into consideration the language that the user is working with.
What approach would you suggest, having in mind that i would like to avoid storing PHP and HTML in the DB.
I was thinking to create a col for every language and to store there the translation of every update, but what will happen it the update contains 2 different translations.
Prototype of the updates:
Some initial text
Translation of some part of the text
Some more text
Second translation
Final update text

I would create a table for languages and a table for updates. Then create a table that links languages to updates. One column for id, one column for language id, one column for update id. Then, you will be able to handle an update with multiple translations.

Another solution would be to use some sort of cdata tag, or something that you could parse easily enough. But this would be a very "wiki" based solution so there are patterns to follow to avoid mixing.
Example:
[English<-[This is the best update ever]->]
[Swedish<-[Detta är den bästa uppdateringen någonsin]->]

Related

Using json field in mysql for page content

I'm creating a website with laravel with a simple page builder where standard page have common fields but some other pages (like home) may have additional fields.
in this case is it good to use json field or another table like page_fields to store additional data?
I've seen many suggestion to avoid using json in term of performance and managing data (like where clause)
must note that in my case these fields will be used just to show them and also can be translated later
thanks in advance
Text would be more suitable along with some templating language like markdown to be stored in such column instead of pure html

How to store multi-lingual website information in database?

I'm currently creating a multi-lingual website, and I have different types of words that are being translated all across the website. However, whats the best practice when it comes to storing this in the database?
This is what I currently do:
I have the language Norwegian and English.
My database looks like this:
Whenever I add a new language it get added into the languages table like this:
At the same time a new table will be created with looking like this language_en. Using the value in language_val.
Later on I use URL routing to retrieve en, no or whatever language and fetch the website information from the correct table.
My question is, should I rather have only 1 table and use the language_id in this one? or is it perfectly fine to split up languages into multiple tables?
Each language table would look just like this:
With Silverlight (and later with PHP) I've done this by putting the language's in the same table, this worked fine and was user friendly (easy to edit). Only when retrieving data you should off course only select the language you need.

Best way to translate database driven content

I've been struggling with this for a while now. One my CMS' is ready to be extended with a translation module. I've been thinking of different methods but haven't figured out what is the best way so far.
Basically I have a CMS which uses a template system to parse all data from a database to the screen. I've come so far to "split up" my templates in different folders to be able to translate things that are "static" like images with text, footer links, etc.
However, there are many modules (pages, news, products) that have multiple fields that require a database driven method to be translated. I started off with a "languages" table which describes languages (id, iso_code, name). That's as far as I've come.. since there were a couple of projects that had to be done I haven't spend any more time to this subject thus far.
My first thought ("the quick fix") was to add multiple fields inside the tables (such as "title_nl", "title_en"), but this actually makes the database more crowded than is needed in my opinion.
My second thought was to create a table, "news_translations" for example. Which contained the language iso code, a news_id, the fields that require translation. Obviously a news_id connects the translation to it's original and the language iso code is used to get the right language from the database. Then in my front-end code I would first check if the default language is selected (=> select from the "news" table) or a translation (=> check inside translations table). If the 2nd case does not return any results a message is display "Sorry, no translation available" and the default is shown (or an error message, what fits the client best..).
But then there's a 3rd option.. my websites all use search engine friendly links (www.domain.com/pagename/ or www.domain.com/news/1-news-item-here.html). It would be far better if I had the ability to also "override" the SEF URL in my translations table. But I guess in this case I would always need 1 extra query to the translations table (since we first want to check for a translated page)... guess it's not such a big deal, but it's worth considering I guess.
In the end I guess by describing my options number 3 is what I need. But I'd like to have some other opinions on the subject as well! This is what I am trying to achieve:
Create a CMS system with multi language support
No language files (obviously this is why I use templates)
Being able to translate an original page/newsitem/product
Optionally: to change the SEF URL according to the language
I think option 3 has all this.. so the steps to create this solution is:
Create a _translation table for each item (or perhaps even in the
original by adding 2 new fields 'translation_to' (containing the
PrimaryKey) and 'translation_is' (containing ISO code) - however..
in that case all fields would need to be edited (which is not always
necessary.. plus by creating a 2nd table I keep the originals
divided with their translations, right?)
If the default language is NOT chosen first query the translations table to find a translation, if one is found display the
translation. Otherwise notify/error the user and/or display the
original text (based on the SEF URL... if the SEF is not found
within the translations or original table, then obviously display an
error only).
Any suggestions? :-)
Thanks for thinking along!
I would like to see what your table structure looks like. Probably the best thing you can do is generate two seperate new tables named something like "CONTENT_MULTI_LANG" & "SITE_LOCALES".
Then in the code that prints out your content do an initial check for a language flag. I'd create two separate classes for loading static content, something like "Content_LoadStandard" and "Content_LoadMultiLang". So then your conditional will look like this.
if ($this->site_locale == 'standard'){
$contentLoader = new Content_LoadStandard();
} else {
$contentLoader = new Content_LoadMultiLang($this->site_locale);
}
$content->blah($cheese);
Your "CONTENT_MULTI_LANG" table should be a narrowed down version of your standard CMS object table, only containing the relevant content field(s) that need to be in alternative languages.
// PSEUDO SQL
CREATE TABLE `LOCALE` (
`id` int(11),
`locale` varchar(16), // name of locale (language)
... // any other fields
)
CREATE TABLE `CONTENT_MULTI_LANG` (
`id` int(11),
`pcid` int(11), // parent content id
`lid` medint(), // locale id
`content` {$type}, // whatever type you use (varchar, text, bin, etc)
... // any other fields
)
In your Content_LoadMultiLang class, create methods to query alternate content using a join.
TIP: Might be a good idea to establish relationships in your table to do cascading deletes on content rows, that way if you delete content in standard your multi lingual version(s) will also be deleted.
From what I've seen from Drupal, option three is how they handle it, with a couple of tweaks. They keep it all in one table and a field called language. Then there is a separate table that maps which items are connected.
This way is primary language agnostic, meaning the content can be created in any language without requiring a translation in any other.

Translations for non dynamic data

I am currently making a web page with dynamic content from a database (a restaurant menu) storing item names, prices, yada yada. The restaurant usually caters to English and Japanese. I have stored the titles of the items in the database as English and Japanese, however when it comes to the type of the item, for example, Type: Drink, Type of Drink: Coffee I can obviously store the type of item alongside the item in the database, but it hardly seems worth it to store it twice as two different languages in the the database which would also make the form to add a new item a pain.
What is the best way to go about translating those little tiny pieces of information, like "Coffee", "Wine", "Click Here", "Menus", and using them on the page?
You can use gettext extension for translation static or predefined info on web page.
See manual http://php.net/manual/en/book.gettext.php
Example http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/
I would store it in both languages, it makes the most sense to me. I don't see why you would go and do a request to your translation system each time you want to show any page in your menu.
If you are using an automatic translation system that you really trust, one thing that you could do is use it when adding a new item to make a suggestion. So, after the user enters the word in English on the form, automatically attempt to translate that into Japanese and put it in the Japanese field. If the user is satisfied with the translation, that's it for that task, otherwise he/she inputs with a better one.
if you are using any phph5 based framework , major framework have localization support like zend has Zend_Locale
if you want your own translation dictionary , keep key value pair data in database table and memcached between application and DB , so static data will be cached for particular expiry time and data base load will be get decreased and fast retrieval of data

HTML Table row edit/Delete

I have an HTML table with contents, I would like to have an feature of Edit/Delete to that table. How do I do it with PHP?
I actually think that this sounds more like a job for JavaScript, which can edit/remove rows on-the-fly and with much less code. (Implement some AJAX too, and you can edit/remove rows in database too).
But if you insist on using PHP, you might just want to add some GET parameters to the Edit/Delete links that would delete or edit those rows.
Well, there is a pure PHP way to do it, and then there is a combination of Javascript and PHP. You must use PHP one way or another if you want your changes to the database to be permanent as that is your gateway to communicating with the database (as far as I know you cannot do that with Javascript as that is client-based and runs entirely in your web browser).
If using just PHP, you must generate HTML documents for each change. E.g., you click on one cell in the table and that gets you to a new HTML page where the field is editable through an input element; or you can list all fields at once for that row and edit them all at the same time. The fields are then posted in a form to a PHP page which will take the new values and update the database (or insert new values or however you wish it to behave). Here's a tutorial for how to do this:
http://www.freewebmasterhelp.com/tutorials/phpmysql/1
You can also mix in some Javascript which allows a more interactive interface to modifying the values in a cell. However, this obviously requires more code and may be overkill for what you're trying to do. Nonetheless, here is a link which demonstrates just that and also shows the code:
http://www.java2s.com/Code/JavaScript/GUI-Components/Editabletablecell.htm
Hope this is what you're looking for.
EDIT:
Forgot that you also wished to delete content in the table. That is also explained in the first link.
If you intend to work with databases, and it seems like you have little understanding of how they work, pick up a good book like: SQL - The Complete Reference. When you have enough knowledge of SQL, look at PHP's PDO extension: http://php.net/manual/en/book.pdo.php

Categories