2013-03-16 30 views
1

我一直在尋找最近兩天的相關答案,但一直未能做出任何答案。從下拉列表中使用SQL,Javascript和/或PHP填充html表格

我正在創建一個使用JavaScript,PHP和SQL的網頁,它最終會比我所要求的更加健壯,但是暫時這是我的問題。

我有一個初始下拉列表,填充39個不同的表(星座或衛星組)在SQL數據庫中的名稱。基於第一個SQL查詢,在提交時,頁面將整個表格顯示爲頁面底部的html。

此外,第二個查詢填充第二個下拉列表(星座內的特定衛星/對象),顯示所選表中每行的名稱。第二個下拉列表顯示正常。

但是,當我在第二個下拉菜單之後單擊提交時,查詢應該傳遞給一個html表格,以便僅顯示來自第二個查詢的所選行信息(衛星/對象的軌道參數)。相反,當我單擊提交時,表中沒有顯示任何內容,並且頁面刷新返回到其初始狀態。我不知道如何成功地將查詢傳遞給表。每個表格包含8到2700個不同對象(總計超過6000個),所以我不能簡單地列出每一行,尤其是因爲數據每天都在變化。

表中的數據最終將用於使用java或其他方法計算軌道,並在嵌入式Google地球頁面中顯示它們。

我對JavaScript,PHP和SQL很熟悉,但對其他方法並不熟悉,但我樂於接受建議。

以下是該頁面的相關代碼。

<?php 

    if (isset($_POST['constellation'])) { 
    $constellation = $_POST['constellation']; } 
    else { 
    $constellation = 'Space Stations'; 
    } 


    if (isset($_POST['satellite'])) { 
    $satellite = $_POST['satellite']; } 
    else { 
    $satellite = 'ISS (Zarya)'; 
    } 

