php – 从sql数据库中检索数据并显示在表格中 – 根据选中的复选框显示某些数据

前端之家收集整理的这篇文章主要介绍了php – 从sql数据库中检索数据并显示在表格中 – 根据选中的复选框显示某些数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个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数组.

原文链接:https://www.f2er.com/php/138577.html

猜你在找的PHP相关文章