home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / db_details_qbe.php < prev    next >
Encoding:
PHP Script  |  2003-12-25  |  36.5 KB  |  1,086 lines

  1. <?php
  2. /* $Id: db_details_qbe.php,v 2.4 2003/12/25 20:50:54 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.  
  6. /**
  7.  * Get the values of the variables posted or sent to this script and display
  8.  * the headers
  9.  */
  10. require_once('./libraries/grab_globals.lib.php');
  11. require_once('./libraries/common.lib.php');
  12. require_once('./libraries/relation.lib.php');
  13.  
  14.  
  15. /**
  16.  * Gets the relation settings
  17.  */
  18. $cfgRelation = PMA_getRelationsParam();
  19.  
  20.  
  21. /**
  22.  * A query has been submitted -> execute it, else display the headers
  23.  */
  24. if (isset($submit_sql) && preg_match('@^SELECT@i', $encoded_sql_query)) {
  25.     $goto      = 'db_details.php';
  26.     $zero_rows = htmlspecialchars($strSuccess);
  27.     $sql_query = urldecode($encoded_sql_query);
  28.     require('./sql.php');
  29.     exit();
  30. } else {
  31.     $sub_part  = '_qbe';
  32.     require('./db_details_common.php');
  33.     $url_query .= '&goto=db_details_qbe.php';
  34.     require('./db_details_db_info.php');
  35. }
  36.  
  37. if (isset($submit_sql) && !preg_match('@^SELECT@i', $encoded_sql_query)) {
  38.     echo '<p class="warning">' . $strHaveToShow . '</p>';
  39. }
  40.  
  41.  
  42. /**
  43.  * Initialize some variables
  44.  */
  45. if (empty($Columns)) {
  46.     $Columns  = 3;  // Initial number of columns
  47. }
  48. if (!isset($Add_Col)) {
  49.     $Add_Col  = '';
  50. }
  51. if (!isset($Add_Row)) {
  52.     $Add_Row  = '';
  53. }
  54. if (!isset($Rows)) {
  55.     $Rows     = '';
  56. }
  57. if (!isset($InsCol)) {
  58.     $InsCol   = '';
  59. }
  60. if (!isset($DelCol)) {
  61.     $DelCol   = '';
  62. }
  63. if (!isset($prev_Criteria)) {
  64.     $prev_Criteria = '';
  65. }
  66. // workaround for a PHP3 problem
  67. if (!isset($Criteria)) {
  68.     //$Criteria = '';
  69.     $Criteria = array();
  70.     for ($i = 0; $i < $Columns; $i++) {
  71.         $Criteria[$i] = '';
  72.     }
  73. }
  74. if (!isset($InsRow)) {
  75. //    $InsRow   = '';
  76.     $InsRow = array();
  77.     for ($i = 0; $i < $Columns; $i++) {
  78.         $InsRow[$i] = '';
  79.     }
  80. }
  81. if (!isset($DelRow)) {
  82. //    $DelRow   = '';
  83.     $DelRow = array();
  84.     for ($i = 0; $i < $Columns; $i++) {
  85.         $DelRow[$i] = '';
  86.     }
  87. }
  88. if (!isset($AndOrRow)) {
  89. //    $AndOrRow = '';
  90.     $AndOrRow = array();
  91.     for ($i = 0; $i < $Columns; $i++) {
  92.         $AndOrRow[$i] = '';
  93.     }
  94. }
  95. if (!isset($AndOrCol)) {
  96. //    $AndOrCol = '';
  97.     $AndOrCol = array();
  98.     for ($i = 0; $i < $Columns; $i++) {
  99.         $AndOrCol[$i] = '';
  100.     }
  101. }
  102. // minimum width
  103. $wid          = 12;
  104. $col          = $Columns + $Add_Col;
  105. if ($col < 0) {
  106.     $col      = 0;
  107. }
  108. $row          = $Rows + $Add_Row;
  109. if ($row < 0) {
  110.     $row      = 0;
  111. }
  112.  
  113.  
  114. /**
  115.  * Prepares the form
  116.  */
  117. $tbl_result     = PMA_mysql_list_tables($db);
  118. $tbl_result_cnt = mysql_num_rows($tbl_result);
  119. $i              = 0;
  120. $k              = 0;
  121.  
  122. // The tables list sent by a previously submitted form
  123. if (!empty($TableList)) {
  124.     $cnt_table_list = count($TableList);
  125.     for ($x = 0; $x < $cnt_table_list; $x++) {
  126.         $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
  127.     }
  128. } // end if
  129.  
  130. // The tables list gets from MySQL
  131. while ($i < $tbl_result_cnt) {
  132.     $tbl             = PMA_mysql_tablename($tbl_result, $i);
  133.     $fld_results     = @PMA_mysql_list_fields_alternate($db, $tbl) or PMA_mysqlDie(PMA_mysql_error(), 'PMA_mysql_list_fields_alternate(' . $db . ', ' . $tbl . ')', FALSE, $err_url);
  134.     $fld_results_cnt = ($fld_results) ? count($fld_results) : 0;
  135.     $j               = 0;
  136.  
  137.     if (empty($tbl_names[$tbl]) && !empty($TableList)) {
  138.         $tbl_names[$tbl] = '';
  139.     } else {
  140.         $tbl_names[$tbl] = ' selected="selected"';
  141.     } //  end if
  142.  
  143.     // The fields list per selected tables
  144.     if ($tbl_names[$tbl] == ' selected="selected"') {
  145.         $fld[$k++]   =  PMA_backquote($tbl) . '.*';
  146.         while ($j < $fld_results_cnt) {
  147.             $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
  148.             $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
  149.  
  150.             // increase the width if necessary
  151.             if (strlen($fld[$k]) > $wid) {
  152.                 $wid = strlen($fld[$k]);
  153.             } //end if
  154.  
  155.             $k++;
  156.             $j++;
  157.         } // end while
  158.     } // end if
  159.  
  160.     $i++;
  161. } // end if
  162. mysql_free_result($tbl_result);
  163.  
  164. // largest width found
  165. $realwidth = $wid . 'ex';
  166.  
  167.  
  168. /**
  169.  * Displays the form
  170.  */
  171. ?>
  172.  
  173. <!-- Query by example form -->
  174. <form action="db_details_qbe.php" method="post">
  175.     <table border="<?php echo $cfg['Border']; ?>">
  176.  
  177.     <!-- Fields row -->
  178.     <tr>
  179.         <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
  180.             <b><?php echo $strField; ?> : </b>
  181.         </td>
  182. <?php
  183. $z = 0;
  184. for ($x = 0; $x < $col; $x++) {
  185.     if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
  186.         ?>
  187.         <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  188.             <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
  189.                 <option value=""></option>
  190.         <?php
  191.         echo "\n";
  192.         for ($y = 0; $y < sizeof($fld); $y++) {
  193.             if ($fld[$y] == '') {
  194.                 $sel = ' selected="selected"';
  195.             } else {
  196.                 $sel = '';
  197.             }
  198.             echo '                ';
  199.             echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
  200.         } // end for
  201.         ?>
  202.             </select>
  203.         </td>
  204.         <?php
  205.         $z++;
  206.     } // end if
  207.     echo "\n";
  208.  
  209.     if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
  210.         continue;
  211.     }
  212.     ?>
  213.         <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  214.             <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
  215.                 <option value=""></option>
  216.     <?php
  217.     echo "\n";
  218.     for ($y = 0; $y < sizeof($fld); $y++) {
  219.         if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
  220.             $curField[$z] = urldecode($Field[$x]);
  221.             $sel          = ' selected="selected"';
  222.         } else {
  223.             $sel          = '';
  224.         } // end if
  225.         echo '                ';
  226.         echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
  227.     } // end for
  228.     ?>
  229.             </select>
  230.         </td>
  231.     <?php
  232.     $z++;
  233.     echo "\n";
  234. } // end for
  235. ?>
  236.     </tr>
  237.  
  238.     <!-- Sort row -->
  239.     <tr>
  240.         <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
  241.             <b><?php echo $strSort; ?> : </b>
  242.         </td>
  243. <?php
  244. $z = 0;
  245. for ($x = 0; $x < $col; $x++) {
  246.     if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
  247.         ?>
  248.         <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
  249.             <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
  250.                 <option value=""></option>
  251.                 <option value="ASC"><?php echo $strAscending; ?></option>
  252.                 <option value="DESC"><?php echo $strDescending; ?></option>
  253.             </select>
  254.         </td>
  255.         <?php
  256.         $z++;
  257.     } // end if
  258.     echo "\n";
  259.  
  260.     if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
  261.         continue;
  262.     }
  263.     ?>
  264.         <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
  265.             <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
  266.                 <option value=""></option>
  267.     <?php
  268.     echo "\n";
  269.  
  270.     // If they have chosen all fields using the * selector,
  271.     // then sorting is not available
  272.     // Robbat2 - Fix for Bug #570698
  273.     if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
  274.         $Sort[$x] = '';
  275.     } //end if
  276.  
  277.     if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
  278.         $curSort[$z] = $Sort[$x];
  279.         $sel         = ' selected="selected"';
  280.     } else {
  281.         $sel         = '';
  282.     } // end if
  283.     echo '                ';
  284.     echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
  285.     if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
  286.         $curSort[$z] = $Sort[$x];
  287.         $sel         = ' selected="selected"';
  288.     } else {
  289.         $sel         = '';
  290.     } // end if
  291.     echo '                ';
  292.     echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
  293.     ?>
  294.             </select>
  295.         </td>
  296.     <?php
  297.     $z++;
  298.     echo "\n";
  299. } // end for
  300. ?>
  301.     </tr>
  302.  
  303.     <!-- Show row -->
  304.     <tr>
  305.         <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
  306.             <b><?php echo $strShow; ?> : </b>
  307.         </td>
  308. <?php
  309. $z = 0;
  310. for ($x = 0; $x < $col; $x++) {
  311.     if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
  312.         ?>
  313.         <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  314.             <input type="checkbox" name="Show[<?php echo $z; ?>]" />
  315.         </td>
  316.         <?php
  317.         $z++;
  318.     } // end if
  319.     echo "\n";
  320.  
  321.     if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
  322.         continue;
  323.     }
  324.     if (isset($Show[$x])) {
  325.         $checked     = ' checked="checked"';
  326.         $curShow[$z] = $Show[$x];
  327.     } else {
  328.         $checked     =  '';
  329.     }
  330.     ?>
  331.         <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  332.             <input type="checkbox" name="Show[<?php echo $z; ?>]"<?php echo $checked; ?> />
  333.         </td>
  334.     <?php
  335.     $z++;
  336.     echo "\n";
  337. } // end for
  338. ?>
  339.     </tr>
  340.  
  341.     <!-- Criteria row -->
  342.     <tr>
  343.         <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
  344.             <b><?php echo $strCriteria; ?> : </b>
  345.         </td>
  346. <?php
  347. $z = 0;
  348. for ($x = 0; $x < $col; $x++) {
  349.     if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
  350.         ?>
  351.         <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
  352.             <input type="text" name="Criteria[<?php echo $z; ?>]" value="" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
  353.         </td>
  354.         <?php
  355.         $z++;
  356.     } // end if
  357.     echo "\n";
  358.  
  359.     if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
  360.         continue;
  361.     }
  362.     if (isset($Criteria[$x])) {
  363.         $stripped_Criteria = $Criteria[$x];
  364.     }
  365.     if ((empty($prev_Criteria) || !isset($prev_Criteria[$x]))
  366.         || urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
  367.         $curCriteria[$z]   = $stripped_Criteria;
  368.         $encoded_Criteria  = urlencode($stripped_Criteria);
  369.     } else {
  370.         $curCriteria[$z]   = urldecode($prev_Criteria[$x]);
  371.         $encoded_Criteria  = $prev_Criteria[$x];
  372.     }
  373.     ?>
  374.         <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
  375.             <input type="hidden" name="prev_Criteria[<?php echo $z; ?>]" value="<?php echo $encoded_Criteria; ?>" />
  376.             <input type="text" name="Criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
  377.         </td>
  378.     <?php
  379.     $z++;
  380.     echo "\n";
  381. } // end for
  382. ?>
  383.     </tr>
  384.  
  385.     <!-- And/Or columns and rows -->
  386. <?php
  387. $w = 0;
  388. for ($y = 0; $y <= $row; $y++) {
  389.     $bgcolor = ($y % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
  390.     if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
  391.         $chk['or']  = ' checked="checked"';
  392.         $chk['and'] = '';
  393.         ?>
  394.     <tr>
  395.         <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">
  396.             <!-- Row controls -->
  397.             <table bgcolor="<?php echo $bgcolor; ?>">
  398.             <tr>
  399.                 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  400.                     <small><?php echo $strQBEIns; ?> :</small>
  401.                     <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
  402.                 </td>
  403.                 <td align="<?php echo $cell_align_right; ?>">
  404.                     <b><?php echo $strAnd; ?> :</b>
  405.                 </td>
  406.                 <td>
  407.                     <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
  408.                      
  409.                 </td>
  410.             </tr>
  411.             <tr>
  412.                 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  413.                     <small><?php echo $strQBEDel; ?> :</small>
  414.                     <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
  415.                 </td>
  416.                 <td align="<?php echo $cell_align_right; ?>">
  417.                     <b><?php echo $strOr; ?> :</b>
  418.                 </td>
  419.                 <td>
  420.                     <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
  421.                      
  422.                 </td>
  423.             </tr>
  424.             </table>
  425.         </td>
  426.         <?php
  427.         $z = 0;
  428.         for ($x = 0; $x < $col; $x++) {
  429.             if ($InsCol[$x] == 'on') {
  430.                 echo "\n";
  431.                 $or = 'Or' . $w . '[' . $z . ']';
  432.                 ?>
  433.         <td align="center" bgcolor="<?php echo $bgcolor; ?>">
  434.             <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
  435.         </td>
  436.                 <?php
  437.                 $z++;
  438.             } // end if
  439.             if ($DelCol[$x] == 'on') {
  440.                 continue;
  441.             }
  442.  
  443.             echo "\n";
  444.             $or = 'Or' . $w . '[' . $z . ']';
  445.             ?>
  446.         <td align="center" bgcolor="<?php echo $bgcolor; ?>">
  447.             <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
  448.         </td>
  449.             <?php
  450.             $z++;
  451.         } // end for
  452.         $w++;
  453.         echo "\n";
  454.         ?>
  455.     </tr>
  456.         <?php
  457.     } // end if
  458.  
  459.     if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
  460.         continue;
  461.     }
  462.  
  463.     if (isset($AndOrRow[$y])) {
  464.         $curAndOrRow[$w] = $AndOrRow[$y];
  465.     }
  466.     if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
  467.         $chk['and'] =  ' checked="checked"';
  468.         $chk['or']  =  '';
  469.     } else {
  470.         $chk['or']  =  ' checked="checked"';
  471.         $chk['and'] =  '';
  472.     }
  473.     echo "\n";
  474.     ?>
  475.     <tr>
  476.         <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  477.             <!-- Row controls -->
  478.             <table bgcolor="<?php echo $bgcolor; ?>">
  479.             <tr>
  480.                 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  481.                     <small><?php echo $strQBEIns; ?> :</small>
  482.                     <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
  483.                 </td>
  484.                 <td align="<?php echo $cell_align_right; ?>">
  485.                     <b><?php echo $strAnd; ?> :</b>
  486.                 </td>
  487.                 <td>
  488.                     <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
  489.                 </td>
  490.             </tr>
  491.             <tr>
  492.                 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  493.                     <small><?php echo $strQBEDel; ?> :</small>
  494.                     <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
  495.                 </td>
  496.                 <td align="<?php echo $cell_align_right; ?>">
  497.                     <b><?php echo $strOr; ?> :</b>
  498.                 </td>
  499.                 <td>
  500.                     <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
  501.                 </td>
  502.             </tr>
  503.             </table>
  504.         </td>
  505.     <?php
  506.     $z = 0;
  507.     for ($x = 0; $x < $col; $x++) {
  508.         if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
  509.             echo "\n";
  510.             $or = 'Or' . $w . '[' . $z . ']';
  511.             ?>
  512.         <td align="center" bgcolor="<?php echo $bgcolor; ?>">
  513.             <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
  514.         </td>
  515.             <?php
  516.             $z++;
  517.         } // end if
  518.         if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
  519.             continue;
  520.         }
  521.  
  522.         echo "\n";
  523.         $or = 'Or' . $y;
  524.         if (!isset(${$or})) {
  525.             ${$or} = '';
  526.         }
  527.         if (!empty(${$or}) && isset(${$or}[$x])) {
  528.             $stripped_or = ${$or}[$x];
  529.         } else {
  530.             $stripped_or     = '';
  531.         }
  532.         ?>
  533.         <td align="center" bgcolor="<?php echo $bgcolor; ?>">
  534.             <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="Or<?php echo $w . '[' . $z . ']'; ?>" dir="<?php echo $text_dir; ?>"><?php echo htmlspecialchars($stripped_or); ?></textarea>
  535.         </td>
  536.         <?php
  537.         if (!empty(${$or}) && isset(${$or}[$x])) {
  538.             ${'cur' . $or}[$z] = ${$or}[$x];
  539.         }
  540.         $z++;
  541.     } // end for
  542.     $w++;
  543.     echo "\n";
  544.     ?>
  545.     </tr>
  546.     <?php
  547.     echo "\n";
  548. } // end for
  549. ?>
  550.  
  551.     <!-- Modify columns -->
  552.     <tr>
  553.         <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
  554.             <b><?php echo $strModify; ?> : </b>
  555.         </td>
  556. <?php
  557. $z = 0;
  558. for ($x = 0; $x < $col; $x++) {
  559.     if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
  560.         $curAndOrCol[$z] = $AndOrCol[$y];
  561.         if ($AndOrCol[$z] == 'or') {
  562.             $chk['or']  = ' checked="checked"';
  563.             $chk['and'] = '';
  564.         } else {
  565.             $chk['and'] = ' checked="checked"';
  566.             $chk['or']  = '';
  567.         }
  568.         ?>
  569.         <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
  570.             <b><?php echo $strOr; ?> :</b>
  571.             <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
  572.               <b><?php echo $strAnd; ?> :</b>
  573.             <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
  574.             <br />
  575.             <?php echo $strQBEIns . "\n"; ?>
  576.             <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
  577.               <?php echo $strQBEDel . "\n"; ?>
  578.             <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
  579.         </td>
  580.         <?php
  581.         $z++;
  582.     } // end if
  583.     echo "\n";
  584.  
  585.     if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
  586.         continue;
  587.     }
  588.  
  589.     if (isset($AndOrCol[$y])) {
  590.         $curAndOrCol[$z] = $AndOrCol[$y];
  591.     }
  592.     if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
  593.         $chk['or']  = ' checked="checked"';
  594.         $chk['and'] = '';
  595.     } else {
  596.         $chk['and'] = ' checked="checked"';
  597.         $chk['or']  = '';
  598.     }
  599.     ?>
  600.         <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
  601.             <b><?php echo $strOr; ?> :</b>
  602.             <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
  603.               <b><?php echo $strAnd; ?> :</b>
  604.             <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
  605.             <br />
  606.             <?php echo $strQBEIns . "\n"; ?>
  607.             <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
  608.               <?php echo $strQBEDel . "\n"; ?>
  609.             <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
  610.         </td>
  611.     <?php
  612.     $z++;
  613.     echo "\n";
  614. } // end for
  615. ?>
  616.     </tr>
  617.     </table>
  618.  
  619.  
  620.     <!-- Other controls -->
  621.     <table border="0">
  622.     <tr>
  623.         <td valign="top">
  624.             <table border="0" align="<?php echo $cell_align_left; ?>">
  625.             <tr>
  626.                 <td rowspan="4" valign="top">
  627.                     <?php echo $strUseTables; ?> :
  628.                     <br />
  629.                     <select name="TableList[]" size="7" multiple="multiple">
  630. <?php
  631. foreach($tbl_names AS $key => $val) {
  632.     echo '                        ';
  633.     echo '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
  634. }
  635. ?>
  636.                     </select>
  637.                 </td>
  638.                 <td align="<?php echo $cell_align_right; ?>" valign="bottom">
  639.                     <input type="hidden" value="<?php echo htmlspecialchars($db); ?>" name="db" />
  640.                     <input type="hidden" value="<?php echo $z; ?>" name="Columns" />
  641. <?php
  642. $w--;
  643. ?>
  644.                     <input type="hidden" value="<?php echo $w; ?>" name="Rows" />
  645.                     <?php echo $strAddDeleteRow; ?> :
  646.                     <select size="1" name="Add_Row">
  647.                         <option value="-3">-3</option>
  648.                         <option value="-2">-2</option>
  649.                         <option value="-1">-1</option>
  650.                         <option value="0" selected="selected">0</option>
  651.                         <option value="1">1</option>
  652.                         <option value="2">2</option>
  653.                         <option value="3">3</option>
  654.                     </select>
  655.                 </td>
  656.             </tr>
  657.             <tr>
  658.                 <td align="<?php echo $cell_align_right; ?>" valign="bottom">
  659.                     <?php echo $strAddDeleteColumn; ?> :
  660.                     <select size="1" name="Add_Col">
  661.                         <option value="-3">-3</option>
  662.                         <option value="-2">-2</option>
  663.                         <option value="-1">-1</option>
  664.                         <option value="0" selected="selected">0</option>
  665.                         <option value="1">1</option>
  666.                         <option value="2">2</option>
  667.                         <option value="3">3</option>
  668.                     </select>
  669.                 </td>
  670.             </tr>
  671.             <!-- Generates a query -->
  672.             <tr align="center" valign="top">
  673.                 <td>
  674.                     <input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" />
  675.                     <?php echo PMA_generate_common_hidden_inputs(); ?>
  676.                 </td>
  677.             </tr>
  678.             <!-- Executes a query -->
  679.             <tr align="center" valign="top">
  680.                 <td>
  681.                     <input type="submit" name="submit_sql" value="<?php echo $strRunQuery; ?>" />
  682.                 </td>
  683.             </tr>
  684.             </table>
  685.         </td>
  686.         <td>
  687.             <!-- Displays the current query -->
  688.             <?php echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?><br />
  689. <textarea cols="30" rows="7" name="sql_query" dir="<?php echo $text_dir; ?>">
  690. <?php
  691. // 1. SELECT
  692. $last_select = 0;
  693. $encoded_qry = '';
  694. if (!isset($qry_select)) {
  695.     $qry_select         = '';
  696. }
  697. for ($x = 0; $x < $col; $x++) {
  698.     if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
  699.         if ($last_select) {
  700.             $qry_select .=  ', ';
  701.         }
  702.         $qry_select     .= $curField[$x];
  703.         $last_select    = 1;
  704.     }
  705. } // end for
  706. if (!empty($qry_select)) {
  707.     $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
  708.     echo  'SELECT ' . htmlspecialchars($qry_select) . "\n";
  709. }
  710.  
  711. // 2. FROM
  712.  
  713. // Create LEFT JOINS out of Relations
  714. // Code originally by Mike Beck <mike.beck@ibmiller.de>
  715. // If we can use Relations we could make some left joins.
  716. // First find out if relations are available in this database.
  717.  
  718. // First we need the really needed Tables - those in TableList might still be
  719. // all Tables.
  720. if (isset($Field) && count($Field) > 0) {
  721.  
  722.     // Initialize some variables
  723.     $tab_all    = array();
  724.     $col_all    = array();
  725.     $tab_wher   = array();
  726.     $tab_know   = array();
  727.     $tab_left   = array();
  728.     $col_where  = array();
  729.     $fromclause = '';
  730.  
  731.     // We only start this if we have fields, otherwise it would be dumb
  732.     foreach($Field AS $value) {
  733.         $parts             = explode('.', $value);
  734.         if (!empty($parts[0]) && !empty($parts[1])) {
  735.             $tab_raw       = urldecode($parts[0]);
  736.             $tab           = str_replace('`', '', $tab_raw);
  737.             $tab_all[$tab] = $tab;
  738.  
  739.             $col_raw       = urldecode($parts[1]);
  740.             $col_all[]     = $tab . '.' . str_replace('`', '', $col_raw);
  741.          }
  742.     } // end while
  743.  
  744.     // Check 'where' clauses
  745.     if ($cfgRelation['relwork'] && count($tab_all) > 0) {
  746.         // Now we need all tables that we have in the where clause
  747.         $crit_cnt         = count($Criteria);
  748.         for ($x = 0; $x < $crit_cnt; $x++) {
  749.             $curr_tab     = explode('.', urldecode($Field[$x]));
  750.             if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
  751.                 $tab_raw  = urldecode($curr_tab[0]);
  752.                 $tab      = str_replace('`', '', $tab_raw);
  753.  
  754.                 $col_raw  = urldecode($curr_tab[1]);
  755.                 $col1     = str_replace('`', '', $col_raw);
  756.                 $col1     = $tab . '.' . $col1;
  757.                 // Now we know that our array has the same numbers as $Criteria
  758.                 // we can check which of our columns has a where clause
  759.                 if (!empty($Criteria[$x])) {
  760.                     if (substr($Criteria[$x], 0, 1) == '=' || stristr($Criteria[$x], 'is')) {
  761.                         $col_where[$col] = $col1;
  762.                         $tab_wher[$tab]  = $tab;
  763.                     }
  764.                 } // end if
  765.             } // end if
  766.         } // end for
  767.  
  768.         // Cleans temp vars w/o further use
  769.         unset($tab_raw);
  770.         unset($col_raw);
  771.         unset($col1);
  772.  
  773.         if (count($tab_wher) == 1) {
  774.             // If there is exactly one column that has a decent where-clause
  775.             // we will just use this
  776.             $master = key($tab_wher);
  777.         } else {
  778.             // Now let's find out which of the tables has an index
  779.             // ( When the control user is the same as the normal user
  780.             // because he is using one of his databases as pmadb,
  781.             // the last db selected is not always the one where we need to work)
  782.             PMA_mysql_select_db($db);
  783.  
  784.             foreach($tab_all AS $tab) {
  785.                 $ind_qry  = 'SHOW INDEX FROM ' . PMA_backquote($tab);
  786.                 $ind_rs   = PMA_mysql_query($ind_qry);
  787.                 while ($ind = PMA_mysql_fetch_array($ind_rs)) {
  788.                     $col1 = $tab . '.' . $ind['Column_name'];
  789.                     if (isset($col_all[$col1])) {
  790.                         if ($ind['non_unique'] == 0) {
  791.                             if (isset($col_where[$col1])) {
  792.                                 $col_unique[$col1] = 'Y';
  793.                             } else {
  794.                                 $col_unique[$col1] = 'N';
  795.                             }
  796.                         } else {
  797.                             if (isset($col_where[$col1])) {
  798.                                 $col_index[$col1] = 'Y';
  799.                             } else {
  800.                                 $col_index[$col1] = 'N';
  801.                             }
  802.                         }
  803.                     }
  804.                 } // end while (each col of tab)
  805.             } // end while (each tab)
  806.             // now we want to find the best.
  807.             if (isset($col_unique) && count($col_unique) > 0) {
  808.                 $col_cand = $col_unique;
  809.                 $needsort = 1;
  810.             } else if (isset($col_index) && count($col_index) > 0) {
  811.                 $col_cand = $col_index;
  812.                 $needsort = 1;
  813.             } else if (isset($col_where) && count($col_where) > 0) {
  814.                 $col_cand = $tab_wher;
  815.                 $needsort = 0;
  816.             } else {
  817.                 $col_cand = $tab_all;
  818.                 $needsort = 0;
  819.             }
  820.  
  821.             // If we came up with $col_unique (very good) or $col_index (still
  822.             // good) as $col_cand we want to check if we have any 'Y' there
  823.             // (that would mean that they were also found in the whereclauses
  824.             // which would be great). if yes, we take only those
  825.             if ($needsort == 1) {
  826.                 foreach($col_cand AS $col => $is_where) {
  827.                     $tab           = explode('.', $col);
  828.                     $tab           = $tab[0];
  829.                     if ($is_where == 'Y') {
  830.                         $vg[$col]  = $tab;
  831.                     } else {
  832.                         $sg[$col]  = $tab;
  833.                     }
  834.                 }
  835.                 if (isset($vg)) {
  836.                     $col_cand      = $vg;
  837.                     // Candidates restricted in index+where
  838.                 } else {
  839.                     $col_cand      = $sg;
  840.                     // None of the candidates where in a where-clause
  841.                 }
  842.             }
  843.  
  844.             // If our array of candidates has more than one member we'll just
  845.             // find the smallest table.
  846.             // Of course the actual query would be faster if we check for
  847.             // the Criteria which gives the smallest result set in its table,
  848.             // but it would take too much time to check this
  849.             if (count($col_cand) > 1) {
  850.                 // Of course we only want to check each table once
  851.                 $checked_tables = $col_cand;
  852.                 foreach($col_cand AS $tab) {
  853.                     if ($checked_tables[$tab] != 1 ) {
  854.                         $rows_qry = 'SELECT COUNT(1) AS anz '
  855.                                   . 'FROM ' . PMA_backquote($tab);
  856.                         $rows_rs  = PMA_mysql_query($rows_qry);
  857.                         while ($res = PMA_mysql_fetch_array($rows_rs)) {
  858.                             $tsize[$tab] = $res['anz'];
  859.                         }
  860.                         $checked_tables[$tab] = 1;
  861.                     }
  862.                     $csize[$tab] = $tsize[$tab];
  863.                 }
  864.                 asort($csize);
  865.                 reset($csize);
  866.                 $master = key($csize); // Smallest
  867.             } else {
  868.                 reset($col_cand);
  869.                 $master = current($col_cand); // Only one single candidate
  870.             }
  871.         } // end if (exactly one where clause)
  872.  
  873.         /**
  874.          * Removes unwanted entries from an array (PHP3 compliant)
  875.          *
  876.          * @param   array  the array to work with
  877.          * @param   array  the list of keys to remove
  878.          *
  879.          * @return  array  the cleaned up array
  880.          *
  881.          * @access  private
  882.          */
  883.         function PMA_arrayShort($array, $key)
  884.         {
  885.             foreach($array AS $k => $v) {
  886.                 if ($k != $key) {
  887.                     $reta[$k] = $v;
  888.                 }
  889.             }
  890.             if (!isset($reta)) {
  891.                 $reta = array();
  892.             }
  893.  
  894.             return $reta;
  895.         } // end of the "PMA_arrayShort()" function
  896.  
  897.  
  898.         /**
  899.          * Finds all related tables
  900.          *
  901.          * @param   string   wether to go from master to foreign or vice versa
  902.          *
  903.          * @return  boolean  always TRUE
  904.          *
  905.          * @global  array    the list of tables that we still couldn't connect
  906.          * @global  array    the list of allready connected tables
  907.          * @global  string   the current databse name
  908.          * @global  string   the super user connection id
  909.          * @global  array    the list of relation settings
  910.          *
  911.          * @access  private
  912.          */
  913.         function PMA_getRelatives($from) {
  914.             global $tab_left, $tab_know, $fromclause;
  915.             global $dbh, $db, $cfgRelation;
  916.  
  917.             if ($from == 'master') {
  918.                 $to    = 'foreign';
  919.             } else {
  920.                 $to    = 'master';
  921.             }
  922.             $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
  923.             $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
  924.  
  925.             $rel_query = 'SELECT *'
  926.                        . ' FROM ' . PMA_backquote($cfgRelation['relation'])
  927.                        . ' WHERE ' . $from . '_db   = \'' . PMA_sqlAddslashes($db) . '\''
  928.                        . ' AND ' . $to   . '_db   = \'' . PMA_sqlAddslashes($db) . '\''
  929.                        . ' AND ' . $from . '_table IN ' . $in_know
  930.                        . ' AND ' . $to   . '_table IN ' . $in_left;
  931.             if (isset($dbh)) {
  932.                 PMA_mysql_select_db($cfgRelation['db'], $dbh);
  933.                 $relations = @PMA_mysql_query($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
  934.                 PMA_mysql_select_db($db, $dbh);
  935.             } else {
  936.                 PMA_mysql_select_db($cfgRelation['db']);
  937.                 $relations = @PMA_mysql_query($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
  938.                 PMA_mysql_select_db($db);
  939.             }
  940.             while ($row = PMA_mysql_fetch_array($relations)) {
  941.                 $found_table                = $row[$to . '_table'];
  942.                 if (isset($tab_left[$found_table])) {
  943.                     $fromclause             .= "\n" . ' LEFT JOIN '
  944.                                             . PMA_backquote($row[$to . '_table']) . ' ON '
  945.                                             . PMA_backquote($row[$from . '_table']) . '.'
  946.                                             . PMA_backquote($row[$from . '_field']) . ' = '
  947.                                             . PMA_backquote($row[$to . '_table']) . '.'
  948.                                             . PMA_backquote($row[$to . '_field']) . ' ';
  949.                     $tab_know[$found_table] = $found_table;
  950.                     $tab_left               = PMA_arrayShort($tab_left, $found_table);
  951.                 }
  952.             } // end while
  953.  
  954.             return TRUE;
  955.         } // end of the "PMA_getRelatives()" function
  956.  
  957.  
  958.         $tab_left          = PMA_arrayShort($tab_all, $master);
  959.         $tab_know[$master] = $master;
  960.  
  961.         $run   = 0;
  962.         $emerg = '';
  963.         while (count($tab_left) > 0) {
  964.             if ($run % 2 == 0) {
  965.                 PMA_getRelatives('master');
  966.             } else {
  967.                 PMA_getRelatives('foreign');
  968.             }
  969.             $run++;
  970.             if ($run > 5) {
  971.  
  972.                 foreach($tab_left AS $tab) {
  973.                     $emerg    .= ', ' . $tab;
  974.                     $tab_left = PMA_arrayShort($tab_left, $tab);
  975.                 }
  976.             }
  977.         } // end while
  978.         $qry_from = $master . $emerg . $fromclause;
  979.     } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
  980.  
  981. } // end count($Field) > 0
  982.  
  983. // In case relations are not defined, just generate the FROM clause
  984. // from the list of tables, however we don't generate any JOIN
  985.  
  986. if (empty($qry_from) && isset($tab_all)) {
  987.     $qry_from = implode(', ', $tab_all);
  988. }
  989. // Now let's see what we got
  990. if (!empty($qry_from)) {
  991.     $encoded_qry  .= urlencode('FROM ' . $qry_from . "\n");
  992.     echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
  993. }
  994.  
  995. // 3. WHERE
  996. $qry_where          = '';
  997. $criteria_cnt       = 0;
  998. for ($x = 0; $x < $col; $x++) {
  999.     if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
  1000.         $qry_where  .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
  1001.     }
  1002.     if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
  1003.         $qry_where  .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
  1004.         $last_where = $x;
  1005.         $criteria_cnt++;
  1006.     }
  1007. } // end for
  1008. if ($criteria_cnt > 1) {
  1009.     $qry_where      = '(' . $qry_where . ')';
  1010. }
  1011. // OR rows ${'cur' . $or}[$x]
  1012. if (!isset($curAndOrRow)) {
  1013.     $curAndOrRow          = array();
  1014. }
  1015. for ($y = 0; $y <= $row; $y++) {
  1016.     $criteria_cnt         = 0;
  1017.     $qry_orwhere          = '';
  1018.     $last_orwhere         = '';
  1019.     for ($x = 0; $x < $col; $x++) {
  1020.         if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x]) && $x) {
  1021.             $qry_orwhere  .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
  1022.         }
  1023.         if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x])) {
  1024.             $qry_orwhere  .= '(' . $curField[$x]
  1025.                           .  ' '
  1026.                           .  ${'curOr' . $y}[$x]
  1027.                           .  ')';
  1028.             $last_orwhere = $x;
  1029.             $criteria_cnt++;
  1030.         }
  1031.     } // end for
  1032.     if ($criteria_cnt > 1) {
  1033.         $qry_orwhere      = '(' . $qry_orwhere . ')';
  1034.     }
  1035.     if (!empty($qry_orwhere)) {
  1036.         $qry_where .= "\n"
  1037.                    .  strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '')
  1038.                    .  $qry_orwhere;
  1039.     } // end if
  1040. } // end for
  1041.  
  1042. if (!empty($qry_where) && $qry_where != '()') {
  1043.     $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
  1044.     echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
  1045. } // end if
  1046.  
  1047. // 4. ORDER BY
  1048. $last_orderby = 0;
  1049. if (!isset($qry_orderby)) {
  1050.     $qry_orderby      = '';
  1051. }
  1052. for ($x = 0; $x < $col; $x++) {
  1053.     if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
  1054.         $qry_orderby  .=  ', ';
  1055.     }
  1056.     if (!empty($curField[$x]) && !empty($curSort[$x])) {
  1057.         // if they have chosen all fields using the * selector,
  1058.         // then sorting is not available
  1059.         // Robbat2 - Fix for Bug #570698
  1060.         if (substr($curField[$x], -2) != '.*') {
  1061.             $qry_orderby  .=  $curField[$x] . ' ' . $curSort[$x];
  1062.             $last_orderby = 1;
  1063.         }
  1064.     }
  1065. } // end for
  1066. if (!empty($qry_orderby)) {
  1067.     $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
  1068.     echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
  1069. }
  1070. ?>
  1071. </textarea>
  1072.             <input type="hidden" name="encoded_sql_query" value="<?php echo $encoded_qry; ?>" />
  1073.         </td>
  1074.     </tr>
  1075.     </table>
  1076.  
  1077. </form>
  1078.  
  1079.  
  1080. <?php
  1081. /**
  1082.  * Displays the footer
  1083.  */
  1084. require_once('./footer.inc.php');
  1085. ?>
  1086.