Documentation TYPO3 par Ameos

db_search.php

00001 <?php
00002 /* $Id: db_search.php,v 2.2 2003/11/26 22:52:24 rabus Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
00012 require('./db_details_common.php');
00013 // If config variable $cfg['Usedbsearch'] is on FALSE : exit.
00014 if (!$cfg['UseDbSearch']) {
00015     PMA_mysqlDie($strAccessDenied, '', FALSE, $err_url);
00016 } // end if
00017 $url_query .= '&amp;goto=db_search.php';
00018 
00019 
00023 $list_tables  = PMA_mysql_list_tables($db);
00024 $num_tables   = ($list_tables ? mysql_num_rows($list_tables) : 0);
00025 for ($i = 0; $i < $num_tables; $i++) {
00026     $tables[] = PMA_mysql_tablename($list_tables, $i);
00027 }
00028 if ($num_tables) {
00029     mysql_free_result($list_tables);
00030 }
00031 
00032 
00036 $sub_part = '';
00037 require('./db_details_links.php');
00038 
00039 
00043 if (isset($submit_search)) {
00044 
00057     function PMA_getSearchSqls($table, $search_str, $search_option)
00058     {
00059         global $err_url;
00060 
00061         // Statement types
00062         $sqlstr_select = 'SELECT';
00063         $sqlstr_delete = 'DELETE';
00064 
00065         // Fields to select
00066         $local_query           = 'SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']);
00067         $res                   = @PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
00068         $res_cnt               = ($res ? mysql_num_rows($res) : 0);
00069         for ($i = 0; $i < $res_cnt; $i++) {
00070             $tblfields[]       = PMA_backquote(PMA_mysql_result($res, $i, 'field'));
00071         } // end if
00072         $sqlstr_fieldstoselect = ' ' . implode(', ', $tblfields);
00073         $tblfields_cnt         = count($tblfields);
00074         if ($res) {
00075             mysql_free_result($res);
00076         }
00077 
00078         // Table to use
00079         $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
00080 
00081         // Beginning of WHERE clause
00082         $sqlstr_where    = ' WHERE';
00083 
00084         $search_words    = (($search_option > 2) ? array($search_str) : explode(' ', $search_str));
00085         $search_wds_cnt  = count($search_words);
00086 
00087         $like_or_regex   = (($search_option == 4) ? 'REGEXP' : 'LIKE');
00088         $automatic_wildcard   = (($search_option <3) ? '%' : '');
00089 
00090         for ($i = 0; $i < $search_wds_cnt; $i++) {
00091             // Elimines empty values
00092             if (!empty($search_words[$i])) {
00093                 for ($j = 0; $j < $tblfields_cnt; $j++) {
00094                     $thefieldlikevalue[] = $tblfields[$j]
00095                                          . ' ' . $like_or_regex
00096                                          . ' \''
00097                                          . $automatic_wildcard
00098                                          . $search_words[$i]
00099                                          . $automatic_wildcard . '\'';
00100                 } // end for
00101 
00102                 $fieldslikevalues[]      = ($search_wds_cnt > 1)
00103                                          ? '(' . implode(' OR ', $thefieldlikevalue) . ')'
00104                                          : implode(' OR ', $thefieldlikevalue);
00105                 unset($thefieldlikevalue);
00106             } // end if
00107         } // end for
00108 
00109         $implode_str  = ($search_option == 1 ? ' OR ' : ' AND ');
00110         $sqlstr_where .= ' ' . implode($implode_str, $fieldslikevalues);
00111         unset($fieldslikevalues);
00112 
00113         // Builds complete queries
00114         $sql['select_fields'] = $sqlstr_select . $sqlstr_fieldstoselect . $sqlstr_from . $sqlstr_where;
00115         $sql['select_count']  = $sqlstr_select . ' COUNT(*) AS count' . $sqlstr_from . $sqlstr_where;
00116         $sql['delete']        = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
00117 
00118         return $sql;
00119     } // end of the "PMA_getSearchSqls()" function
00120 
00121 
00125     if (!empty($search_str) && !empty($search_option)) {
00126 
00127         $original_search_str = $search_str;
00128         $search_str          = PMA_sqlAddslashes($search_str, TRUE);
00129 
00130         // Get the true string to display as option's comment
00131         switch ($search_option) {
00132             case 1:
00133                 $option_str = ' (' . $strSearchOption1 . ')';
00134                 break;
00135             case 2:
00136                 $option_str = ' (' . $strSearchOption2 . ')';
00137                 break;
00138             case 3:
00139                 $option_str = ' (' . $strSearchOption3 . ')';
00140                 break;
00141             case 4:
00142                 $option_str = ' (' . $strSearchOption4 . ')';
00143                 break;
00144         } // end switch
00145 
00146         // If $table is defined or if there is only one table in $table_select
00147         // set $onetable to the table's name (display is different if there is
00148         // only one table).
00149         //
00150         // Recall:
00151         //  $tables is an array with all tables in database $db
00152         //  $num_tables is the size of $tables
00153         if (isset($table)) {
00154             $onetable           = $table;
00155         }
00156         else if (isset($table_select)) {
00157             $num_selectedtables = count($table_select);
00158             if ($num_selectedtables == 1) {
00159                 $onetable       = $table_select[0];
00160             }
00161         }
00162         else if ($num_tables == 1) {
00163             $onetable           = $tables[0];
00164         }
00165         else {
00166             for ($i = 0; $i < $num_tables; $i++) {
00167                 $table_select[] = $tables[$i];
00168             }
00169             $num_selectedtables = $num_tables;
00170         } // end if... else if... else
00171         ?>
00172 <br />
00173 
00174         <?php
00175         $url_sql_query = PMA_generate_common_url($db)
00176                    . '&amp;goto=db_details.php'
00177                    . '&amp;pos=0'
00178                    . '&amp;is_js_confirmed=0';
00179 
00180         // Only one table defined in an variable $onetable
00181         if (isset($onetable)) {
00182             // Displays search string
00183             echo '    ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
00184             echo '    <br />' . "\n";
00185 
00186             // Gets the SQL statements
00187             $newsearchsqls = PMA_getSearchSqls($onetable, $search_str, $search_option);
00188 
00189             // Executes the "COUNT" statement
00190             $local_query   = $newsearchsqls['select_count'];
00191             $res           = @PMA_mysql_query($local_query)  or PMA_mysqlDie('', $local_query, FALSE, $err_url);
00192             if ($res) {
00193                 $res_cnt   = PMA_mysql_result($res, 0, 'count');
00194                 mysql_free_result($res);
00195             } else {
00196                 $res_cnt   = 0;
00197             } // end if... else ...
00198             $num_search_result_total = $res_cnt;
00199 
00200             echo '    <!-- Search results in table ' . $onetable . ' (' . $res_cnt . ') -->' . "\n"
00201                  . '    <br />' . "\n"
00202                  . '    <table><tr><td>' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($onetable)) . "</td>\n";
00203 
00204             if ($res_cnt > 0) {
00205                    echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
00206                     . '&amp;sql_query=' .urlencode($newsearchsqls['select_fields']),
00207                     $strBrowse, '') .  "</td>\n";
00208 
00209                    echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
00210                     . '&amp;sql_query=' .urlencode($newsearchsqls['delete']),
00211                     $strDelete, $newsearchsqls['delete']) .  "</td>\n";
00212 
00213             } // end if
00214             echo '</tr></table>' . "\n";
00215         } // end only one table
00216 
00217         // Several tables defined in the array $table_select
00218         else if (isset($table_select)) {
00219             // Displays search string
00220             echo '    ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
00221             echo '    <ul>' . "\n";
00222 
00223             $num_search_result_total = 0;
00224             for ($i = 0; $i < $num_selectedtables; $i++) {
00225                 // Gets the SQL statements
00226                 $newsearchsqls = PMA_getSearchSqls($table_select[$i], $search_str, $search_option);
00227 
00228                 // Executes the "COUNT" statement
00229                 $local_query   = $newsearchsqls['select_count'];
00230                 $res           = @PMA_mysql_query($local_query)  or PMA_mysqlDie('', $local_query, FALSE, $err_url);
00231                 if ($res) {
00232                     $res_cnt   = PMA_mysql_result($res, 0, 'count');
00233                     mysql_free_result($res);
00234                 } else {
00235                     $res_cnt   = 0;
00236                 } // end if... else ...
00237                 $num_search_result_total += $res_cnt;
00238 
00239                 echo '        <!-- Search results in table ' . $table_select[$i] . ' (' . $res_cnt . ') -->' . "\n"
00240                      . '        <li>' . "\n"
00241                      . '            <table><tr><td>' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($table_select[$i])) . "</td>\n";
00242 
00243                 if ($res_cnt > 0) {
00244                    echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
00245                     . '&amp;sql_query=' .urlencode($newsearchsqls['select_fields']),
00246                     $strBrowse, '') .  "</td>\n";
00247 
00248                    echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
00249                     . '&amp;sql_query=' .urlencode($newsearchsqls['delete']),
00250                     $strDelete, $newsearchsqls['delete']) .  "</td>\n";
00251 
00252                 } // end if
00253 
00254                 echo '        </tr></table></li>' . "\n";
00255             } // end for
00256 
00257             echo '    </ul>' . "\n";
00258             echo '    <p>' . sprintf($strNumSearchResultsTotal, $num_search_result_total) . '</p>' . "\n";
00259         } // end several tables
00260 
00261         echo "\n";
00262         ?>
00263 <hr width="100%">
00264         <?php
00265     } // end if (!empty($search_str) && !empty($search_option))
00266 
00267 } // end 1.
00268 
00269 
00273 echo "\n";
00274 $searched          = (isset($original_search_str))
00275                    ? htmlspecialchars($original_search_str)
00276                    : '';
00277 if (empty($search_option)) {
00278     $search_option = 1;
00279 }
00280 ?>
00281 <!-- Display search form -->
00282 <p align="center">
00283     <b><?php echo $strSearchFormTitle; ?></b>
00284 </p>
00285 
00286 <a name="db_search"></a>
00287 <form method="post" action="db_search.php" name="db_search">
00288     <?php echo PMA_generate_common_hidden_inputs($db); ?>
00289 
00290     <table>
00291     <tr>
00292         <td>
00293             <?php echo $strSearchNeedle; ?>&nbsp;
00294         </td>
00295         <td>
00296             <input type="text" name="search_str" size="30" value="<?php echo $searched; ?>" />
00297         </td>
00298     </tr>
00299     <tr>
00300         <td colspan="2">&nbsp;</td>
00301     </tr>
00302     <tr>
00303         <td valign="top">
00304             <?php echo $strSearchType; ?>&nbsp;
00305         </td>
00306         <td>
00307             <input type="radio" id="search_option_1" name="search_option" value="1"<?php if ($search_option == 1) echo ' checked="checked"'; ?> />
00308             <label for="search_option_1"><?php echo $strSearchOption1; ?></label>&nbsp;*<br />
00309             <input type="radio" id="search_option_2" name="search_option" value="2"<?php if ($search_option == 2) echo ' checked="checked"'; ?> />
00310             <label for="search_option_2"><?php echo $strSearchOption2; ?></label>&nbsp;*<br />
00311             <input type="radio" id="search_option_3" name="search_option" value="3"<?php if ($search_option == 3) echo ' checked="checked"'; ?> />
00312             <label for="search_option_3"><?php echo $strSearchOption3; ?></label><br />
00313             <input type="radio" id="search_option_4" name="search_option" value="4"<?php if ($search_option == 4) echo ' checked="checked"'; ?> />
00314             <label for="search_option_4"><?php echo $strSearchOption4 . '</label> ' . PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
00315             <br />
00316             *&nbsp;<?php echo $strSplitWordsWithSpace . "\n"; ?>
00317         </td>
00318     </tr>
00319     <tr>
00320         <td colspan="2">&nbsp;</td>
00321     </tr>
00322     <tr>
00323         <td valign="top">
00324             <?php echo $strSearchInTables; ?>&nbsp;
00325         </td>
00326         <td>
00327 <?php
00328 if ($num_tables > 1) {
00329     $i = 0;
00330 
00331     echo '            <select name="table_select[]" size="6"  multiple="multiple">' . "\n";
00332     while ($i < $num_tables) {
00333         if (!empty($unselectall)) {
00334             $is_selected = '';
00335         }
00336         else if ((isset($table_select) && PMA_isInto($tables[$i], $table_select) != -1)
00337                 || (!empty($selectall))
00338                 || (isset($onetable) && $onetable == $tables[$i])) {
00339             $is_selected = ' selected="selected"';
00340         }
00341         else {
00342             $is_selected = '';
00343         }
00344 
00345         echo '                <option value="' . htmlspecialchars($tables[$i]) . '"' . $is_selected . '>' . htmlspecialchars($tables[$i]) . '</option>' . "\n";
00346         $i++;
00347     } // end while
00348     echo '            </select>' . "\n";
00349     ?>
00350             <br />
00351             <a href="db_search.php?<?php echo $url_query; ?>&amp;selectall=1#db_search" onclick="setSelectOptions('db_search', 'table_select[]', true); return false;"><?php echo $strSelectAll; ?></a>
00352             &nbsp;/&nbsp;
00353             <a href="db_search.php?<?php echo $url_query; ?>&amp;unselectall=1#db_search" onclick="setSelectOptions('db_search', 'table_select[]', false); return false;"><?php echo $strUnselectAll; ?></a>
00354     <?php
00355 }
00356 else {
00357     echo "\n";
00358     echo '            ' . htmlspecialchars($tables[0]) . "\n";
00359     echo '            <input type="hidden" name="table" value="' . htmlspecialchars($tables[0]) . '" />' . "\n";
00360 } // end if... else...
00361 
00362 echo"\n";
00363 ?>
00364         </td>
00365     </tr>
00366 
00367     <tr>
00368         <td colspan="2">&nbsp;</td>
00369     </tr>
00370     <tr>
00371         <td colspan="2"><input type="submit" name="submit_search" value="<?php echo $strGo; ?>" /></td>
00372     </tr>
00373     </table>
00374 </form>
00375 
00376 
00377 <?php
00381 echo "\n";
00382 require_once('./footer.inc.php');
00383 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6