PHP循环数据

时间:2015-12-14 06:13:18

标签: php

我想让第一行'更新'按钮链接到A页面和第二行'更新'按钮链接到B页面,但是当PHP函数循环时,它会一直显示两行'更新'按钮链接到同一页面。

<?php
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("smart_train",$conn);
    $result = mysql_query("SELECT * FROM detail");
>
<table id="example1" class="table table-bordered table-striped">
    <thead>
          <tr>
             <th>Station Place</th>
             <th>Line1</th>
             <th>Line2</th>
             <th>Line3</th>
             <th>Station Code</th>
             <th>Opening Hours</th>
             <th>Parking</th>
             <th>Content Control</th>
          </tr>
    </thead>

<?php
                        $i=0;
                        while($row = mysql_fetch_array($result)) {
                        if($i%2==0)
                        $classname="evenRow";
                        else
                        $classname="oddRow";
                   ?>
                  <tr class="<?php if(isset($classname)) echo $classname;?>">   
                    <td> <?php echo $row["place"]; ?> </td>
                    <td id="station_line1"> <?php echo $row["line1"]; ?> </td>
                    <td id="station_line2"> <?php echo $row["line2"]; ?> </td>
                    <td id="station_line3"> <?php echo $row["line3"]; ?> </td>
                    <td id="station_code"> <?php echo $row["code"]; ?> </td>
                    <td id="station_opening"> <?php echo $row["opening"]; ?> </td>
                    <td id="station_parking"> <?php echo $row["parking"]; ?> </td>
                    <td>
                        <div class="btn-group">
                          <a href="station_detail _update1.php">
                          <button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
                          </a>
                          <a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
                          <a><button type="button" class="btn btn-warning">Delete</button></a>
                          <?php if(isset($message)) { echo $message; } ?>
                      </div>
                    </td>
                  </tr>
                   <?php
                    $i++;
                    }
                   ?>
</tbody>

enter image description here

有人可以帮我解决这个问题吗?我对编程很陌生。

1 个答案:

答案 0 :(得分:1)

您需要在该按钮的锚点中传递变量,以在页面上重定向,其中页面名称将取决于$i的值。

if($i%2==0){
{ 
   $classname="evenRow";
   $goesToPage='Page1.php';
 }
else{
   $classname="oddRow";
   $goesToPage='Page2.php';
}

     <a href="<?php echo $goToPage; ?>">