如何将函数类中的单个值返回到foreach循环中

时间:2015-07-25 04:27:18

标签: php

我真的需要帮助这个,我是这个PHP oop的新手。我的问题在这里我只能在我桌子的一行显示数据。 这是我的函数类

<?php

include('connection.php'); 

class guestDetails{

    public function guest($id){

            $query = mysql_query("SELECT * FROM tb_member WHERE memberID = '{$id}' ");
            $row = mysql_fetch_array($query);

            return $row;


    }
}
?>

这是我foreach循环的地方:

      <table class="table table-bordered" id="table_id">
        <thead>
          <tr>
            <th>Booking No.</th>
            <th>Date</th>
            <th>RoomType</th>
            <th>Guest</th>
            <th>Status</th>
            <th>Payment</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <?php
            $lists = new crudBooking;
                    $list = $lists->viewBooking();
            $rows = new guestDetails;
            foreach($list as $key => $value){             
            $id = $value['reserveID'];

            $from = new DateTime( $value['arrival'] );
            $to = new DateTime( $value['departure'] );
          ?>
          <tr>
            <td><?php echo $value['reserveID']; ?></td>
            <td><?php echo $from->format( 'F d, Y' ) . " - " . $to->format( 'F d, Y' ); ?></td>
            <td><?php echo $value['category_id']; ?></td>
            <td>
              <?php 

                $row = $rows->guest($value['reserveID']);
                echo $row['firstname'] . " " . $row['lastname'] . "<br /><a href='#'>" . $row['email'] . "</a>"; 
              ?>
            </td>
            <td><?php echo $value['status']; ?></td>
            <td>to be edited</td>
            <td style="text-align: center;">
              <!-- Split button -->
                <div class="btn-group">
                  <button type="button" class="btn btn-default btn-sm">Action</button>
                    <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      <span class="glyphicon glyphicon-asterisk"></span>
                      <span class="sr-only">Toggle Dropdown</span>
                    </button>
                  <ul class="dropdown-menu">
                    <li><a href="<?php echo $value['reserveID']; ?>" data-toggle="modal" data-target="#editProduct<?php echo $value['reserveID']; ?>">Check-in</a></li>
                    <li><a href="<?php echo $value['reserveID']; ?>" data-toggle="modal" data-target="#deleteProduct<?php echo $value['reserveID']; ?>">Check-out</a></li>                             
                    <li><a href="<?php echo $value['reserveID']; ?>" data-toggle="modal" data-target="#deleteProduct<?php echo $value['reserveID']; ?>">Cancel</a></li>
                    <li><a href="<?php echo $value['reserveID']; ?>" data-toggle="modal" data-target="#deleteProduct<?php echo $value['reserveID']; ?>">More Details</a></li>
                  </ul>
                </div>
            </td>
            <?php include('includes/modals.php'); ?>
          </tr>
          <?php }  ?>
        </tbody>
      </table>

这里的问题是它只返回我表格的第一行。 这是我调用guest() class:

的返回值的代码
                <td>
              <?php 

                $row = $rows->guest($value['reserveID']);
                echo $row['firstname'] . " " . $row['lastname'] . "<br /><a href='#'>" . $row['email'] . "</a>"; 
              ?>
            </td>

这是表格 enter image description here

1 个答案:

答案 0 :(得分:2)

你确定tb_member.memberID有所有reserveID,做类似

的事情
 $row = $rows->guest($value['reserveID']);
 if(!$row){
  echo "NO this member,reserveID:".$value['reserveID'];
  }else{
     echo $row['firstname'] . " " . $row['lastname'] . "<br /><a href='#'>" . $row['email'] . "</a>";
 }

如果reserveID不是memberID

 $row = $rows->guest($value['YOUR MENBER ID COLUME NAME']);