Retrieve data from sql database and display in tables - Display certain data according to checkboxes checked

后端 未结 2 2054
[愿得一人]
[愿得一人] 2020-12-21 13:15

I have created an sql database(with phpmyadmin) filled with measurements from which I want to call data between two dates( the user selects the DATE by entering in the HTML

2条回答
  •  独厮守ぢ
    2020-12-21 13:46

    Try to create your checkbox like below:

    Solar_Time_Decimal
    GHI
    DiffuseHI
    Zenith_Angle
    DNI 
    

    And try to hange your PHP code to this:

     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 "";
    foreach($column_names as $k => $v) { 
      if($all || (is_array($column_entries) && in_array($k, $column_entries)))
         echo "";
    }
    echo "";
    while( $row = mysql_fetch_assoc($result))
    {
        echo "";  
        echo "";   
        echo "";  
        foreach($column_names as $k => $v) { 
          if($all || (is_array($column_entries) && in_array($k, $column_entries))) {
             echo "";
           }
        }
        echo "";
    }
    echo '
    DATE Local_Time_Decimal$v
    " . $row['DATE'] . "" . $row['Local_Time_Decimal'] . "".$row[$v]."
    '; if($result){ echo "Successful"; } else{ echo "Enter correct dates"; } ?>

    This solution consider your particular table columns but if your wish a generic solution you can try to use this SQL too:

    $sql_names = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$database_Database_Test' AND TABLE_NAME = '$table_name'";
    

    and use the result to construct the $column_names array.

提交回复
热议问题