Activation Button

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Open sidebar

Bootstrap Activate/Deactivate Button

Grace Paul Nyingi

show me how can I create the bootstrap deactivate and activate button for user from database and
show me how can I make it works using php..

To create a Bootstrap deactivate and activate button for a user from a database, you can use HTML,
Bootstrap, and PHP code. Here is an example of how to do this:

HTML and Bootstrap code:

php

Copy code

<table class="table">

<thead>

<tr>

<th>ID</th>

<th>Name</th>

<th>Status</th>

<th>Action</th>

</tr>

</thead>

<tbody>

<?php

// Connect to the database and retrieve user information

$conn = mysqli_connect("localhost", "username", "password", "database_name");

$query = "SELECT * FROM users";


$result = mysqli_query($conn, $query);

// Loop through each user and display their information

while ($row = mysqli_fetch_array($result)) {

echo "<tr>";

echo "<td>" . $row['id'] . "</td>";

echo "<td>" . $row['name'] . "</td>";

// Display the current status of the user

if ($row['status'] == 'active') {

echo "<td><span class='badge bg-success'>Active</span></td>";

} else {

echo "<td><span class='badge bg-danger'>Inactive</span></td>";

// Display the activate/deactivate button

if ($row['status'] == 'active') {

echo "<td><a href='deactivate.php?id=" . $row['id'] . "' class='btn


btn-danger'>Deactivate</a></td>";

} else {

echo "<td><a href='activate.php?id=" . $row['id'] . "' class='btn btn-success'>Activate</a></td>";

echo "</tr>";

?>

</tbody>

</table>

In this code, we first create an HTML table with four columns: ID, Name, Status, and Action. We then use
PHP to connect to the database and retrieve information about all users. For each user, we display their
ID, Name, and current Status. We also include an Action column that displays a button to activate or
deactivate the user, depending on their current status.

PHP code for activating or deactivating a user:

To make the activate/deactivate button work, we need to create two separate PHP scripts: one to
activate a user and one to deactivate a user. Here is an example of what those scripts might look like:

activate.php:

php

Copy code

<?php

// Connect to the database

$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Retrieve the user ID from the URL parameter

$id = $_GET['id'];

// Update the user's status to "active"

$query = "UPDATE users SET status='active' WHERE id=$id";

mysqli_query($conn, $query);

// Redirect back to the page with the user table

header("Location: index.php");

?>

deactivate.php:

php
Copy code

<?php

// Connect to the database

$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Retrieve the user ID from the URL parameter

$id = $_GET['id'];

// Update the user's status to "inactive"

$query = "UPDATE users SET status='inactive' WHERE id=$id";

mysqli_query($conn, $query);

// Redirect back to the page with the user table

header("Location: index.php");

?>

In these scripts, we first connect to the database using mysqli_connect. We then retrieve the user ID
from the URL parameter using $_GET['id']. We use this ID to update the user's status in the database
using an

ChatGPT Feb 13 Version. Free Research Preview. Our goal is to make AI systems more natural and safe
to interact with. Your feedback will help us improve.

You might also like