我已经创建了一个装有测量值的sql数据库(使用phpmyadmin),我想从中调用两个日期之间的数据(用户通过在HTML表单中输入“ FROM”和“ TO”日期来选择DATE),并将其显示在桌子。
另外,我在html表单下放置了一些复选框,通过选中它们可以限制显示的数据量。
每个复选框代表我数据库的一列;因此,连同日期和小时列一起,将显示已检查的所有内容(如果未选中则显示所有内容)。
到目前为止,我设法编写了一个连接数据库的php脚本,在未选中任何复选框的情况下显示所有内容,并且还设法将其中一个复选框排序。
问题:我需要的数据显示了两次。
问题:我要有四个复选框。
我是否需要为每种可能的组合编写一个sql查询,还是有一种更简单的方法?
<?php # FileName="Connection_php_mysql.htm" # Type="MYSQL" # HTTP="true" $hostname_Database_Test = "localhost"; $database_Database_Test = "database_test"; $table_name = "solar_irradiance"; $username_Database_Test = "root"; $password_Database_Test = ""; $Database_Test = mysql_pconnect($hostname_Database_Test, $username_Database_Test, $password_Database_Test) or trigger_error(mysql_error(),E_USER_ERROR); //HTML forms -> variables $fromdate = $_POST['fyear']; $todate = $_POST['toyear']; //DNI CHECKBOX + ALL $dna="SELECT DATE, Local_Time_Decimal, DNI FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; $tmp ="SELECT * FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; $entry=$_POST['dni']; if (empty($entry)) { $result = mysql_query($tmp); echo "<table border='1' style='width:300px'> <tr> <th>DATE</th> <th>Local_Time_Decimal</th> <th>Solar_time_decimal</th> <th>GHI</th> <th>DiffuseHI</th> <th>zenith_angle</th> <th>DNI</th> "; while( $row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $row['DATE'] . "</td>"; echo "<td>" . $row['Local_Time_Decimal'] . "</td>"; echo "<td>" . $row['Solar_Time_Decimal'] . "</td>"; echo "<td>" . $row['GHI'] . "</td>"; echo "<td>" . $row['DiffuseHI'] . "</td>"; echo "<td>" . $row['Zenith_Angle'] . "</td>"; echo "<td>" . $row['DNI'] . "</td>"; echo "</tr>"; } echo '</table>';} else { $result= mysql_query($dna); echo "<table border='1' style='width:300px'> <tr> <th>DATE</th> <th>Local_Time_Decimal</th> <th>DNI</th> "; while($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $row['DATE'] . "</td>"; echo "<td>" . $row['Local_Time_Decimal']."</td>"; echo "<td>" . $row['DNI'] . "</td>"; echo "</tr>"; } echo '</table>'; } if($result){ echo "Successful"; } else{ echo "Enter correct dates"; } ?> <?php mysql_close(); ?>
尝试创建您的复选框,如下所示:
Solar_Time_Decimal<checkbox name='columns[]' value='1'> GHI<checkbox name='columns[]' value='2'> DiffuseHI<checkbox name='columns[]' value='3'> Zenith_Angle<checkbox name='columns[]' value='4'> DNI<checkbox name='columns[]' value='5'>
并尝试将您的PHP代码附加到此:
<?php //HTML forms -> variables $fromdate = isset($_POST['fyear']) ? $_POST['fyear'] : data("d/m/Y"); $todate = isset($_POST['toyear']) ? $_POST['toyear'] : data("d/m/Y"); $all = false; $column_names = array('1' => 'Solar_Time_Decimal', '2'=>'GHI', '3'=>'DiffuseHI', '4'=>'Zenith_Angle','5'=>'DNI'); $column_entries = isset($_POST['columns']) ? $_POST['columns'] : array(); $sql_columns = array(); foreach($column_entries as $i) { if(array_key_exists($i, $column_names)) { $sql_columns[] = $column_names[$i]; } } if (empty($sql_columns)) { $all = true; $sql_columns[] = "*"; } else { $sql_columns[] = "DATE,Local_Time_Decimal"; } //DNI CHECKBOX + ALL $tmp ="SELECT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; $result = mysql_query($tmp); echo "<table border='1' style='width:300px'> <tr> <th>DATE</th> <th>Local_Time_Decimal</th>"; foreach($column_names as $k => $v) { if($all || (is_array($column_entries) && in_array($k, $column_entries))) echo "<th>$v</th>"; } echo "</tr>"; while( $row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $row['DATE'] . "</td>"; echo "<td>" . $row['Local_Time_Decimal'] . "</td>"; foreach($column_names as $k => $v) { if($all || (is_array($column_entries) && in_array($k, $column_entries))) { echo "<th>".$row[$v]."</th>"; } } echo "</tr>"; } echo '</table>'; if($result){ echo "Successful"; } else{ echo "Enter correct dates"; } ?> <?php mysql_close();?>
此解决方案考虑您特定的表列,但是如果您希望使用通用解决方案,则也可以尝试使用此SQL:
$sql_names = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$database_Database_Test' AND TABLE_NAME = '$table_name'";
并使用结果构造$column_names数组。
$column_names