Ejemplo básico para seleccionar varias filas

Ejemplo básico para seleccionar varias filas

PHP

Se trata de una tabla con varias filas, las cuales podremos seleccionar para eliminar.

Puede resultar muy útil para realizar operaciones de edición o eliminación masiva con registros en un base de datos.

Estructura de ficheros

  • [re-directory]base
    • [re-file]definitions.php
    • [re-file]delete.php
    • [re-file]index.php

Contenido de cada fichero

definitions.php

<?php 
/* Data set */
$products = array(
	0 => array(
		'id' => 1,
		'name' => 'T shirt',
		'price' => '14.00€'
	),
	1 => array(
		'id' => 2,
		'name' => 'Pants',
		'price' => '9.95€'
	),
	2 => array(
		'id' => 3,
		'name' => 'Hat',
		'price' => '19.95€'
	),
	3 => array(
		'id' => 4,
		'name' => 'Flip flops',
		'price' => '5.00€'
	),
	4 => array(
		'id' => 5,
		'name' => 'Blouse',
		'price' => '4.95€'
	),

);
?>

delete.php

<?php
/* Delete */
if(isset($_POST['to_delete']) AND is_array($_POST['to_delete']) AND count($_POST['to_delete'])>0){
	$to_delete_msg = '';
	foreach($products as $pos => $product){
		if(in_array($product['id'], $_POST['to_delete'])){
			$to_delete_msg .= '<div class="alert alert-info"> Deleting #'.$product["id"].' with name ['.$product["name"].']</div>';
			unset($products[$pos]);
		}
	}
}
?>

index.php

<?php
require_once 'definitions.php';
require_once 'delete.php';
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Multiple rows operation</title>
	<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css"/>
	<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#checkAllRows").click(function(){
				if($(this).is(':checked')){
					$('#exampleTable input[type=checkbox]').prop('checked', true);
				}else{
					$('#exampleTable input[type=checkbox]').prop('checked', false);
				}
			}); 
		});
	</script>
</head>
<body>
	<div class="container">
		<div class="row mt-5">
			<div class="col-md-12">
				<h1>Basic example for selecting multiple rows</h1>
			</div>
			<div class="col-md-12">
				<?php if(isset($to_delete_msg)) echo $to_delete_msg; ?>
			</div>
			<div class="col-md-12">
				<form action="#" method="POST">
					<table id="exampleTable" class="table table-bordered">
						<thead>
							<tr>
								<th></th>
								<th>ID</th>
								<th>Name</th>
								<th>Price</th>
							</tr>
						</thead>
						<tbody>
							<?php 
							foreach ($products as $pos => $product) {
								?>
								<tr>
									<td><input type="checkbox" name="to_delete[]" value="<?php echo $product['id']; ?>"></td>
									<td><?php echo $product['id']; ?></td>
									<td><?php echo $product['name']; ?></td>
									<td><?php echo $product['price']; ?></td>
								</tr>
								<?php
							}
							?>			
						</tbody>
					</table>
					<p><input type="checkbox" id="checkAllRows" class="btn btn-info"/> Check all</p>
					<input type="submit" value="Delete rows"/>
				</form>
			</div>
		</div>
	</div>
</body>
</html>

Leave a reply