home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / tbl_select.php < prev    next >
Encoding:
PHP Script  |  2003-12-29  |  11.8 KB  |  297 lines

  1. <?php
  2. /* $Id: tbl_select.php,v 2.3 2003/12/30 12:22:51 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.  
  6. /**
  7.  * Gets some core libraries
  8.  */
  9. require_once('./libraries/grab_globals.lib.php');
  10. require_once('./libraries/common.lib.php');
  11. require_once('./libraries/relation.lib.php'); // foreign keys
  12.  
  13. if ($cfg['PropertiesIconic'] == true) {
  14.     // We need to copy the value or else the == 'both' check will always return true
  15.     $propicon = (string)$cfg['PropertiesIconic'];
  16.  
  17.     if ($propicon == 'both') {
  18.         $iconic_spacer = '<nobr>';
  19.     } else {
  20.         $iconic_spacer = '';
  21.     }
  22.  
  23.     $titles['Browse']     = $iconic_spacer . '<img width="12" height="13" src="images/button_browse.png" alt="' . $strBrowseForeignValues . '" title="' . $strBrowseForeignValues . '" border="0" />';
  24.  
  25.     if ($propicon == 'both') {
  26.         $titles['Browse']        .= ' ' . $strBrowseForeignValues . '</nobr>';
  27.     }
  28. } else {
  29.     $titles['Browse']        = $strBrowseForeignValues;
  30. }
  31.  
  32. /**
  33.  * Defines arrays of functions (should possibly be in config.inc.php
  34.  * so it can also be used in tbl_qbe.php)
  35.  *
  36.  * LIKE works also on integers and dates so I added it in numfunctions
  37.  */
  38. $numfunctions  = array('=', '>', '>=', '<', '<=', '!=', 'LIKE');
  39. $textfunctions = array('LIKE', '=', '!=');
  40.  
  41. /**
  42.  * Not selection yet required -> displays the selection form
  43.  */
  44. if (!isset($param) || $param[0] == '') {
  45.     // Gets some core libraries
  46.     require('./tbl_properties_common.php');
  47.     //$err_url   = 'tbl_select.php' . $err_url;
  48.     $url_query .= '&goto=tbl_select.php&back=tbl_select.php';
  49.     require('./tbl_properties_table_info.php');
  50.  
  51.     if (!isset($goto)) {
  52.         $goto = $cfg['DefaultTabTable'];
  53.     }
  54.     // Defines the url to return to in case of error in the next sql statement
  55.     $err_url   = $goto . '?' . PMA_generate_common_url($db, $table);
  56.  
  57.     // Gets the list and number of fields
  58.     $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db);
  59.     $result      = @PMA_mysql_query($local_query);
  60.     if (!$result) {
  61.         PMA_mysqlDie('', $local_query, '', $err_url);
  62.     }
  63.     else {
  64.         $fields_cnt        = mysql_num_rows($result);
  65.         while ($row = PMA_mysql_fetch_array($result)) {
  66.             $fields_list[] = $row['Field'];
  67.             $type          = $row['Type'];
  68.             // reformat mysql query output - staybyte - 9. June 2001
  69.             $shorttype     = substr($type, 0, 3);
  70.             if ($shorttype == 'set' || $shorttype == 'enu') {
  71.                 $type      = str_replace(',', ', ', $type);
  72.             } else {
  73.                 $type          = preg_replace('@BINARY@i', '', $type);
  74.                 $type          = preg_replace('@ZEROFILL@i', '', $type);
  75.                 $type          = preg_replace('@UNSIGNED@i', '', $type);
  76.             }
  77.             if (empty($type)) {
  78.                 $type      = ' ';
  79.             }
  80.             $fields_type[] = $type;
  81.         } // end while
  82.         mysql_free_result($result);
  83.  
  84.         // <markus@noga.de>
  85.         // retrieve keys into foreign fields, if any
  86.         $cfgRelation = PMA_getRelationsParam();
  87.         // check also foreigners even if relwork is FALSE (to get
  88.         // foreign keys from innodb)
  89.         //$foreigners  = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
  90.         $foreigners  = PMA_getForeigners($db, $table);
  91.         ?>
  92. <form method="post" action="tbl_select.php" name="insertForm">
  93.     <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
  94.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  95.     <input type="hidden" name="back" value="tbl_select.php" />
  96.          
  97.     <?php echo $strSelectFields; ?> :<br />
  98.          
  99.     <select name="param[]" size="<?php echo ($fields_cnt < 10) ? $fields_cnt : 10; ?>" multiple="multiple">
  100.         <?php
  101.         echo "\n";
  102.         // Displays the list of the fields
  103.         for ($i = 0 ; $i < $fields_cnt; $i++) {
  104.             echo '        <option value="' . htmlspecialchars($fields_list[$i]) . '" selected="selected">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
  105.         }
  106.         ?>
  107.     </select><br />
  108.     <ul>
  109.         <li>
  110.             <div style="margin-bottom: 10px">
  111.             <?php echo $strLimitNumRows . "\n"; ?>
  112.             <input type="text" size="4" name="session_max_rows" value="<?php echo $cfg['MaxRows']; ?>" class="textfield" />
  113.             </div>
  114.         </li>
  115.         <li>
  116.             <?php echo $strAddSearchConditions; ?><br />
  117.             <input type="text" name="where" class="textfield" /> 
  118.             <?php echo PMA_showMySQLDocu('Reference', 'Functions') . "\n"; ?>
  119.             <br /><br />
  120.             <?php echo '<i>' . $strOr . '</i> ' . $strDoAQuery; ?><br />
  121.             <table border="<?php echo $cfg['Border']; ?>">
  122.             <tr>
  123.                 <th><?php echo $strField; ?></th>
  124.                 <th><?php echo $strType; ?></th>
  125.                 <th><?php echo $strFunction; ?></th>
  126.                 <th><?php echo $strValue; ?></th>
  127.             </tr>
  128.         <?php
  129.         for ($i = 0; $i < $fields_cnt; $i++) {
  130.             echo "\n";
  131.             $bgcolor   = ($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
  132.             ?>
  133.             <tr>
  134.                 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo htmlspecialchars($fields_list[$i]); ?></td>
  135.                 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo $fields_type[$i]; ?></td>
  136.                 <td bgcolor="<?php echo $bgcolor; ?>">
  137.                     <select name="func[]">
  138.             <?php
  139.             if (preg_match('@char|blob|text|set|enum@i', $fields_type[$i])) {
  140.                 foreach($textfunctions AS $k => $fc) {
  141.                     echo "\n" . '                        '
  142.                          . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
  143.                 } // end while
  144.             } else {
  145.                 foreach($numfunctions AS $k => $fc) {
  146.                     echo "\n" . '                        '
  147.                          . '<option value="' .  htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
  148.                 } // end while
  149.             } // end if... else...
  150.             echo "\n";
  151.             ?>
  152.                     </select>
  153.                 </td>
  154.                 <td bgcolor="<?php echo $bgcolor; ?>">
  155.             <?php
  156.             // <markus@noga.de>
  157.             $field = $fields_list[$i];
  158.  
  159.             // do not use require_once here
  160.             require('./libraries/get_foreign.lib.php');
  161.  
  162.             echo "\n";
  163.             // we got a bug report: in some cases, even if $disp is true,
  164.             // there are no rows, so we add a fetch_array
  165.  
  166.             if ($foreigners && isset($foreigners[$field]) && isset($disp) && $disp && @PMA_mysql_fetch_array($disp)) {
  167.                 // f o r e i g n    k e y s
  168.                 echo '                    <select name="fields[]">' . "\n";
  169.                 // go back to first row
  170.                 mysql_data_seek($disp,0);
  171.                 echo PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, 100);
  172.                 echo '                    </select>' . "\n";
  173.             } else if (isset($foreign_link) && $foreign_link == true) {
  174.             ?>
  175.             <input type="text"   name="fields[]" id="field_<?php echo md5($field); ?>[]" class="textfield" />
  176.             <script type="text/javascript" language="javascript">
  177.                 document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&field=<?php echo urlencode($field); ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
  178.             </script>
  179.             <?php
  180.             } else if (substr($fields_type[$i], 0, 3)=='enu'){
  181.                 // e n u m s
  182.                 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
  183.                 echo '                    <select name="fields[]">' . "\n";
  184.                 echo '                        <option value=""></option>' . "\n";
  185.                 $cnt_enum_value = count($enum_value);
  186.                 for ($j=0; $j<$cnt_enum_value;$j++){
  187.                     echo '                        <option value="' . $enum_value[$j] . '">' . $enum_value[$j] . '</option>';
  188.                 } // end for
  189.                 echo '                    </select>' . "\n";
  190.             } else {
  191.                 // o t h e r   c a s e s
  192.                 echo '                    <input type="text" name="fields[]" size="40" class="textfield" />' .  "\n";
  193.             }
  194.  
  195.             ?>
  196.                     <input type="hidden" name="names[]" value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
  197.                     <input type="hidden" name="types[]" value="<?php echo $fields_type[$i]; ?>" />
  198.                 </td>
  199.             </tr>
  200.             <?php
  201.         } // end for
  202.         echo "\n";
  203.         ?>
  204.             </table><br />
  205.         </li>
  206.         <li>
  207.             <?php echo $strDisplayOrder; ?><br />
  208.             <select name="orderField" style="vertical-align: middle">
  209.                 <option value="--nil--"></option>
  210.         <?php
  211.         echo "\n";
  212.         for ($i = 0; $i < $fields_cnt; $i++) {
  213.             echo '                ';
  214.             echo '<option value="' . htmlspecialchars($fields_list[$i]) . '">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
  215.         } // end for
  216.         ?>
  217.             </select>
  218.             <input type="radio" name="order" value="ASC" checked="checked" />
  219.             <?php echo $strAscending; ?> 
  220.             <input type="radio" name="order" value="DESC" />
  221.             <?php echo $strDescending; ?><br /><br />
  222.         </li>
  223.     </ul>
  224.  
  225.         
  226.     <input type="hidden" name="max_number_of_fields" value="<?php echo $fields_cnt; ?>" />
  227.     <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
  228. </form>
  229.         <?php
  230.     } // end if
  231.     require_once('./footer.inc.php');
  232. }
  233.  
  234.  
  235. /**
  236.  * Selection criteria have been submitted -> do the work
  237.  */
  238. else {
  239.     // Builds the query
  240.  
  241.     $sql_query = 'SELECT ';
  242.  
  243.     // if all fields were selected to display, we do a SELECT *
  244.     // (more efficient and this helps prevent a problem in IE
  245.     // if one of the rows is edited and we come back to the Select results)
  246.  
  247.     if (count($param) == $max_number_of_fields) {
  248.         $sql_query .= '* ';
  249.     } else {
  250.  
  251.         $sql_query .= PMA_backquote(urldecode($param[0]));
  252.         $i         = 0;
  253.         $c         = count($param);
  254.         while ($i < $c) {
  255.             if ($i > 0) {
  256.                 $sql_query .= ',' . PMA_backquote(urldecode($param[$i]));
  257.             }
  258.             $i++;
  259.         }
  260.     } // end if
  261.  
  262.     $sql_query .= ' FROM ' . PMA_backquote($table);
  263.     // The where clause
  264.     if ($where != '') {
  265.         $sql_query .= ' WHERE ' . $where;
  266.     }
  267.     else {
  268.         $sql_query .= ' WHERE 1';
  269.         $cnt_fields = count($fields);
  270.         for ($i = 0; $i < $cnt_fields; $i++) {
  271.             if (!empty($fields) && $fields[$i] != '') {
  272.                 if (preg_match('@char|blob|text|set|enum|date|time|year@i', $types[$i])) {
  273.                     $quot     = '\'';
  274.                 } else {
  275.                     $quot     = '';
  276.                 }
  277.                 if (strtoupper($fields[$i]) == 'NULL' || strtoupper($fields[$i]) == 'NOT NULL') {
  278.                     $quot     = '';
  279.                     $func[$i] = 'IS';
  280.                 }
  281.                 //$sql_query    .= ' AND ' . PMA_backquote(urldecode($names[$i])) . " $func[$i] $quot$fields[$i]$quot";
  282.  
  283.                 $sql_query    .= ' AND ' . PMA_backquote(urldecode($names[$i])) . ' ' . $func[$i] . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;
  284.  
  285.             } // end if
  286.         } // end for
  287.     } // end if
  288.  
  289.     if ($orderField != '--nil--') {
  290.         $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
  291.     } // end if
  292.  
  293.     include('./sql.php');
  294. }
  295.  
  296. ?>
  297.