ISO639DB

Searches ISO 639 codes.

Source code (hide)

Global files are not shown below.

Table of contents
  1. index.php

index.php

back to toc
<?php
/* globals, templates */
$locals = array (
    
'title'       => 'ISO639DB',
    
'description' => 'Searches ISO 639 codes.',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

function 
mysql_fetch_all($result) {
    for (
$i=0$i<mysql_num_rows($result); $i++) {
        
$return[$i] = mysql_fetch_row($result);
    }
    return 
$return;
}

/* connect to the database */
include("dbconnection.php"); // source code for this file is hidden for security reasons

/* variables */
$input = array(
    
'text'       => $_GET['search'],
    
'exact'      => $_GET['exact'],
    
'offset'     => $_GET['offset'],
    
'codes'      => $_GET['codes'],
    
'names'      => $_GET['names'],
    
'iso639_1'   => $_GET['iso639_1'],
    
'iso639_2'   => $_GET['iso639_2'],
    
'iso639_3'   => $_GET['iso639_3'],
    
'individual' => $_GET['individual'],
    
'macrolanguages'=> $_GET['macrolanguages'],
    
'collections'=> $_GET['collections'],
    
'dialects'   => $_GET['dialects'],
    
'reserved'   => $_GET['reserved'],
    
'special'    => $_GET['special'],
    
'living'     => $_GET['living'],
    
'extinct'    => $_GET['extinct'],
    
'ancient'    => $_GET['ancient'],
    
'historic'   => $_GET['historic'],
    
'constructed'=> $_GET['constructed']
);
if(!
$input['offset'] or $input['offset']<0) {
    
$offset 0;
}

if(
$input['text']) {
    
/* build db query */
    // basic
    
$db['query'] = 'SELECT * FROM iso639db WHERE ';

    
// search method
    
if($input['exact']) {
        
$db['search_syntax'] = '=';
    }
    else {
        
$db['search_syntax'] = 'REGEXP';
    }

    
// alias variables for simpler query code
    
$compare = &$db['search_syntax'];
    
$search  = &$input['text'];
    
$offset  = &$input['offset'];

    
// search
    
if(empty($input['codes']) and empty($input['names'])) {
        
$db['query'] .= "(code $compare '$search' OR name $compare '$search') ";
    }
    else {
        
$db['query'] .= '(';
        
// codes
        
if($input['codes']) {
            
$db['query'] .= "code $compare '$search' ";
        }
        
// names
        
if($input['names']) {
            
$db['query'] .= "OR name $compare '$search' ";
        }
        
$db['query'] .= ') ';
    }

    
// restrict by list
    
if($input['iso639_1'] || $input['iso639_2'] || $input['iso639_3']) {
        
$db['query'] .= 'AND (';
        if(
$input['iso639_1']) {
            
$db['query'] .= 'list=\'1\' ';
        }
        if(
$input['iso639_2']) {
            
$db['query'] .= 'OR list LIKE \'2%\' ';
        }
        if(
$input['iso639_3']) {
            
$db['query'] .= 'OR list=\'3\' ';
        }
        
$db['query'] .= ') ';
    }

    
// restrict by scope
    
if($input['individual'] || $input['macrolanguages'] || $input['collections'] || $input['dialects'] || $input['reserved'] || $input['special']) {
            
$db['query'] .= 'AND (';
        if(
$input['individual']) {
            
$db['query'] .= 'scope=\'individual\' ';
        }
        if(
$input['macrolanguages']) {
            
$db['query'] .= 'OR scope=\'macrolanguage\' ';
        }
        if(
$input['collections']) {
            
$db['query'] .= 'OR scope=\'collection\' ';
        }
        if(
$input['dialects']) {
            
$db['query'] .= 'OR scope=\'dialect\' ';
        }
        if(
$input['reserved']) {
            
$db['query'] .= 'OR scope=\'reserved\' ';
        }
        if(
$input['special']) {
            
$db['query'] .= 'OR scope=\'special\' ';
        }
        
$db['query'] .= ') ';
    }

    
// restrict by type
    
if($input['living'] || $input['extinct'] || $input['ancient'] || $input['historic'] || $input['constructed']) {
        
$db['query'] .= "AND (";
        if(
$input['living']) {
            
$db['query'] .= "type='living' ";
        }
        if(
$input['extinct']) {
            
$db['query'] .= "OR type='extinct' ";
        }
        if(
$input['ancient']) {
            
$db['query'] .= "OR type='ancient' ";
        }
        if(
$input['historic']) {
            
$db['query'] .= "OR type='historic' ";
        }
        if(
$input['constructed']) {
            
$db['query'] .= "OR type='constructed' ";
        }
        
$db['query'] .= ") ";
    }

    
// complete query string
    
$db['query'] .= 'ORDER BY list,code ';
    
    
// limiting
    
$db['query'] .= 'LIMIT 501 ';
    if(
$input['offset']) {
        
$db['query'] .= "OFFSET $offset ";
    }
    
    
// clean up SQL
    
$reg['search']  = '#\(\s*(?:AND|OR)\s*#';
    
$reg['replace'] = '(';

    
$db['query'] = preg_replace($reg['search'],$reg['replace'],$db['query']);

    
/* query db */
    
$db['queryobj'] = mysql_query($db['query']);
    if(
$db['queryobj']) {
        
$db['result'] = mysql_fetch_all($db['queryobj']);
        
$db['count']  = count($db['result']);
    }
    else {
        echo 
'<p class="fail">Database transaction failed: ' mysql_error() . '</p>';
        
$db['count'] = 0;
    }

    
/* prepare navigation */
    
if($db['count']>500 || $input['offset']>0) {
        
// start nav box
        
$output['navlinks'] = '<div style="border:1px solid #CCC;">';
    
        
// carry over GET data
        
$output['navpath'] = $_SERVER['SCRIPT_NAME'] . "?" $_SERVER['QUERY_STRING'];
        
$output['navpath'] = preg_replace('/&offset=[^&]*/','',$output['navpath']);
        
        
// build nav links
        
if($input['offset']>0) {
            
$output['navpath_prev'] = $output['navpath'] . "&offset=" . ($input['offset']-500);
            
$output['navlinks'] .= '&lt;&lt;<a href="' $output['navpath_prev'] . '" title="previous 500">previous 500</a>';
        }        
        if(
$db['count']>500) {
            
$output['navpath_next'] = $output['navpath'] . "&offset=" . ($input['offset']+500);
            
// visually separate links
            
if($output['navpath_prev']) {
                
$output['navlinks'] .= " | ";
            }
            
$output['navlinks'] .= '<a href="' $output['navpath_next'] . '" title="next 500">next 500</a>&gt;&gt;';
        }
        
        
// close nav box
        
$output['navlinks'] .= '</div>';
    }
    
    
/* build table */
    
if($db['result']) {
        for(
$i=0$i<$db['count'] && $i<500$i++) {
            
$output['table'] .= '<tr>' 
            
'<td>' preg_replace('/^(.*)$/','ISO 639-$1',$db['result'][$i][0]) . '</td>' .
            
'<td>' $db['result'][$i][1] . '</td>' .
            
'<td>' $db['result'][$i][2] . ( $db['result'][$i][3] ? '"' htmlentities($db['result'][$i][3], ENT_NOQUOTES'UTF-8') . '"' "" ) . '</td>' .
            
'<td>' $db['result'][$i][4] . '</td>' .
            
'<td>' $db['result'][$i][5] . '</td>' .
            
'<td>' $db['result'][$i][6] . '</td>' .
            
'</tr>';
        }
    }
}

/* Output */
gDebug(get_defined_vars());
// result
if($output['table']) {
    echo 
'<h2>Query result</h2>' $output['navlinks'] . '<table>';
    
?>
        <tr>
            <th>List</th>
            <th>Code</th>
            <th>Language name</th>
            <th>Scope</th>
            <th>Type</th>
            <th>Notes</th>
        </tr>
    <?php
    
echo $output['table'] . '</table>' $output['navlinks'];

    
// output query
    
echo '<p style="font-style:0.9em; color:#CCC;">query: ' $db['query'] . '</p>';
}
else if(
$input['text']) {
    echo 
'<p class="fail">No languages were found with these criteria and options.</p>';
}

// input form
?>
<h2>Search</h2>
<form action="index.php" method="get">
     <label for="search">Search by code or language name</label><br />
    <input
        type="text"
        id="search"
        name="search"
        value="<?php echo $input['text']; ?>"
    /><br />
    <input
        type="checkbox"
        id="exact"
        name="exact"
        value="1"
        <?php gCheckBox($input['exact']); ?>
    /> <label for="exact">Exact matches only</label><br />
    <input
        type="submit"
        value="search"
        id="submit"
    /><br />
    <input
        type="reset"
        value="reset form"
    /><br />

    <div class="section">
        <h3>Extended search options</h3>
        <p>Select checkboxes below if you want to restrict the displayed results to the selected criteria. If no checkboxes in a category are selected, all matches for that category will be returned.</p>
            
        <fieldset>
            <legend>Fields</legend>
            <input
                type="checkbox"
                id="codes"
                name="codes"
                value="1"
                <?php gCheckBox($input['codes']); ?>
            /> <label for="codes">code</label><br />
            <input
                type="checkbox"
                id="names"
                name="names"
                value="1"
                <?php gCheckBox($input['names']); ?>
            /> <label for="names">language name</label>
        </fieldset>
        <fieldset>
            <legend>Code lists</legend>
            <input
                type="checkbox"
                id="iso639_1"
                name="iso639_1"
                value="1"
                <?php gCheckBox($input['iso639_1']); ?>
            /> <label for="iso639_1">ISO 639-1</label><br />
            <input
                type="checkbox"
                id="iso639_2"
                name="iso639_2"
                value="1"
                <?php gCheckBox($input['iso639_2']); ?>
            /> <label for="iso639_2">ISO 639-2</label><br />
            <input
                type="checkbox"
                id="iso639_3"
                name="iso639_3"
                value="1"
                <?php gCheckBox($input['iso639_3']); ?>
            /> <label for="iso639_3">ISO 639-3</label>
        </fieldset>
        <fieldset>
            <legend><a href="http://www.sil.org/iso639-3/scope.asp" title="about scopes">Scopes</a></legend>
            <input
                type="checkbox"
                id="individual"
                name="individual"
                value="1"
                <?php gCheckBox($input['individual']); ?>
            /> <label for="individual">individual languages</label><br />
            <input
                type="checkbox"
                id="macrolanguages"
                name="macrolanguages"
                value="1"
                <?php gCheckBox($input['macrolanguages']); ?>
            /> <label for="macrolanguages">macrolanguages</label><br />
            <input
                type="checkbox"
                id="collections"
                name="collections"
                value="1"
                <?php gCheckBox($input['collections']);
            
?> /> <label for="collections">collections</label><br />
            <input
                type="checkbox"
                id="dialects"
                name="dialects"
                value="1"
                <?php gCheckBox($input['dialects']); ?>
            /> <label for="dialects">dialects</label><br />
            <input
                type="checkbox"
                id="reserved"
                name="reserved"
                value="1"
                <?php gCheckBox($input['reserved']); ?>
            /> <label for="reserved">reserved for local use</label><br />
            <input
                type="checkbox"
                id="special"
                name="special"
                value="1"
                <?php gCheckBox($input['special']); ?>
            /> <label for="special">special situations</label>
        </fieldset>
        <fieldset>
            <legend><a href="http://www.sil.org/iso639-3/types.asp" title="about types">Types</a></legend>
            <input
                type="checkbox"
                id="living"
                name="living"
                value="true" <?php gCheckBox($input['living']); ?>
            /> <label for="living">living</label><br />
            <input
                type="checkbox"
                id="extinct"
                name="extinct"
                value="true" <?php gCheckBox($input['extinct']); ?>
            /> <label for="extinct">extinct</label><br />
            <input
                type="checkbox"
                id="ancient"
                name="ancient"
                value="true" <?php gCheckBox($input['ancient']); ?>
            /> <label for="ancient">ancient</label><br />
            <input
                type="checkbox"
                id="historic"
                name="historic"
                value="true" <?php gCheckBox($input['historic']); ?>
            /> <label for="historic">historic</label><br />
            <input
                type="checkbox"
                id="constructed"
                name="constructed"
                value="true" <?php gCheckBox($input['constructed']); ?>
            /> <label for="constructed">constructed</label><br /><br />
        </fieldset>
        <?php gDebugOption(); ?>        
        <input
            type="submit"
            value="search"
        /><br />
        <input
            type="reset"
            value="reset form"
        />
    </div>
</form>

<?php
/* globals, templates */
makeFooter($license);
?>

Search






Extended search options

Select checkboxes below if you want to restrict the displayed results to the selected criteria. If no checkboxes in a category are selected, all matches for that category will be returned.

Fields
Code lists

Scopes




Types







This tool is written and copyright by Jesse Plamondon-Willard (Pathoschild). You may freely use, distribute, and modify this script in any way and for any purpose, so long as you cite the above name as original author.