PHP Translator help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hackerspk
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2011 3:10 am

PHP Translator help please

Post by hackerspk »

i am a newbie i make this code for word to word translation it works fine until my dictionary is under 50000 words but now it given error time out if there is any way to make this code better? thanks in advance
Error:  Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\salar\engine\modules\show.full.php on line 623

Code

Code: Select all

$user_name = "root";
$password = "";
$database = "salar";
$server = "127.0.0.1";

$mydata = "text to translate";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
   $SQL = "SET NAMES 'utf8'";
   $SQL = "SELECT * FROM dle_roman ORDER BY LENGTH( roman ) DESC";
   $result = mysql_query($SQL);
   while ($db_field = mysql_fetch_assoc($result)) {
      $mydata = str_replace ($db_field['urdu'],$db_field['roman'],$mydata);
   }
   echo $mydata;
   mysql_close($db_handle);
} else {
   print "Database NOT Found ";
   mysql_close($db_handle);
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP Translator help please

Post by Benjamin »

If this isn't something you're running often, just change the time limit. Have a look at http://br2.php.net/manual/en/function.s ... -limit.php
hackerspk
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2011 3:10 am

Re: PHP Translator help please

Post by hackerspk »

if i increase time limit it would be very rough for every one to wait 2 or 3 minutes for each translation
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP Translator help please

Post by Benjamin »

How may fields are in the table?

Why not store the results of the following line of code in the table itself?

Code: Select all

$mydata = str_replace ($db_field['urdu'],$db_field['roman'],$mydata);
You'll probably want to store the output in a cache file for up to an hour or so, depending on how often the data changes. Then just display the cache file instead of executing the code.
hackerspk
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2011 3:10 am

Re: PHP Translator help please

Post by hackerspk »

there are 2 fields
urdu,roman
because mydatabase is very big if i store these results then i need to buy extra mysql storage
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP Translator help please

Post by Weirdan »

how long (typically) is the string you have in $mydata variable?
hackerspk
Forum Newbie
Posts: 4
Joined: Wed Jul 27, 2011 3:10 am

Re: PHP Translator help please

Post by hackerspk »

more than 2000 words less then 10 words
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP Translator help please

Post by Weirdan »

I'd suggest to export the wordlist to a php array, store that somewhere in a file and include that file. Assuming you have opcode cache the loading latency should be pretty low. Of course you'd need to regenerate the file when your database changes.
Post Reply