TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-22-2009, 09:39 PM   #1 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default Language database

Hi guys,

I've got a table of fields for translations but am looking for an effective way to display them all in a form and update them.

The table looks like:

LANGUAGES
id
language_name
translation1
translation2
translation3
etc

How would i output the all these with a title and an input textbox without having to do this:

PHP Code:
$thome $row["thome"];
$tevents $row["tevents"];
$tvenues $row["tvenues"];

echo 
$thome.'<input type="text" name="thome" id="thome" />'.
$tevents.'<input type="text" name="tevents" id="tevents" />'.
$tvenues.'<input type="text" name="tvenues" id="tvenues" />'
That is going to be a huge list, plus that doesn't even include an input value which means even more work and there would be a huge list to save the inputed/edited text. Any ideas how to approach this would be great.
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 04-23-2009, 01:08 AM   #2 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

I would suggest abit different database structure.

Make a language table that contains all the languages available:
sql Code:
CREATE TABLE `languages`
(
    `id` INT NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(48) NOT NULL,
    `default` TINYINT(1) DEFAULT '0',
    PRIMARY ID(`id`)
);

And then a phrase table:
sql Code:
CREATE TABLE `phrase`
(
    `id` INT NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(128) NOT NULL,
    `phrase` TEXT NOT NULL,
    `phrasegroup` VARCHAR(24) NOT NULL,
    `language` INT NOT NULL,
    PRIMARY KEY(`id`)
);

So the design is like, you have a language table with the languages and a phrase tables with all the translations. You fill in a language like:
sql Code:
INSERT INTO `languages` (`title`, `default`) VALUES ('English', 1);

And you insert phrases like this:
sql Code:
INSERT INTO `phrase` (`title`, `phrase`, `phrasegroup`, `language`) VALUES ('greeting', 'Hello %s', 'default', 1);

The order works as:
  • title: The title of the phrase
  • phrase: The phrase itself, you may want to use sprintf-alike conversions here
  • phrasegroup: This is mainly for performance, you set a phrasegroup and the script may want to get a phrasegroup for "these type of functions do this", so you can seperate them
  • language: The language id from the language table

So in your bootstraper script you would do:
php Code:
/* If this script is included from a separate file, you may wish to define $phrasegroups with an array of phrasegroups you want to precache */
if(!isset($phrasegroups))
{
    $phrasegroups = Array('default');
}
else
{
    array_push($phrasegroups, 'default');
}

/* Assumes you have the default language id, either for this user or from your options table assigned as $language_id */

/* Assumes a database connection is made, correct the calls so they fit your API */
$p = $db->query('SELECT `title`, `phrase` FROM `phrases` WHERE `phrasegroups` IN (\'' . implode('\', \'', $phrasegroups) . '\') AND `language` = ' . (integer) $language_id);

if(!$p->getNumRows())
{
    /* throw a fatal error here, no phrases were "downloaded" */
    exit;
}

/* Make a simple class, that contains the phrases in form of $language->phrasegroup['phrase'] = 'value'; */

$language = new stdClass;
$language->phrases = Array();

foreach($p as $r)
{
    if(!isset($language->{$p})
    {
        $language->{$p} = Array();
    }

    $language->{$p}[$r['title']] = $r['phrase'];
    $language->phrases[$r['phrase']] =& $language->{$p}[$r['title']];
}

unset($p, $r);

And then we need a utility function for formatting phrases in a sprintf-alike format:
php Code:
function phrase($title)
{
    global $language;

    if(!isset($language->phrases[$title]))
    {
        return('');
    }
    elseif(func_num_args() == 1)
    {
        /* No formatting */
        return($language->phrases[$title]);
    }

    $args = func_get_args();
    $args[0] = $language->phrases[$title];

    return(call_user_func_array('sprintf', $args));
}

Now as you fill in phrases you should be able to simply do:
php Code:
echo phrase('greeting', 'Kalle');

That would print:
Code:
Hello Kalle
(That is, if you added that 'greeting' phrase for english above), or if your language was danish and the phrase looked like:
sql Code:
INSERT INTO `languages` (`title`, `default`) VALUES ('Dansk', 0);
sql Code:
INSERT INTO `phrase` (`title`, `phrase`, `phrasegroup`, `language`) VALUES ('greeting', 'Hej %s', 'default', 2);

it would print:
Code:
Hej Kalle
Hope its not too complex, I know I rushed abit here since its late ;)
__________________

Last edited by Kalle : 04-23-2009 at 09:31 AM.
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
oMIKEo (04-23-2009)
Old 04-23-2009, 09:12 AM   #3 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default

Hi Kalle, thank you for such a detailed answer. It's a little complex but i understand the approach you are going for and that should make it easier to manage! Thanks!
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 04-23-2009, 09:33 AM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

No problem, feel free to questioning more if needed ;)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Injection and mysql_real_escape_string Durux General 61 01-29-2013 12:20 PM
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Adding Images to a database from a folder Rendair Advanced PHP Programming 3 01-13-2008 07:40 PM
Important Database Structure Question! AnthonyOS MySQL & Databases 5 12-20-2007 03:26 PM


All times are GMT. The time now is 10:25 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design