?> 

    <p>Use the pull-down menu's to choose the specific constellation (group) of satellites to view the most recent orbital data.</a></p> 
    <hr /> 

    <div class="masterpulldown"> 

    <table class="pulldown"> 
    <tr> 
    <td "colspan="2"> 
     <p class="pulldownheader">Constellation Query</p> 
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="pulldownGroup" name="pulldownGroup" method="post" > 
     <select id="constellation" name="constellation" value=$constellation > 
     <option value="Last 30 Days" <?php if($constellation=="Last 30 Days") echo "selected"; ?> >Last 30 Days</option> 
     <option value="Space Stations" <?php if($constellation=="Space Stations") echo "selected"; ?> >Space Stations</option> 
     <option value="100 (Or So) Brightest" <?php if($constellation=="100 (Or So) Brightest") echo "selected"; ?> >100 (Or So) Brightest</option> 
     <option value="Weather" <?php if($constellation=="Weather") echo "selected"; ?> >Weather</option> 
     <option value="Noaa" <?php if($constellation=="Noaa") echo "selected"; ?> >Noaa</option> 
     <option value="Goes" <?php if($constellation=="Goes") echo "selected"; ?> >Goes</option> 
     <option value="Earth Resources" <?php if($constellation=="Earth Resources") echo "selected"; ?> >Earth Resources</option> 
     <option value="Search And Rescue (Sarsat)" <?php if($constellation=="Search And Rescue (Sarsat)") echo "selected"; ?> >Search And Rescue (Sarsat)</option> 
     <option value="Disaster Monitoring" <?php if($constellation=="Disaster Monitoring") echo "selected"; ?> >Disaster Monitoring</option> 
     <option value="Tracking And Data Relay Satellite System (Tdrss)" <?php if($constellation=="Tracking And Data Relay Satellite System (Tdrss)") echo "selected"; ?> >Tracking And Data Relay Satellite System (Tdrss)</option> 
     <option value="Geostationary" <?php if($constellation=="Geostationary") echo "selected"; ?> >Geostationary</option> 
     <option value="Intelsat" <?php if($constellation=="Intelsat") echo "selected"; ?> >Intelsat</option> 
     <option value="Gorizont" <?php if($constellation=="Gorizont") echo "selected"; ?> >Gorizont</option> 
     <option value="Raduga" <?php if($constellation=="Raduga") echo "selected"; ?> >Raduga</option> 
     <option value="Molniya" <?php if($constellation=="Molniya") echo "selected"; ?> >Molniya</option> 
     <option value="Iridium" <?php if($constellation=="Iridium") echo "selected"; ?> >Iridium</option> 
     <option value="Orbcomm" <?php if($constellation=="Orbcomm") echo "selected"; ?> >Orbcomm</option> 
     <option value="Globalstar" <?php if($constellation=="Globalstar") echo "selected"; ?> >Globalstar</option> 
     <option value="Amateur Radio" <?php if($constellation=="Amateur Radio") echo "selected"; ?> >Amateur Radio</option> 
     <option value="Experimental" <?php if($constellation=="Experimental") echo "selected"; ?> >Experimental</option> 
     <option value="Communication Satellites - Other" <?php if($constellation=="Communication Satellites - Other") echo "selected"; ?> >Communication Satellites - Other</option> 
     <option value="Gps Operational" <?php if($constellation=="Gps Operational") echo "selected"; ?> >Gps Operational</option> 
     <option value="Glonass Operational" <?php if($constellation=="Glonass Operational") echo "selected"; ?> >Glonass Operational</option> 
     <option value="Galileo" <?php if($constellation=="Galileo") echo "selected"; ?> >Galileo</option> 
     <option value="Satellite-Based Augmentation System" <?php if($constellation=="Satellite-Based Augmentation System") echo "selected"; ?> >Satellite-Based Augmentation System</option> 
     <option value="Navy Navigation Satellite System (Nnss)" <?php if($constellation=="Navy Navigation Satellite System (Nnss)") echo "selected"; ?> >Navy Navigation Satellite System (Nnss)</option> 
     <option value="Russian Leo Navigation" <?php if($constellation=="Russian Leo Navigation") echo "selected"; ?> >Russian Leo Navigation</option> 
     <option value="Space And Earth Science" <?php if($constellation=="Space And Earth Science") echo "selected"; ?> >Space And Earth Science</option> 
     <option value="Geodetic" <?php if($constellation=="Geodetic") echo "selected"; ?> >Geodetic</option> 
     <option value="Engineering" <?php if($constellation=="Engineering") echo "selected"; ?> >Engineering</option> 
     <option value="Education" <?php if($constellation=="Education") echo "selected"; ?> >Education</option> 
     <option value="Miscellaneous Military" <?php if($constellation=="Miscellaneous Military") echo "selected"; ?> >Miscellaneous Military</option> 
     <option value="Radar Calibration" <?php if($constellation=="Radar Calibration") echo "selected"; ?> >Radar Calibration</option> 
     <option value="Cubesats" <?php if($constellation=="Cubesats") echo "selected"; ?> >Cubesats</option> 
     <option value="Miscellaneous Satellites - Other" <?php if($constellation=="Miscellaneous Satellites - Other") echo "selected"; ?> >Miscellaneous Satellites - Other</option> 
     <option value="Fengyun 1C Debris" <?php if($constellation=="Fengyun 1C Debris") echo "selected"; ?> >Fengyun 1C Debris</option> 
     <option value="Iridium 33 Debris" <?php if($constellation=="Iridium 33 Debris") echo "selected"; ?> >Iridium 33 Debris</option> 
     <option value="Cosmos 2251 Debris" <?php if($constellation=="Cosmos 2251 Debris") echo "selected"; ?> >Cosmos 2251 Debris</option> 
     <option value="Breeze-M Breakup" <?php if($constellation=="Breeze-M Breakup") echo "selected"; ?> >Breeze-M Breakup</option> 
     <input id="submit" type=submit value=submit /> 
     </form> 
     </select> 
    </td> 
    </tr> 
    </table> 


