如何使用php中的select语句循环遍历数组

时间:2016-12-13 06:59:14

标签: php arrays loops select

这是我的代码。我想基于数据库表中的itemID打印输出。如何使用数组和循环打印每个itemID的每个输出?这样做的最佳方式是什么?

<?php
$result=mysql_query("select * from item where itemID='1'") or die(mysql_error());
$get_row=mysql_fetch_array($result);
?>
<?php echo $get_row['No']; ?>.<?php echo $get_row['Item']; ?> :<?php echo $get_row['Unit']; ?> unit

<br>
</br>
<center>
    <table border="1" width="1000" height="80">
        <thead>
            <tr>
                <th>Nama</th>
                 <th>Jenama</th>
                 <th>Model</th>
                 <th>No_Siri</th>
                 <th>Tagging</th>
                 <th>Tarikh_Diterima</th>
                 <th>Remarks</th>
            </tr>
            </thead>
            <tbody>
                <?php
                $emp_query=mysql_query("select * from service_record where itemID='1' order by Nama");
                while($row=mysql_fetch_array($emp_query)){
                     $id=$row['service_record_id'];
                     $emp=$row['itemID'];
                ?>
                <tr class="del<?php echo $id ?>">
                    <?php
                    $result=mysql_query("select * from item  where itemID='1'")or die(mysql_error());
                    $row_emp=mysql_fetch_array($result);
                    ?>
                     <td><?php echo $row['Nama']; ?></td>
                      <td><?php echo $row['Jenama']; ?></td>
                      <td><?php echo $row['Model']; ?></td>
                      <td><?php echo $row['No_Siri']; ?></td>
                      <td><?php echo $row['Tagging']; ?></td>
                      <td><?php echo $row['Tarikh_Diterima']; ?></td>
                      <td><?php echo $row['Remarks']; ?></td>
                    </tr>


    

            <?php $result=mysql_query("select * from item where itemID='2'")or die (mysql_error());
        $get_row=mysql_fetch_array($result);
    ?>

            <?php echo $get_row['No']; ?>.<?php echo $get_row['Item']; ?> :<?php echo $get_row['Unit']; ?> unit


    <br>
    </br>

    <center>
        <table border="1" width="1000" height="80">



            <thead>
                <tr>


                    <th>Nama</th>
                    <th>Jenama</th>
                    <th>Model</th>
                    <th>No_Siri</th>
                    <th>Tagging</th>
                    <th>Tarikh_Diterima</th>
                    <th>Remarks</th>

                </tr>
            </thead>
            <tbody>

                <?php $emp_query=mysql_query("select * from service_record where itemID='2' order by Nama");
                    while($row=mysql_fetch_array($emp_query)){ $id=$row['service_record_id']; $emp=$row['itemID'];  ?>

                    <tr class="del<?php echo $id ?>">
                        <?php
                            $result=mysql_query("select * from item  where itemID='2'")or die(mysql_error());
                            $row_emp=mysql_fetch_array($result);
                        ?>
                        <td><?php echo $row['Nama']; ?></td>
                        <td><?php echo $row['Jenama']; ?></td>
                        <td><?php echo $row['Model']; ?></td>
                        <td><?php echo $row['No_Siri']; ?></td>
                        <td><?php echo $row['Tagging']; ?></td>
                        <td><?php echo $row['Tarikh_Diterima']; ?></td>
                        <td><?php echo $row['Remarks']; ?></td>




                    </tr>
                    <?php }?>
            </tbody>
        </table>
    </center>

          <br>
</br>

  <br>
</br>

现在,我根据itemID = 1,itemID = 2依旧编写代码,依此类推。我不知道如何使输出显示数据库中的所有itemID而不逐一写入

this is the example of picture that i want the output to display

1 个答案:

答案 0 :(得分:0)

如果你只有一个行数据而不是你可以保持在里面,那么你需要将它保持在循环之外。

               <tr>
                while($row=mysql_fetch_array($emp_query))
                { 
                $id=$row['service_record_id']; 
                $emp=$row['itemID'];
                // use itemID='$emp' instead of itemID='1'
                    $result=mysql_query("select * from item  where itemID='$emp'")or die(mysql_error());
                    $row_emp=mysql_fetch_array($result);
                    ?>
                     <td><?php echo $row['Nama']; ?></td>
                      <td><?php echo $row['Jenama']; ?></td>
                      <td><?php echo $row['Model']; ?></td>
                      <td><?php echo $row['No_Siri']; ?></td>
                      <td><?php echo $row['Tagging']; ?></td>
                      <td><?php echo $row['Tarikh_Diterima']; ?></td>
                      <td><?php echo $row['Remarks']; ?></td>

                <?php } ?>  
                </tr>