2013-03-12 58 views
0

我正在構造一個允許用戶查詢表/數據庫的表單。用戶通過HTML下拉菜單選擇要從目錄加載的表格。該下拉菜單由一個PHP循環驅動,該循環讀取並顯示目錄中的文件(或表)。爲了實現對同一個表的多個查詢,用戶FIRST選擇一個複選框將他們查詢的表下載到同一目錄中,THEN SECOND從相同的下拉菜單中選擇他們的查詢文件。合理?提交表單後訪問使用PHP創建的文件

但是,在提交後,下拉菜單中新創建的文件不存在。該文件在頁面刷新後顯示。

現在的問題是:什麼是最好的方式來顯示新下載的表,以便在按下提交按鈕後立即通過下拉框(和PHP循環)識別它。我玩過javascript location.reload();但無濟於事。請參見下面簡化的代碼:

<html> 
<form action = "" method = "post"> 

Table File: <select name="hfile"> 


<?php 
$dir = "/Director/to/table/files"; 
$table_files = scandir($dir, 1); 

//This is going to create the drop-down menu displaying all files in directory $dir. 
$i = 0; 
while($i <= count($table_files)) { 
    echo "<option value = $table_files[$i]> $table_file[$i] </option>"; 
    $i = $i + 1; 
} 
?> 
</select> 

<!-- Below are just two of the form elements --> 
<input type ="checkbox" name="download_table" value="download"> Download Queried Table 
<input type = "submit" value="Submit" name="submit_query"> 

//Variables are set once the submit button is pressed 
if(isset($_POST["submit_query"])) 
    { 
    $download_table = $_POST['download_table']; 
    } 

//Download the table (if the download checkbox is on) 
if(isset($download_table)) { 
    $file = "query.txt"; 
    mysql_query("SELECT * FROM table INTO OUTFILE \"$file\" FIELDS TERMINATED BY ','") or die(mysql_error()); 

} 

+0

在哪裏聲明'$ download_table'?可能你正在談論'$ _POST ['download_table']'?而且,在刪除它之前,你應該檢查'/ Applications/MAMP/db/mysql/IESE/query.txt'是否存在(爲了避免消息)。 – MatRt 2013-03-12 02:56:00

+0

對不起,一旦按下提交按鈕,我就聲明$ download_table(參見上面更正的代碼)。我也刪除了'unlink(「/ Applications /..../」')這一行,因爲這只是一種讓我在創建該應用程序時能夠將該目錄中的文件數量控制得小而且易於管理的方式。 ! – 2013-03-12 03:02:27

+0

你在哪裏聲明'$ hypo_files'你的代碼有點混淆 – MatRt 2013-03-12 03:07:28

回答

0

問這個問題之後,我意識到自己的錯誤,並希望闡述,以避免一個懸而未決的問題。存在多個PHP if語句來確定如何查詢表。在每個if語句中,都有一個嵌入語句,用於下載原始問題中描述的排隊表。但是,如果沒有查詢任何內容,則在用戶請求下沒有處理下載的聲明。這在檢查整個應用程序的功能時引起頭痛。

如果有人感興趣,下面是代碼全部。

<form action = "" method = "post"> 

Hypo File: <select name="hfile"> 


<?php 
$dir = "/Applications/MAMP/db/mysql/IESE"; 
$hypo_files = scandir($dir, 1); 

$i = 0; 
while($i <= count($hypo_files) - 4) { 
    echo "<option value = $hypo_files[$i]> $hypo_files[$i] </option>"; 
    $i = $i + 1; 
} 

?> 
</select> 
<br><br> 

<fieldset> 
<legend>Hypo Query:</legend> 
<br> 

Min Value: <input type = "text" name = "min_value"> 
<!-- NOTE the names are named in relation to the col_name fields i.e. col names is greater than the field to the left and less than field to the right. --> 
<input type="radio" name="greater_than" value= ">="> <= 
<input type="radio" name="greater_than" value= ">"> <  

<!-- THIS IS THE DROP DOWN BOX FORM! (and an html comment) --> 
<select name="hypo_cols"> 
<option value="latitude_deg">latitude deg</option> 
<option value="longitude">longitude</option> 
<option value="depth">depth</option> 
<option value="origin_time">origin time</option> 
<option value="magnitude">magnitude</option> 
<option value="maximum_azimuthal_gap">max azimuthal gap</option> 
<option value="distance_to_nearest_station_km">distance to nearest station km</option> 
<option value="rms_travel_time_residual">rms travel time residual</option> 
<option value="version">version</option> 
<option value="auxiliary_remark_from_program">auxiliary remark from program</option> 
</select> 

<input type="radio" name="less_than" value = "="> == 
<input type="radio" name="less_than" value= "<="> <= 
<input type="radio" name="less_than" value= "<"> < 

Max/Equal to Value: <input type = "text" name = "max_value"> <br> <br> 
</fieldset> 

<div id="forum_options"> 
     <!-- THIS IS THE CHECKBOX TO DISPLAY HYPO TABLE --> 