<?php 

    require_once('connectvars.php'); 
    // require_once('appvars.php'); 


    // Connect to the database 
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    $a1=array("Last30Days","SpaceStations","HundredOrSoBrightest","Weather","Noaa","Goes","EarthResources","SearchAndRescueSarsat","DisasterMonitoring","TrackingAndDataRelaySatelliteSystemTdrss","Geostationary","Intelsat","Gorizont","Raduga","Molniya","Iridium","Orbcomm","Globalstar","AmateurRadio","Experimental","CommunicationSatellitesOther","GpsOperational","GlonassOperational","Galileo","SatelliteBasedAugmentationSystem","NavyNavigationSatelliteSystemNnss","RussianLeoNavigation","SpaceAndEarthScience","Geodetic","Engineering","Education","MiscellaneousMilitary","RadarCalibration","Cubesats","MiscellaneousSatellitesOther","Fengyun1CDebris","Iridium33Debris","Cosmos2251Debris","BreezeMRBBreakup"); 
    $a2=array("Last 30 Days","Space Stations","100 (Or So) Brightest","Weather","Noaa","Goes","Earth Resources","Search And Rescue (Sarsat)","Disaster Monitoring","Tracking And Data Relay Satellite System (Tdrss)","Geostationary","Intelsat","Gorizont","Raduga","Molniya","Iridium","Orbcomm","Globalstar","Amateur Radio","Experimental","Communication Satellites - Other","Gps Operational","Glonass Operational","Galileo","Satellite-Based Augmentation System","Navy Navigation Satellite System (Nnss)","Russian Leo Navigation","Space And Earth Science","Geodetic","Engineering","Education","Miscellaneous Military","Radar Calibration","Cubesats","Miscellaneous Satellites - Other","Fengyun 1C Debris","Iridium 33 Debris","Cosmos 2251 Debris","Breeze-M Breakup"); 
    $arr=array_combine($a1, $a2); 

     $Last30Days = $arr["Last30Days"]; 
     $SpaceStations = $arr["SpaceStations"]; 
     $HundredOrSoBrightest = $arr["HundredOrSoBrightest"]; 
     $Weather = $arr["Weather"]; 
     $Noaa = $arr["Noaa"]; 
     $Goes = $arr["Goes"]; 
     $EarthResources = $arr["EarthResources"]; 
     $SearchAndRescueSarsat = $arr["SearchAndRescueSarsat"]; 
     $DisasterMonitoring = $arr["DisasterMonitoring"]; 
     $TrackingAndDataRelaySatelliteSystemTdrss = $arr["TrackingAndDataRelaySatelliteSystemTdrss"]; 
     $Geostationary = $arr["Geostationary"]; 
     $Intelsat = $arr["Intelsat"]; 
     $Gorizont = $arr["Gorizont"]; 
     $Raduga = $arr["Raduga"]; 
     $Molniya = $arr["Molniya"]; 
     $Iridium = $arr["Iridium"]; 
     $Orbcomm = $arr["Orbcomm"]; 
     $Globalstar = $arr["Globalstar"]; 
     $AmateurRadio = $arr["AmateurRadio"]; 
     $Experimental = $arr["Experimental"]; 
     $CommunicationSatellitesOther = $arr["CommunicationSatellitesOther"]; 
     $GpsOperational = $arr["GpsOperational"]; 
     $GlonassOperational = $arr["GlonassOperational"]; 
     $Galileo = $arr["Galileo"]; 
     $SatelliteBasedAugmentationSystem = $arr["SatelliteBasedAugmentationSystem"]; 
     $NavyNavigationSatelliteSystemNnss = $arr["NavyNavigationSatelliteSystemNnss"]; 
     $RussianLeoNavigation = $arr["RussianLeoNavigation"]; 
     $SpaceAndEarthScience = $arr["SpaceAndEarthScience"]; 
     $Geodetic = $arr["Geodetic"]; 
     $Engineering = $arr["Engineering"]; 
     $Education = $arr["Education"]; 
     $MiscellaneousMilitary = $arr["MiscellaneousMilitary"]; 
     $RadarCalibration = $arr["RadarCalibration"]; 
     $Cubesats = $arr["Cubesats"]; 
     $MiscellaneousSatellitesOther = $arr["MiscellaneousSatellitesOther"]; 
     $Fengyun1CDebris = $arr["Fengyun1CDebris"]; 
     $Iridium33Debris = $arr["Iridium33Debris"]; 
     $Cosmos2251Debris = $arr["Cosmos2251Debris"]; 
     $BreezeMRBBreakup = $arr["BreezeMRBBreakup"]; 


    $a3=array($Last30Days,$SpaceStations,$HundredOrSoBrightest,$Weather,$Noaa,$Goes,$EarthResources,$SearchAndRescueSarsat,$DisasterMonitoring,$TrackingAndDataRelaySatelliteSystemTdrss,$Geostationary,$Intelsat,$Gorizont,$Raduga,$Molniya,$Iridium,$Orbcomm,$Globalstar,$AmateurRadio,$Experimental,$CommunicationSatellitesOther,$GpsOperational,$GlonassOperational,$Galileo,$SatelliteBasedAugmentationSystem,$NavyNavigationSatelliteSystemNnss,$RussianLeoNavigation,$SpaceAndEarthScience,$Geodetic,$Engineering,$Education,$MiscellaneousMilitary,$RadarCalibration,$Cubesats,$MiscellaneousSatellitesOther,$Fengyun1CDebris,$Iridium33Debris,$Cosmos2251Debris,$BreezeMRBBreakup); 
    $a4=array("Last30Days","SpaceStations","HundredOrSoBrightest","Weather","Noaa","Goes","EarthResources","SearchAndRescueSarsat","DisasterMonitoring","TrackingAndDataRelaySatelliteSystemTdrss","Geostationary","Intelsat","Gorizont","Raduga","Molniya","Iridium","Orbcomm","Globalstar","AmateurRadio","Experimental","CommunicationSatellitesOther","GpsOperational","GlonassOperational","Galileo","SatelliteBasedAugmentationSystem","NavyNavigationSatelliteSystemNnss","RussianLeoNavigation","SpaceAndEarthScience","Geodetic","Engineering","Education","MiscellaneousMilitary","RadarCalibration","Cubesats","MiscellaneousSatellitesOther","Fengyun1CDebris","Iridium33Debris","Cosmos2251Debris","BreezeMRBBreakup"); 
    $arr=array_combine($a3, $a4); 

    $query = "SELECT * FROM $arr[$constellation] order by mission ASC"; 
    $data = mysqli_query($dbc, $query); 

?> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="pulldownGroup2" name="pulldownGroup" method="post" > 

    <table class="pulldown"> 
    <tr> 
     <td colspan="2"> 
     <p class="pulldownheader">Satellite Results (Norad Number and Satellite Name)</p> 
     <select id="satellite" name="satellite" value=$satellite > 
     <option class="subdued" value=$satellite>Select for Satellite or leave blank for Constellation</option> 
<?php 
     $i = 0; 
     while ($row = mysqli_fetch_array($data)) { 
      // Display the satellite data 
       if ($i == 0) { 
       echo '<option value=$satellite>' . $row['mission'] . ' ' . $row['satellite'] . '</option>'; 
       } 
      } 
?> 
     <input id="submit" type=submit value=submit /> 
     </select> 
     </td> 
    </tr> 
    </table> 

<?php 
    //while ($row = mysqli_fetch_array($data)) { 
?> 

    <table class="chosensat"> 
    <tr> 
     <td class="sat2">Satellite Mission and Name: <?php $row['mission'] . ' ' . $row['satellite']; ?></td> 
    </tr> 
    <tr> 
     <td class="sat2">Epoch Time and Greenwich time: <?php $row['epochTime'] . ' ' . $row['gmtTime']; ?></td> 
    </tr> 
    <tr> 
     <td class="sat2">Inclination: <?php $row['inclination']; ?> deg</td> 
    </tr> 
    <tr> 
     <td class="sat2">Right Ascension: <?php $row['raan']; ?> deg</td> 
    </tr> 
    <tr> 
     <td class="sat2">Eccentricity: <?php $row['eccentricity']; ?></td> 
    </tr> 
    <tr> 
     <td class="sat2">Argument of Perigee: <?php $row['argOfPerigee']; ?> deg</td> 
    </tr> 
    <tr> 
     <td class="sat2">Mean Anomaly: <?php $row['meanAnomaly']; ?> deg</td> 
    </tr> 
    <tr> 
     <td class="sat2">Mean Motion: <?php $row['meanMotion']; ?> rev/day</td> 
    </tr> 
    </table> 

