簡體   English   中英

我正在嘗試將數據從數據庫中提取到選擇框中

[英]I'm trying to pull data from database into selectboxes

我正在嘗試將數據從數據庫中提取到選擇框中,但是當數據被拉出時,它會進入一個'td',而不是進入單獨的td。 我正在努力實現如下所示的結果

在此輸入圖像描述


但我一直得到這個結果

在此輸入圖像描述


這是我的代碼


<?php
$data_array = array();
$result2 = mysql_query("SELECT * FROM `firefightersonscene` 
                        JOIN `firefighterinfo` ON `firefightersonscene`.`FireFighterInfo_fighterID` = `firefighterinfo`.`fighterID` 
                        JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID`=`stationlocation`.`locationID` 
                        WHERE `IncidenceOfFire_incidentID`='$getIncID' ORDER BY `firstName`");

if(mysql_num_rows($result2) > 0)
{    
    while($rows2 = mysql_fetch_object($result2))
    {
        $data_array[] = $rows2;
    }   
}
?>
<form action="core_viewfireoccurrence.php?incidentID=<?php echo $rows->incidentID; ?>" method="post" class="view_occurrence_form">
<table id="myTable">
            <thead>
                <tr>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="count">1</td>
                    <td>
                            <?php 
                            foreach($data_array as $rows2):
                            $fighterID = $rows2->FireFighterInfo_fighterID; 
                            $results = mysql_query("SELECT `fighterID`, `firstName`, `middleName`, `lastName`, `stationlocation`.`exactlocation` 
                                                    FROM `firefighterinfo` 
                                                    JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID`=`stationlocation`.`locationID` ORDER BY `firstName`");               
                            echo '<select name="fireman[]" required><option value=""></option>';
                            while($row = mysql_fetch_array($results))
                            {
                                if($row['fighterID'] == $fighterID)
                                    echo '<option selected>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';
                                else
                                    echo '<option>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';

                            }// end while
                            echo '</select><br>';
                        endforeach;
                        ?> 
                    </td>
                    <td>
                        <input type="button" value="X" class="removeVar"/>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <input type="button" id="addVar" value="Add Item"/>
                </tr>
            </tfoot>
            </table>
</form>

JS代碼


<script type="text/javascript">
    $('form').on('click', '.removeVar', function(){
    $(this).closest('tr').remove();
    $('.count').each(function(i){
      $(this).text(i + 1);
    });
});

//add a new node
$('#addVar').on('click', function(){
    var varCount = $('#myTable tr').length - 1;
    $node = ['<tr>',
    '<td class="count">'+varCount+'</td>',
    '<td><select name="fireman[]" class="ctlGroup" required>',
      '<option value=""></option>',
      '<?php require("php/fireman_list.php"); ?>',
    '</select></td>',
    '<td><input type="button" value="X" class="removeVar"/>',
    '</td></tr>'].join('\n');
    $('#myTable > tbody:last').append($node);
});
</script>

您需要將整個表行放在循環中。 您還需要添加一個變量來為您計算行號。

<?php 
$row =1;
foreach($data_array as $rows2):
?>
<tr>
    <td class="count"><?php echo $row; ?></td>
    <td>
        <?php    
             $fighterID = $rows2->FireFighterInfo_fighterID; 
            $results = mysql_query("SELECT `fighterID`, `firstName`, `middleName`, `lastName`, `stationlocation`.`exactlocation` 
                                    FROM `firefighterinfo` 
                                    JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID`=`stationlocation`.`locationID` ORDER BY `firstName`");               
            echo '<select name="fireman[]" required><option value=""></option>';
            while($row = mysql_fetch_array($results))
            {
                if($row['fighterID'] == $fighterID)
                    echo '<option selected>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';
                else
                    echo '<option>'. $row['firstName']." ".$row['middleName']." ".$row['lastName'].", ".$row['exactlocation'].'</option>';

            }// end while
            echo '</select><br>';

        ?> 
    </td>
    <td>
        <input type="button" value="X" class="removeVar"/>
    </td>
</tr>
<?php
    $row++;
    endforeach;
?>    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM