Philip Chow
Philip Chow

Reputation: 73

PHP Looping Data

I want to make the first row 'update' button link to A page and second row 'update' button link to B page, but when the PHP function loops it keep showing that both row 'update' button link to the same page.

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

Can someone help me to solve this problem? I'm quite new to programming.

Upvotes: 0

Views: 72

Answers (1)

PRANAV
PRANAV

Reputation: 629

You need to pass variable in anchor of that button to redirect on page where page name will be dependent of $i's value.

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

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

Upvotes: 1

Related Questions