<?php 
    //} 
?> 

    </form> 

<p class="pulldownheader2">Selected Constellation: 


<?php 

    if($constellation=="Last 30 Days") echo $Last30Days; 
    elseif($constellation=="Space Stations") echo $SpaceStations; 
    elseif($constellation=="Hundred Or So Brightest") echo $HundredOrSoBrightest; 
    elseif($constellation=="Weather") echo $Weather; 
    elseif($constellation=="Noaa") echo $Noaa; 
    elseif($constellation=="Goes") echo $Goes; 
    elseif($constellation=="Earth Resources") echo $EarthResources; 
    elseif($constellation=="Search And Rescue (Sarsat)") echo $SearchAndRescueSarsat; 
    elseif($constellation=="Disaster Monitoring") echo $DisasterMonitoring; 
    elseif($constellation=="Tracking And Data Relay Satellite System (Tdrss)") echo $TrackingAndDataRelaySatelliteSystemTdrss; 
    elseif($constellation=="Geostationary") echo $Geostationary; 
    elseif($constellation=="Intelsat") echo $Intelsat; 
    elseif($constellation=="Gorizont") echo $Gorizont; 
    elseif($constellation=="Raduga") echo $Raduga; 
    elseif($constellation=="Molniya") echo $Molniya; 
    elseif($constellation=="Iridium") echo $Iridium; 
    elseif($constellation=="Orbcomm") echo $Orbcomm; 
    elseif($constellation=="Globalstar") echo $Globalstar; 
    elseif($constellation=="Amateur Radio") echo $AmateurRadio; 
    elseif($constellation=="Experimental") echo $Experimental; 
    elseif($constellation=="Communication Satellites - Other") echo $CommunicationSatellitesOther; 
    elseif($constellation=="Gps Operational") echo $GpsOperational; 
    elseif($constellation=="Glonass Operational") echo $GlonassOperational; 
    elseif($constellation=="Galileo") echo $Galileo; 
    elseif($constellation=="Satellite-Based Augmentation System") echo $SatelliteBasedAugmentationSystem; 
    elseif($constellation=="Navy Navigation Satellite System (Nnss)") echo $NavyNavigationSatelliteSystemNnss; 
    elseif($constellation=="Russian Leo Navigation") echo $RussianLeoNavigation; 
    elseif($constellation=="Space And Earth Science") echo $SpaceAndEarthScience; 
    elseif($constellation=="Geodetic") echo $Geodetic; 
    elseif($constellation=="Engineering") echo $Engineering; 
    elseif($constellation=="Education") echo $Education; 
    elseif($constellation=="Miscellaneous Military") echo $MiscellaneousMilitary; 
    elseif($constellation=="Radar Calibration") echo $RadarCalibration; 
    elseif($constellation=="Cubesats") echo $Cubesats; 
    elseif($constellation=="Miscellaneous Satellites - Other") echo $MiscellaneousSatellitesOther; 
    elseif($constellation=="Fengyun 1C Debris") echo $Fengyun1CDebris; 
    elseif($constellation=="Iridium 33 Debris") echo $Iridium33Debris; 
    elseif($constellation=="Cosmos 2251 Debris") echo $Cosmos2251Debris; 
    elseif($constellation=="Breeze Breakup") echo $BreezeMRBBreakup; 
    else echo $constellation; 

?> 

</p> 

</div> 


<div id="map3d"></div> 


