soldiershin
soldiershin

Reputation: 1620

select that particular id when button is pressed in php

I am trying to send the id from one page to other and i have achieved that but how do i make sure that when the show is pressed of a particular column only that id is sent.Here is my code. This is index.php.It displays the rows and a show button against each row.

    <?php session_start(); ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
    </head>

    <body>
    <div class="container">
            <div class="row">
                <h3>Restaurant Categories</h3>
            </div>
            <div class="row">
            <p> <a href="create_categories.php" class="btn btn-success">Create</a> </p>
                <table class="table table-striped table-bordered">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>Name</th>
                      <th>Description</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
                   include 'database.php';
                   $pdo = Database::connect();
                   $sql = 'SELECT * FROM Categories';
                   $result = $pdo->query($sql);
                   foreach ($pdo->query($sql) as $row) {
                            echo '<tr>';
                            echo '<td>'. $row['C_id'] . '</td>';
                            echo '<td>'. $row['C_name'] . '</td>';
                            echo '<td>'. $row['C_description'] . '</td>';
                            echo '<td><a class="btn" href="show_items.php">Shitems</a>     </td>';

                            $_SESSION["id"] = $row['C_id'];     <-----I am saving the id here.But it only saves the latest id
                            echo $_SESSION["id"];
                            echo '</tr>';
                   }
                   Database::disconnect();
                  ?>
                  </tbody>
            </table>
        </div>
       </div> <!-- /container -->
    </body>
  </html>

Upvotes: 0

Views: 547

Answers (3)

Fortran
Fortran

Reputation: 593

First you need from form around button. But you can make this with ajax(if you don't redirect the page) or link

Upvotes: 0

geggleto
geggleto

Reputation: 2625

Simple using _GET...

<a class="btn" href="show_items.php">

<a class="btn" href="show_items.php?id=<?=$row['C_id']?>">

Then you can access it via... _GET['id']

Upvotes: 0

Marc B
Marc B

Reputation: 360862

Just embed the ID in the url as a query string:

echo '<td><a class="btn" href="show_items.php?id=' . $row['C_id'] . '">Shitems</a>     </td>';
                                             ^^^^^^^^^^^^^^^^^^^^ 

Upvotes: 2

Related Questions