简体   繁体   English

我正在尝试将数据从数据库中提取到选择框中

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

I'm trying to pull data from database into selectboxes, but when the data is pulled it goes into one 'td' and not into separate td's. 我正在尝试将数据从数据库中提取到选择框中,但是当数据被拉出时,它会进入一个'td',而不是进入单独的td。 I'm trying to achieve result as shown below 我正在努力实现如下所示的结果

在此输入图像描述


but I keep getting this result 但我一直得到这个结果

在此输入图像描述


here is my code 这是我的代码


<?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 CODE 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>

You need to put your whole table row in the loop. 您需要将整个表行放在循环中。 You will also need to add a variable to count the row number for you. 您还需要添加一个变量来为您计算行号。

<?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.

相关问题 我正在尝试使用从 php 数据库中提取的一些数据作为可点击 SVG 图片(javascript)中的信息 - I'm trying to use some data i pulled from a php database as information in a clickable SVG picture (javascript) 我正在尝试使用javascript ajax函数绑定数据库中的数据,但是它不起作用 - i'm trying to bind data from database using javascript ajax function but it didn't work imacros,我正在尝试从网页中提取图像文件的名称 - imacros, I'm trying to pull the name of an image file from a web page 我正在尝试使用 Python web 刮板从定期更新该文本的页面中提取文本 - I'm trying to use a Python web scraper to pull text from a page that has that text update periodically 在选择框中更改数据angularjs - Change data in selectboxes angularjs 我很难从软件中的 web 页面访问数据 - I'm stumped trying to access data from a web page in software 我正在尝试使用来自 html 中的 api 的数据传递参数 - I'm trying to pass a parameter with data that comes from an api in the html 我正在尝试从React中的对象(API)提取数据 - I'm trying to extract Data from Object (API) in React 我正在尝试将从 API 收到的数据放入表中,但没有显示任何内容 - I'm trying to put the data I'm receiving from my API into a table but nothing is displayed 当我尝试通过在反应中使用 useEffect 从 API 获取数据时遇到问题 - I'm fetching a problem when I'm trying to fetch data from an API by using useEffect in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM