fbpx
Friday, December 8, 2023
Hire us on Freelancer.com for web development service
HomeProjectsPhpHow to Create Active and Inactive Status in PHP

How to Create Active and Inactive Status in PHP

Managing the activation and deactivation of status in PHP is a crucial aspect of web development. It allows you to control the visibility and accessibility of certain features or content on your website. In this guide, we will walk you through the process of How to Active Deactive Status in PHP. Whether you are working on user accounts, product listings, or any other dynamic content, this tutorial will provide you with the necessary insights.

What is the Active Inactive Status in PHP?

Status activation and deactivation refer to the process of enabling or disabling specific functionalities or content within your PHP application. It is often used to control user accounts, toggle features, or manage the visibility of items in a database.

Why is Status Activation and Deactivation Important?

This feature is vital for maintaining the security and functionality of your web application. It allows you to grant or restrict access, as well as manage user privileges effectively.

What Are the Use Cases for Active Deactive Status?

You can use status activation and deactivation for various purposes, such as controlling user accounts, enabling or disabling product listings, moderating comments, and more.

Implementing Active Deactive Status in PHP

To demonstrate the implementation, we’ll create a simple user management system.

User Table Setup

First, create a ‘user’ table in your database with columns like ‘id,’ ‘username,’ ’email,’ and ‘status.’

PHP Code Example

Here’s a PHP code snippet to activate and deactivate a user’s status:

<?php

    include('function.php');
    
    if(isset($_GET['type']) && $_GET['type']!='')

    {

        $operation = $_GET['operation'];
        
        $type = $_GET['type'];

        $id = $_GET['id'];

        if($type=='status')

        {
            
            if($operation=='active')

            {
                $status='1';
            }else

            {
                $status='0';
            }

            $sql = "UPDATE user SET status = '$status' WHERE id = '$id'";

            $query = mysqli_query($connection, $sql);

            if ($query) 
            {
                $_SESSION['Success'] = "Status Updated Successfully";

                header('Location: index.php');
            }else

            {
                $_SESSION['error'] = "Something went wrong";

                header('Location: Gallery');

            }
        
        }

        if($type=='delete')

        {
            
            
            $sql = "DELETE FROM gallery WHERE id = '$id'";

            $query = mysqli_query($connection, $sql);

            if ($query) 
            {
                $_SESSION['Success'] = "Deleted Successfully";

                header('Location: Gallery');
            }else

            {
                $_SESSION['error'] = "Something went wrong";

                header('Location: Gallery');

            }
        
        }
        
        
    }


?>

 

User Interface: Create a user-friendly interface (e.g., buttons, checkboxes, or switches) to allow users or administrators to activate or deactivate status.

<?php 
    
    include('function.php');

?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Active Deactive Status in PHP</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    
 
</head>
<body>

    <section class="mt-5">

        <div class="container">

            <?php success(); ?>
            
            <div class="row">
 
            	<div class="col-lg-12 col-md-12 col-sm-12 shadow shadow-lg p-5">

                	<div class="card">

                  		<div class="card-header">
                                            
                    		<h3 class="text-center">Active Deactive Status in PHP</h3>


                  		</div>

                  		<div class="card-body">
                                            
                        	<table id="myTable" class="table  table-responsive">

                          		<thead>
                            		
                            		<tr>

                            			<th>#</th>
                                                          
                            			<th>Name</th>                                       
                              	
                              			<th>Status</th>
                                    
                            		</tr>
                          		
                          		</thead>
                                                        
                          		<tbody>

            					<?php

            
            						$no = 0;

                					$sql = "SELECT * FROM user";
                                    $query = mysqli_query($connection, $sql);

                                    while ($row = mysqli_fetch_assoc($query))

                					{?>

                						<?php $no++; ?>

                          			<tr>

                          				<td><?php echo $no; ?></td>
                            
                            			<td><?php echo $row['name']; ?></td>

                            			<td>
                                        
                                            <?php

                                            	if($row['status']==1)

                                          	{?>

                                                <a href="update.php?type=status&operation=deactive&id=<?php echo $row['id'];?>" class="btn btn-success">Active</a>
                                                                           
                                          	<?php    }else{?>
                                                                                        
                                                <a href="update.php?type=status&operation=active&id=<?php echo $row['id'];?>" class="btn btn-primary">Inactive</a>

                                          	<?php    } ?>
                                        
                                        </td>                                    

                                                           
                                    </tr>
                                                    
                                <?php    } ?>  


                                                                
                                                                

                       	 		</tbody>
                                                            
                  			</table> 

              
                                            

            			</div> <!-- end card body-->
          			
          			</div> <!-- end card -->
        		
        		</div><!-- end col-->
      
      		</div>

        </div>

   
    </section>

</body>
</html>

 

Handling Status in Your Application

Now, based on the user’s status, you can control their access and display appropriate messages or content.

For a detailed guide on how to upload an image in a database with PHP, you can refer to this comprehensive tutorial.

Conclusion

In PHP development, managing Active Deactive Status in PHP is a fundamental aspect of building dynamic web applications. By following the steps outlined in this guide, you can efficiently control status, enhance user experiences, and maintain data integrity.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Hire Us

Categories