<?php 

    // Retrieve the data from MySQL 
    $query2 = "SELECT * FROM $arr[$constellation] ORDER BY id ASC"; 
    $data2 = mysqli_query($dbc, $query2); 


    // Loop through the array of data, formatting it as HTML 
    echo '<table class="tablecontent">'; 

    echo '<tr><td colspan="11" class="constellationheader">' . $constellation . '</td></tr>'; 
    echo '<tr><th width="20px" class="headerinfo"><strong>ID:</strong></th>'; 
    echo '<th width="180px" class="headerinfo"><strong>Satellite:</strong></th>'; 
    echo '<th class="headerinfo"><strong>Mission:</strong></th>'; 
    echo '<th class="headerinfo"><strong>Epoch Time:</strong></th>'; 
    echo '<th width="160px" class="headerinfo"><strong>GMT:</strong></th>'; 
    echo '<th class="headerinfo"><strong>Inclination:</strong></th>'; 
    echo '<th class="headerinfo"><strong>RAAN:</strong></th>'; 
    echo '<th class="headerinfo"><strong>Eccentricity:</strong></th>'; 
    echo '<th width="120px" class="headerinfo"><strong>Argument of Perigee:</strong></th>'; 
    echo '<th class="headerinfo"><strong>Mean Anomaly:</strong></th>'; 
    echo '<th class="headerinfo"><strong>Mean Motion:</strong></th></tr>'; 

    $i = 0; 
    while ($row = mysqli_fetch_array($data2)) { 
    // Display the satellite data 
    if ($i == 0) { 

    echo '<tr><td align="center" width="20px" class="sat">' . $row['id'] . '</td>'; 
    echo '<td width="180px" class="sat">' . $row['satellite'] . '</td>'; 
    echo '<td class="sat">' . $row['mission'] . '</td>'; 
    echo '<td class="sat">' . $row['epochTime'] . '</td>'; 
    echo '<td width="160px" class="sat">' . $row['gmtTime'] . '</td>'; 
    echo '<td class="sat">' . $row['inclination'] . ' deg' . '</td>'; 
    echo '<td class="sat">' . $row['raan'] . ' deg' . '</td>'; 
    echo '<td class="sat">' . $row['eccentricity'] . '</td>'; 
    echo '<td width="120px" class="sat">' . $row['argOfPerigee'] . ' deg' . '</td>'; 
    echo '<td class="sat">' . $row['meanAnomaly'] . ' deg' . '</td>'; 
    echo '<td class="sat">' . $row['meanMotion'] . ' rev/day' . '</td></tr>'; 

    } 

    } 

    echo '</table>'; 

    mysqli_close($dbc); 

?> 

任何幫助,將不勝感激。我對這一切都不是很有經驗,但我很高興學習。

回答

0

這段代碼有點難以閱讀,並且使用數組來跟蹤所有變量,您會從中獲益良多。

例如,在你的「星座查詢」下拉菜單中,你可以使用類似以下內容:

$constellations = array('Last 30 Days', 'Space Stations', 'etc..'); 
foreach ($constellations as $constellation) { 
    $selected = null; 
    if ($_POST['constellation'] == $constellation) { 
    $selected = ' selected'; 
    } 
    echo "<option value=\"$constellation\" $selected>$constellation</option>"; 
} 

至於你的實際問題。我注意到你的「select」(id =「constellation」)裏有一個「/ form」標籤。我個人更喜歡把「表格」標籤放在「表格」之外。我的猜測是,如果你能夠用一些數組清理代碼,你可能會發現像這樣的其他小錯誤,阻止你的表單工作。希望這可以幫助!

+0

是的,我正在試驗

標籤,看看是否有任何改變,如果我在表格中包含兩個表。它沒有任何意義,但我沒有收到任何錯誤,所以我暫時保持這種狀態。我會設置它的權利。至於陣列去,我設法得到第一個查詢來處理它,但我會看看其他選項來改善代碼。謝謝您的幫助。 – 2013-03-16 19:38:45

+0

當然,也許你可以嘗試清理代碼並重新發布,如果它仍然無法正常工作。至少可以讓每個人都更容易看到問題。 – Steven 2013-03-17 01:49:48

相關問題