问题描述:

For the first page, I want to set the column 'content control' to be button 'update or delete' because it already got data for the particular row. While for the second page, I want to set the column 'content control' to be button 'add or update or delete' as it don't have data along with the row yet. The problem I faced is how to set the first page to contain button 'update or delete' while for the second page to contain button 'add or update or delete'.

<?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>

网友答案:

Your code is unclear you are missing pagination display part ... but anyway with current placed code i can suggest you this simplified algorithm, with following optimizations

First of all you should create add_station.php and update_station.php files which are expect from GET $_GET['id'] pram and then display your add or edit form fir data based on that id, we are going to use such links in html add_station.php?id=.$ID instead writing many switch and creating many duplicated add_station_update1..10.php files

<?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 } ?>

also my suggestion to you do not display or word between buttons it is anyway clear for user that he can press or on update or on delete ...

if something from above code unclear just ask

相关阅读:
Top