按钮

时间:2015-12-15 07:22:37

标签: php

enter image description here enter image description here

对于第一页,我想设置列'内容控件'按钮'更新或删除'因为它已经获得了特定行的数据。对于第二页,我想设置列'内容控件'按钮'添加或更新或删除'因为它还没有数据和行。我遇到的问题是如何设置第一页包含按钮'更新或删除'而第二页包含按钮'添加或更新或删除'。

<?php
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("smart_train",$conn);
    $result = mysql_query("SELECT * FROM station");
  ?>
  <?php
        $i=0;
        while($row = mysql_fetch_array($result)) {
        $i%2==0;
        switch ($i) {
             case "0":
                 $classname="1Row";
                 $goesToPage='add_station_update1.php';
                 break;
             case "1":
                 $classname="2Row";
                 $goesToPage='add_station_update2.php';
                 break;
             case "2":
                 $classname="3Row";
                 $goesToPage='add_station_update3.php';
                 break;
             case "3":
                 $classname="4Row";
                 $goesToPage='add_station_update4.php';
                 break;
             case "4":
                 $classname="5Row";
                 $goesToPage='add_station_update5.php';
                 break;
             case "6":
                 $classname="6Row";
                 $goesToPage='add_station_update6.php';
                 break;
             case "6":
                 $classname="7Row";
                 $goesToPage='add_station_update7.php';
                 break;
             case "7":
                 $classname="8Row";
                 $goesToPage='add_station_update8.php';
                 break;
             case "8":
                 $classname="9Row";
                 $goesToPage='add_station_update9.php';
                 break;
             case "9":
                 $classname="10Row";
                 $goesToPage='add_station_update10.php';
                 break;
        }
   ?>
  <tr>
    <td> <?php echo $row["id"];?> </td>
    <td> <?php echo $row["thailand"];?> </td>
    <td> <?php echo $row["malaysia"];?> </td>
    <td>
      <div class="btn-group" class="<?php if(isset($classname)) echo $classname;?>">
          <a href="<?php echo $goesToPage; ?>">
          <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>
      <div class="btn-group" class="<?php if(isset($addclass)) echo $addclass;?>">
          <a href="<?php echo $goesToPage; ?>">
          <button onclick="myFunction()" type="button" class="btn btn-warning">Add</button>
          </a>
          <a><button type="button" class="btn btn-default" id="content_control_or">or</button></a>
          <a href="<?php echo $goesToPage; ?>">
          <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>
      <?php
        $i++;
        }
      ?>
    </td>
  </tr>

1 个答案:

答案 0 :(得分:1)

您的代码不清楚您是否缺少分页显示部分...但无论如何使用当前放置的代码我可以建议您使用此简化算法,并进行以下优化

首先,您应该创建期望来自GET add_station.php pram的update_station.php$_GET['id']个文件,然后根据该ID显示您的添加或编辑表单数据,我们将继续在html add_station.php?id=。$ ID中使用此类链接,而不是编写许多switch并创建许多重复的add_station_update1..10.php个文件

<?php
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("smart_train",$conn);
    $result = mysql_query("SELECT * FROM station");
?>

<?php
    while($row = mysql_fetch_array($result)) {
   ?>
  <tr>
    <td> <?php echo $row["id"];?> </td>
    <td> <?php echo $row["thailand"];?> </td>
    <td> <?php echo $row["malaysia"];?> </td>
    <td>
      <div class="btn-group" >
          <?php if(empty($row["thailand"]) && empty($row["malaysia"]) ){?>
              <a href="add_station.php?id=<?php echo $row["id"];?>">
                  <button onclick="myFunction()" type="button" class="btn btn-warning">Add</button>
              </a>
          <?php }?>
          <a href="update_station.php?id=<?php echo $row["id"];?>">
              <button onclick="myFunction()" type="button" class="btn btn-warning">Update</button>
          </a>
          <a><button type="button" class="btn btn-warning">Delete</button></a>
          <?php if(isset($message)) { echo $message; } ?>
      </div>
    </td>
  </tr>
 <?php } ?>

另外,我建议您不要在按钮之间显示or字,无论如何,用户可以按下或updatedelete上显示...

如果上面代码中的某些内容不清楚,请询问