<input type ="checkbox" name="display_table" value="display"> Print Queried Table 
<input type ="checkbox" name="display_map" value="map"> Print Map from Query 
    <input type ="checkbox" name="download_table" value="download"> Download Queried Table <br> <br> 
    <div id="submit"> 
    <input type = "submit" value="Submit" name="submit_query"> 
    <input type=button value="Refresh" onClick="window.location.reload()"> 
    </div> 
    </div> 
    </form> 


<?php 

// What are we going to do once the submit button is pressed? 
if(isset($_POST["submit_query"])) 
    { 
    //Lets define all the form values as $ without GET/POST. Seems to read better in the query further below. 
    $hfile = $_POST['hfile']; 
    $min_value = $_POST['min_value']; 
    $greater_than = $_POST['greater_than']; 
    $hypo_cols = $_POST['hypo_cols']; 
    $less_than = $_POST['less_than']; 
    $max_value = $_POST['max_value']; 
    $display_table = $_POST['display_table']; 
    $display_map = $_POST['display_map']; 
    $download_table = $_POST['download_table']; 

    //DEFINE and POPULATE the TABLE with $hfile. This is the only bit that is done no matter what else the form says! 
    $load_hypo_param = "LOAD DATA INFILE '/Applications/MAMP/db/mysql/IESE/$hfile' INTO TABLE hypo FIELDS TERMINATED BY ','"; 
    mysql_query($load_hypo_param) or die(mysql_error()); 

    } 


//a 
if($less_than == "=") 
    { 
    //echo "part a successful. <br>"; 
    $result1 = mysql_query("SELECT * FROM hypo WHERE $hypo_cols $less_than $max_value") or die(mysql_error()); 

    //Lets download(?) the table (if the download checkbox is on) 
    if(isset($download_table)) { 
     $file = "query.txt"; 
     unlink("/Applications/MAMP/db/mysql/IESE/query.txt"); 
     mysql_query("SELECT * FROM hypo WHERE $hypo_cols $less_than $max_value INTO OUTFILE \"$file\" FIELDS TERMINATED BY ','") or die(mysql_error()); 
    } 

    } 

//b 
else if($min_value != NULL and $max_value != NULL) //isset($min_value) and isset($max_value)) 
    { 
    //echo "part b successful" . "<br>" ; 
    $result1 = mysql_query("SELECT * FROM hypo WHERE $hypo_cols BETWEEN $min_value AND $max_value") or die(mysql_error()); 

    if(isset($download_table)) { 
     $file = "query.txt"; 
     unlink("/Applications/MAMP/db/mysql/IESE/query.txt"); 
     mysql_query("SELECT * FROM hypo WHERE $hypo_cols $less_than $max_value INTO OUTFILE \"$file\" FIELDS TERMINATED BY ','") or die(mysql_error()); 
    } 

    } 

//c 
else if($min_value != NULL) 
    { 
    //echo "part c successful" . "<br>"; 
    $result1 = mysql_query("SELECT * FROM hypo WHERE $hypo_cols $greater_than $min_value") or die(mysql_error()); 

    if($download_table != NULL) { 
     $file = "query.txt"; 
     unlink("/Applications/MAMP/db/mysql/IESE/query.txt"); 
     mysql_query("SELECT * FROM hypo WHERE $hypo_cols $greater_than $min_value INTO OUTFILE '$file' FIELDS TERMINATED BY ','") or die(mysql_error()); 
    } 
    } 

//d 
else if($max_value != NULL) 
    { 
    //echo "part d successful" . "<br>"; 
    $result1 = mysql_query("SELECT * FROM hypo WHERE $hypo_cols $less_than $max_value") or die(mysql_error()); 

    if(isset($download_table)) { 
    $file = "query.txt"; 
    unlink("/Applications/MAMP/db/mysql/IESE/query.txt"); 
    mysql_query("SELECT * FROM hypo WHERE $hypo_cols $less_than $max_value INTO OUTFILE \"$file\" FIELDS TERMINATED BY ','") or die(mysql_error()); 
    } 

    } 

else 
    { 
    echo "Nothing seems to be set. <br>"; 
    $file = "query.txt"; 
    unlink("/Applications/MAMP/db/mysql/IESE/query.txt"); 
    mysql_query("SELECT * FROM hypo INTO OUTFILE \"$file\" FIELDS TERMINATED BY ','") or die(mysql_error()); 

    $result1 = mysql_query("SELECT * FROM hypo"); 
    $nothing_set=TRUE; 
    } 

//echo all results. 

if(!isset($nothing_set)) { 
echo "<h4>" . "Query successful and involves:" . "<br>" . "$hypo_cols " . "$greater_than " . "$min_value " . "<br>" . "$hypo_cols " . "$less_than " . "$max_value" . "<br>" . "</h4>"; 
} 

    //Lets print the map (if the print map checkbox is on) 
    if(isset($display_map)) { 
    //lets create a string of lat long values from the result1 queried table. 

    ?>