I’m developing a parents evening booking system as part of my A level programming project, but I’ve just gotten stuck.
I’ll try explain the project the best I can in this post, so I can give you guys a clear picture of what the application does.
So far:
I have created a sort of language selector function with php, which gets the users language selection before logging in based upon a cookie. Here’s the code for the script (Yes, very messy right now, but i’ll fix it later on):
function check_language() {
//directory name for the custom defined languages.
$dir = 'languages';
//set a default language
$default = 'english';
//make sure that we have a language selected...
if (!isset($_COOKIE["lang"])){
//if there is no cookie set, set one.
setcookie("lang", "english", time()+3600*24*365);
}else{
$lang = ($_COOKIE["lang"]);
}
//build the path string.
$path = $dir."/".$lang.".php";
if (file_exists($path)) {
//return the selected language pack directory.
return $path;
}else{
//protect the server, by returning the default language path...
return $dir."/".$default.".php";
}
}
Whenever I want my PHP script to access language files, I call the function I have created and then include the language file on the page like so:
$lang = check_language();
include_once($lang);
english.php file…
$txt['languagename'] = "English";
//text for the index page...
$txt['titletext'] = 'Parents evening booking system';
$txt['welcometext1'] = 'Welcome to the parents evening booking system for ';
welsh.php file…
$txt['languagename'] = "Cymraeg";
//text am y tudalen index...
$txt['titletext'] = 'System bwcio noson rhieni';
$txt['welcometext1'] = 'Croeso i system bwcio noson rhieni ';
So for example, if the users cookie contains ‘welsh’, the file welsh.php would be included, and I would have access to an associative array ($txt), with the supplied translations.
That part of my script works perfectly so far.
Now i’m coding the admin page, and I need an option to be able to add a school year to the database through the admin panel.
This is where i’m a bit confused on what to do, as the string (‘year 10’ for example) would be ‘blwyddyn 10’ in welsh. So this means I would need to add another element to the associate array for all language files with a string for the required language so it could be accessed in the script. (if this makes any sense at all).
I have created a table in the database for all languages like so:
languageid name filename
1 English english.php
2 Welsh welsh.php
Then I wrote this code to connect to the database and create an input box for each language in the database, along with a “translation id key” input box.
include'/inc/functions.php');
ConnectToDB();
$query = "SELECT * FROM languages WHERE 1";
$result = mysql_query($query);
$languages = array();
while($row = mysql_fetch_assoc($result)){
$languages[] = $row['name'];
}
echo 'Translation key id: <input type="text" name="languagetermkey"/>';
echo '</br>';
foreach($languages as $item){
echo 'Year name ('.$item. " string):";
echo '<input type="text" name="'.$item.'String'.'"/>'; //e.g "EnglishString"
echo "</br>";
}
Now if I wanted to add the terms above (the text from both input boxes) into my language files (“english.php” and “welsh.php”) how would I go about sending an array with an ajax post request?
Previously I have been using this code, but I guess it won’t work for what I want to do this time 🙁
$.ajax({
type: 'post',
url: 'add_class.php',
data: 'classname=' + "test",
error: function(){
alert('There was a problem adding the class !');
},
success: function() {
alert("Class has been added successfuly");
}
});
Is this the right way to go about doing this sort of thing ? If not, can somebody guide me towards the right direction on what to do? i’m just a bit confused and this is my first time using jQuery.
I know this post is long, and I do appreciate people’s efforts for reading the whole thing.
Thanks in advance for any replies (Y)
For the bit about sending the array with AJAX, you can serialize a form and pass that, meaning you can access it just as if it was a normal POST from PHP.
For the “Year 10”, you can use sprintf or vsprintf like this – example showing both sprintf and vsprintf.