首页 > 解决方案 > 如何在循环外显示数组变量

问题描述

你好我有这样的代码

                <div class="table-wrapper-2">

                    <!--Table-->
                    <table class="table table-striped">
                        <thead class="mdb-color lighten-4">
                            <tr>
                                <th>#</th>
                                <th class="th-lg">Name</th>
                                <th class="th-lg">Surname</th>
                                <th class="th-lg">Country</th>
                                <th class="th-lg">City</th>
                                <th class="th-lg">Position</th>
                                <th class="th-lg">Age</th>
                                <th class="th-lg">Position</th>
                                <th class="th-lg">Position</th>


                            </tr>
                        </thead>
                        <tbody>
                        <?php
                        $a='TAJUL';
                           $stid = oci_parse($conn, 'SELECT def_usr,problem,solution,def_date,closed_date FROM hd_report  where def_usr=:num order by def_date DESC');
                       oci_bind_by_name($stid, ":num", $a);
                          oci_execute($stid);
                       $count = 0;

                                 while (($row = oci_fetch_row($stid)) != false) {
                                   $def_usr=$row[0];
                                   $problem=$row[1];
                                   $solution=$row[2];
                                   $def_date=$row[3];
                                   $closed_date=$row[4];
                                   $count=$count+1;
                             ?>
                            <tr>
                                <th scope="row"><?php echo $count ?></th>
                                <td><?php echo $def_usr ?></td>
                                <td><?php echo $def_date ?></td>
                                <td><?php echo $closed_date ?></td>
                                <td><?php echo $problem ?></td>
                                            <td>
                                            <button type="button" href="#myModalDelete"
                                                    data-toggle="modal" class="btn btn-danger"
                                                    data-problem="<?php $problem?>">
                                                    <span class="glyphicon glyphicon-trash"></span>Detail
                                            </button>
                                            </td>           


        <?php
        }
        ?>      
                            </tr>

                        </tbody>

                </table>
                    <!--Table-->

                </div>

            </div>
        </div>

    <!-- Start Modal for Delete -->
        <div id="myModalDelete" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                        <h4 class="modal-title">Alert message !!!</h4>
                    </div>

                    <div class="modal-body">

                        <!-- The form is placed inside the body of modal -->
                        <form method="post" action="view_detailquestion">

                            <h3>Are you sure you want to View this question ?</h3>

                            <div class="form-group">
                                <label>ID</label> <input type="text" class="form-control"
                                         name="problem" value="<?php echo $problem?>" readonly>
                            </div>

                            <button type="submit" class="btn btn-danger" name="action"
                                value="view">view</button>

                        </form>

                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>
    </div>

我的问题是我想在循环外回显 $problem。当我这样做时,它只包含 1 个值。所以我想有什么解决方案可以让我在括号外解决 $problem。就像我们都可以在启动模式中看到的那样对于删除,我尝试回显 $problem 但结果很喜欢我说只携带 1 个值而不是数组......所以我希望你们可以成为我的向导,谢谢......

标签: php

解决方案


像这样:

$problem=array();
while (($row = oci_fetch_row($stid)) != false) {
  $problem[]=$row[1];
}

foreach($var in $problem){
  echo $var . "<br />"
}

推荐阅读