Author |
Message |
neralex
Site Admin

Joined: Aug 22, 2007
Posts: 1775
|
Posted:
Tue Sep 18, 2012 10:30 am |
|
Hey!
I want compare the existing languages with stored documents in my database table. The documents are divided into two types. 1 and 2. I use an admin function with a variable to determine the type. How i can compare all stored languages of the documents table with the existing languages files and not with only one part of a array?
for example: /admin.php?op=docadd&type=1
Code:function docadd($type) {
global $prefix, $db, $admin_file, $language, $multilingual;
$qry = $db->sql_query('SELECT lang FROM ' . $prefix . '_documents WHERE type=\'' . $type . '\'');
while (list($lang) = $db->sql_fetchrow($qry)) {
$langarray[] = $lang;
}
include_once 'header.php';
OpenTable();
if ($multilingual == 1) {
echo _LANGUAGE . ': <select name="doclang">' . PHP_EOL;
$temp_dir = dir(NUKE_LANGUAGE_DIR);
$langlist = '';
while($file = $temp_dir->read()) {
if (substr($file, 0, 5) == 'lang-') {
$langlist .= $file . ' ';
}
}
closedir($temp_dir->handle);
$langlist = explode(' ', $langlist);
sort($langlist);
for ($i = 0; $i < count($langlist); $i++) {
if ($langlist[$i] != '') {
$tl = str_replace('lang-', '', $langlist[$i]);
$tl = str_replace('.php', '', $tl);
if ($tl != $langarray[0]) { // < how i can compare it here?
echo ' <option value="' . $tl . '"' . ($tl == $language ? ' selected="selected"' : '') . '>' . ucwords($tl) . '</option>' . PHP_EOL;
}
}
}
echo '</select><br /><br />' . PHP_EOL;
} else {
echo '<input type="hidden" name="doclang" value="' . $language . '" />' . PHP_EOL;
}
CloseTable();
include_once 'footer.php';
}
|
 |
|
|
|
 |
neralex

|
Posted:
Tue Sep 18, 2012 4:03 pm |
|
I have the following languages installed:
English
German
French
Spanish
In the docuemnt type 1 I have stored two documents with the languages "english" and "german". Now i have added an ORDER BY in the sql-query and a 2nd for loop inside of the 1st loop.
Code:function docadd($type) {
global $prefix, $db, $admin_file, $language, $multilingual;
$qry = $db->sql_query('SELECT lang FROM ' . $prefix . '_documents WHERE type=\'' . $type . '\' ORDER BY lang ASC');
while (list($lang) = $db->sql_fetchrow($qry)) {
$langarray[] = $lang;
}
include_once 'header.php';
OpenTable();
if ($multilingual == 1) {
echo _LANGUAGE . ': <select name="doclang">' . PHP_EOL;
$temp_dir = dir(NUKE_LANGUAGE_DIR);
$langlist = '';
while($file = $temp_dir->read()) {
if (substr($file, 0, 5) == 'lang-') {
$langlist .= $file . ' ';
}
}
closedir($temp_dir->handle);
$langlist = explode(' ', $langlist);
sort($langlist);
for ($i = 0; $i < count($langlist); $i++) {
for ($l = 0; $l < count($langarray); $l++) {
if ($langlist[$i] != '') {
$tl = str_replace('lang-', '', $langlist[$i]);
$tl = str_replace('.php', '', $tl);
if ($tl == $langarray[$l]) { // < how i can compare it here?
echo ' <option value="' . $tl . '">' . ucwords($tl) . '</option>' . PHP_EOL;
}
}
}
}
echo '</select><br /><br />' . PHP_EOL;
} else {
echo '<input type="hidden" name="doclang" value="' . $language . '" />' . PHP_EOL;
}
CloseTable();
include_once 'footer.php';
}
|
If I match the existing language files with the languages of the two documents comparable in type 1, then I get the correct result in my select box:
1 English
3 German
If I want to get in my select box ($tl != $langarray[$l]), only the unsaved languages in the output, then I get the following result:
1 English
2 French
2 French
3 German
4 Spanish
4 Spanish
Arrays are not my strong point and I get a headache every time.
 |
|
|
|
 |
Palbin
Site Admin

Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania
|
Posted:
Tue Sep 18, 2012 8:04 pm |
|
Try this:
Code:
function docadd($type) {
global $db, $language, $multilingual, $prefix;
$qry = $db->sql_query('SELECT `lang` FROM `' . $prefix . '_documents` WHERE `type` = \'' . $type . '\' ORDER BY `lang` ASC');
while (list($lang) = $db->sql_fetchrow($qry, SQL_NUM)) {
$langarray[] = $lang;
}
include_once 'header.php';
OpenTable();
if ($multilingual == 1) {
$temp_dir = dir(NUKE_LANGUAGE_DIR);
$langlist = array();
while(false !== ($file = $temp_dir->read())) {
if (preg_match('/lang-(.*?)\.php/i', $file, $lang)) {
$langlist[] = $lang[1];
}
}
closedir($temp_dir->handle);
sort($langlist);
echo _LANGUAGE , ': <select name="doclang">' , PHP_EOL;
for ($i = 0, $count = count($langlist); $i < $count; $i++) {
if (!empty($langlist[$i])) {
if (!in_array($langlist[$i], $langarray)) {
echo '<option value="' . $langlist[$i] . '">' , ucwords($langlist[$i]) , '</option>' , PHP_EOL;
}
}
}
echo '</select><br /><br />' , PHP_EOL;
} else {
echo '<input type="hidden" name="doclang" value="' , $language . '" />' , PHP_EOL;
}
CloseTable();
include_once 'footer.php';
}
|
|
_________________ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. |
|
|
 |
neralex

|
Posted:
Wed Sep 19, 2012 8:12 am |
|
Palbin, you have saved my day! Many thanks, it works! |
|
|
|
 |
neralex

|
Posted:
Wed Sep 19, 2012 11:04 am |
|
One question for my understanding:
I have it seen in many scripts, that you are using "," instead of "." - what is the difference between the both?
and
|
|
|
|
 |
Palbin

|
Posted:
Wed Sep 19, 2012 12:08 pm |
|
|
|
 |
neralex

|
Posted:
Wed Sep 19, 2012 12:16 pm |
|
|
|
 |
montego
Site Admin

Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Thu Sep 20, 2012 6:47 pm |
|
Be careful... this is possible due to how 'echo' is coded. In other words, this is not a string concatenation technique. In the echo function, these are actually additional "parameters" being "passed"... |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|
|
 |
|