Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
neralex
Site Admin



Joined: Aug 22, 2007
Posts: 1775

PostPosted: Tue Sep 18, 2012 10:30 am Reply with quote

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';
}


Sad
 
View user's profile Send private message
neralex







PostPosted: Tue Sep 18, 2012 4:03 pm Reply with quote

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.

Bang Head
 
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Tue Sep 18, 2012 8:04 pm Reply with quote

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. 
View user's profile Send private message
neralex







PostPosted: Wed Sep 19, 2012 8:12 am Reply with quote

Palbin, you have saved my day! Many thanks, it works!
 
neralex







PostPosted: Wed Sep 19, 2012 11:04 am Reply with quote

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?

Code:
' , $language , '


and

Code:
' . $language . '
 
Palbin







PostPosted: Wed Sep 19, 2012 12:08 pm Reply with quote

It is a slight efficiency issue. You can probably find a lot of articles searching the web, but here is a link to a nice short comparison.

http://www.fusionswift.com/2010/05/php-concatenation-benchmark-comma-vs-period/

P.S. I must have been tired last night. It looks like I mixed a lot of "." and "," in the echo statements. They should have all been ",".
 
neralex







PostPosted: Wed Sep 19, 2012 12:16 pm Reply with quote

Nice. Thank you.
 
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Thu Sep 20, 2012 6:47 pm Reply with quote

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! 
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©