Objetivo
Criar uma lista de compras temporária que permite adicionar, remover e exportar itens.
Estrutura de Arquivos
/compras/ ├── index.php ├── export.php └── style.css
1. CSS para Lista de Compras (style.css)
/* style.css - Lista de Compras - Design Moderno */
/* Reset e variáveis globais */
:root {
--primary: #6C63FF;
--primary-dark: #5a52d5;
--primary-light: #8b83ff;
--secondary: #FF6584;
--success: #2ecc71;
--danger: #e74c3c;
--warning: #f39c12;
--dark: #2d3436;
--gray: #636e72;
--light-gray: #dfe6e9;
--bg: #f0f2f5;
--white: #ffffff;
--shadow: 0 10px 30px rgba(108, 99, 255, 0.15);
--radius: 16px;
--transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
background: var(--bg);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
background-image:
radial-gradient(circle at 20% 50%, rgba(108, 99, 255, 0.05) 0%, transparent 50%),
radial-gradient(circle at 80% 50%, rgba(255, 101, 132, 0.05) 0%, transparent 50%);
}
.container {
max-width: 900px;
width: 100%;
background: var(--white);
border-radius: var(--radius);
padding: 40px;
box-shadow: var(--shadow);
position: relative;
overflow: hidden;
}
/* Decoração de fundo */
.container::before {
content: '';
position: absolute;
top: -50%;
right: -50%;
width: 100%;
height: 100%;
background: linear-gradient(135deg, rgba(108, 99, 255, 0.03), rgba(255, 101, 132, 0.03));
transform: rotate(45deg);
pointer-events: none;
}
/* Títulos */
h1 {
font-size: 2.2rem;
font-weight: 700;
color: var(--dark);
margin-bottom: 8px;
display: flex;
align-items: center;
gap: 12px;
}
h1 span {
background: linear-gradient(135deg, var(--primary), var(--secondary));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.subtitle {
color: var(--gray);
font-size: 0.95rem;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 2px solid var(--light-gray);
}
/* Barra de estatísticas */
.stats-bar {
display: flex;
gap: 20px;
margin-bottom: 25px;
flex-wrap: wrap;
}
.stat-item {
background: var(--bg);
padding: 12px 20px;
border-radius: 12px;
display: flex;
align-items: center;
gap: 10px;
font-size: 0.95rem;
color: var(--dark);
transition: var(--transition);
}
.stat-item:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}
.stat-item .number {
font-weight: 700;
font-size: 1.2rem;
color: var(--primary);
}
.stat-item .label {
color: var(--gray);
}
/* Formulário de adição */
.add-form {
display: flex;
gap: 12px;
margin-bottom: 30px;
background: var(--bg);
padding: 15px;
border-radius: var(--radius);
position: relative;
z-index: 1;
}
.add-form .form-group {
flex: 1;
margin: 0;
}
.add-form input {
width: 100%;
padding: 14px 18px;
border: 2px solid transparent;
border-radius: 10px;
font-size: 1rem;
transition: var(--transition);
background: var(--white);
color: var(--dark);
}
.add-form input:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 4px rgba(108, 99, 255, 0.1);
}
.add-form input::placeholder {
color: var(--gray);
opacity: 0.7;
}
.add-form .input-quantidade {
max-width: 100px;
text-align: center;
}
.btn-primary {
background: linear-gradient(135deg, var(--primary), var(--primary-dark));
color: white;
border: none;
padding: 14px 28px;
border-radius: 10px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: var(--transition);
display: flex;
align-items: center;
gap: 8px;
white-space: nowrap;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(108, 99, 255, 0.3);
}
.btn-primary:active {
transform: translateY(0);
}
/* Tabela */
.table-wrapper {
overflow-x: auto;
border-radius: var(--radius);
border: 2px solid var(--light-gray);
margin-bottom: 20px;
position: relative;
z-index: 1;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 0.95rem;
}
table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-dark));
color: white;
}
table th {
padding: 16px 20px;
text-align: left;
font-weight: 600;
letter-spacing: 0.5px;
}
table th:first-child {
border-radius: 10px 0 0 0;
}
table th:last-child {
border-radius: 0 10px 0 0;
}
table tbody tr {
transition: var(--transition);
border-bottom: 1px solid var(--light-gray);
}
table tbody tr:last-child {
border-bottom: none;
}
table tbody tr:hover {
background: rgba(108, 99, 255, 0.04);
}
table td {
padding: 14px 20px;
color: var(--dark);
}
table td:last-child {
text-align: center;
}
/* Badge de quantidade */
.badge-quantidade {
display: inline-block;
background: var(--primary-light);
color: white;
padding: 4px 12px;
border-radius: 20px;
font-weight: 600;
font-size: 0.85rem;
}
/* Botões de ação */
.btn-action {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 14px;
border-radius: 8px;
text-decoration: none;
font-size: 0.85rem;
font-weight: 500;
transition: var(--transition);
border: 2px solid transparent;
}
.btn-remove {
background: rgba(231, 76, 60, 0.1);
color: var(--danger);
}
.btn-remove:hover {
background: var(--danger);
color: white;
transform: scale(1.05);
}
.btn-clear {
background: rgba(231, 76, 60, 0.1);
color: var(--danger);
padding: 8px 16px;
border-radius: 10px;
text-decoration: none;
font-size: 0.9rem;
font-weight: 500;
transition: var(--transition);
display: inline-flex;
align-items: center;
gap: 6px;
}
.btn-clear:hover {
background: var(--danger);
color: white;
}
/* Rodapé da tabela */
.table-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 0 0;
flex-wrap: wrap;
gap: 15px;
}
.btn-export {
background: linear-gradient(135deg, var(--success), #27ae60);
color: white;
padding: 12px 24px;
border-radius: 10px;
text-decoration: none;
font-weight: 600;
display: inline-flex;
align-items: center;
gap: 8px;
transition: var(--transition);
border: none;
cursor: pointer;
}
.btn-export:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(46, 204, 113, 0.3);
}
/* Estado vazio */
.empty-state {
text-align: center;
padding: 60px 20px;
background: var(--bg);
border-radius: var(--radius);
}
.empty-state .icon {
font-size: 4rem;
margin-bottom: 15px;
display: block;
}
.empty-state h3 {
color: var(--dark);
margin-bottom: 8px;
}
.empty-state p {
color: var(--gray);
}
/* Aviso de sessão */
.session-warning {
margin-top: 25px;
padding: 14px 20px;
background: rgba(243, 156, 18, 0.1);
border-radius: 10px;
display: flex;
align-items: center;
gap: 10px;
color: var(--warning);
font-size: 0.9rem;
border-left: 4px solid var(--warning);
position: relative;
z-index: 1;
}
/* Responsividade */
@media (max-width: 768px) {
.container {
padding: 20px;
}
h1 {
font-size: 1.6rem;
}
.add-form {
flex-direction: column;
padding: 12px;
}
.add-form .input-quantidade {
max-width: 100%;
}
.btn-primary {
width: 100%;
justify-content: center;
}
.stats-bar {
flex-direction: column;
gap: 10px;
}
.stat-item {
justify-content: center;
}
table {
font-size: 0.85rem;
}
table th,
table td {
padding: 10px 12px;
}
.table-footer {
flex-direction: column;
align-items: stretch;
}
.btn-export {
justify-content: center;
}
.session-warning {
font-size: 0.8rem;
padding: 12px 16px;
}
}
@media (max-width: 480px) {
.container {
padding: 15px;
}
h1 {
font-size: 1.4rem;
}
table th,
table td {
padding: 8px 10px;
font-size: 0.8rem;
}
.btn-action {
padding: 4px 10px;
font-size: 0.8rem;
}
}
/* Animações */
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
table tbody tr {
animation: slideIn 0.3s ease forwards;
}
table tbody tr:nth-child(1) { animation-delay: 0.05s; }
table tbody tr:nth-child(2) { animation-delay: 0.1s; }
table tbody tr:nth-child(3) { animation-delay: 0.15s; }
table tbody tr:nth-child(4) { animation-delay: 0.2s; }
table tbody tr:nth-child(5) { animation-delay: 0.25s; }
table tbody tr:nth-child(6) { animation-delay: 0.3s; }
table tbody tr:nth-child(7) { animation-delay: 0.35s; }
table tbody tr:nth-child(8) { animation-delay: 0.4s; }
table tbody tr:nth-child(9) { animation-delay: 0.45s; }
table tbody tr:nth-child(10) { animation-delay: 0.5s; }
/* Scrollbar personalizada */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: var(--bg);
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
background: var(--primary-light);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--primary);
}
2. Criar o arquivo index.php
<?php
// index.php - Lista de Compras com Sessão
// Inicia a sessão para armazenar a lista temporariamente
session_start();
// Inicializa a lista se não existir
if (!isset($_SESSION['lista_compras'])) {
$_SESSION['lista_compras'] = [];
}
// Processa ações
$acao = $_GET['acao'] ?? '';
if ($acao === 'adicionar' && $_SERVER['REQUEST_METHOD'] === 'POST') {
$item = trim($_POST['item'] ?? '');
$quantidade = intval($_POST['quantidade'] ?? 1);
if (!empty($item)) {
$_SESSION['lista_compras'][] = [
'item' => $item,
'quantidade' => $quantidade,
'id' => uniqid() // Identificador único
];
}
header('Location: index.php');
exit;
}
if ($acao === 'remover' && isset($_GET['id'])) {
$id = $_GET['id'];
foreach ($_SESSION['lista_compras'] as $chave => $produto) {
if ($produto['id'] === $id) {
unset($_SESSION['lista_compras'][$chave]);
break;
}
}
// Reindexa o array
$_SESSION['lista_compras'] = array_values($_SESSION['lista_compras']);
header('Location: index.php');
exit;
}
if ($acao === 'limpar') {
$_SESSION['lista_compras'] = [];
header('Location: index.php');
exit;
}
// Conta total de itens
$total_itens = count($_SESSION['lista_compras']);
$total_quantidade = 0;
foreach ($_SESSION['lista_compras'] as $produto) {
$total_quantidade += $produto['quantidade'];
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Lista de Compras</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>🛒 Lista de Compras</h1>
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;">
<span>
Itens: <?php echo $total_itens; ?> |
Quantidade total: <?php echo $total_quantidade; ?>
</span>
<span>
<a href="?acao=limpar" style="color: #dc3545;">🗑️ Limpar lista</a>
</span>
</div>
<!-- Formulário para adicionar itens -->
<form method="POST" action="?acao=adicionar" style="display: flex; gap: 10px; margin-bottom: 20px;">
<div class="form-group" style="flex: 1; margin: 0;">
<input type="text" name="item" placeholder="Digite o item..." required style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;">
</div>
<div class="form-group" style="width: 80px; margin: 0;">
<input type="number" name="quantidade" value="1" min="1" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;">
</div>
<button type="submit" style="padding: 12px 24px;">➕ Adicionar</button>
</form>
<!-- Lista de itens -->
<?php if (count($_SESSION['lista_compras']) > 0): ?>
<table>
<thead>
<tr>
<th>#</th>
<th>Item</th>
<th>Quantidade</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
<?php
$contador = 1;
foreach ($_SESSION['lista_compras'] as $produto):
?>
<tr>
<td><?php echo $contador++; ?></td>
<td><?php echo htmlspecialchars($produto['item']); ?></td>
<td><?php echo $produto['quantidade']; ?></td>
<td>
<a href="?acao=remover&id=<?php echo $produto['id']; ?>"
style="color: #dc3545; text-decoration: none;"
onclick="return confirm('Remover este item?')">
❌ Remover
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="4" style="text-align: center;">
<a href="export.php" class="btn-export" style="text-decoration: none; display: inline-block; padding: 10px 20px;">
📥 Exportar Lista (CSV)
</a>
</td>
</tr>
</tfoot>
</table>
<?php else: ?>
<div style="text-align: center; padding: 40px; background: #f8f9fa; border-radius: 5px;">
<p style="color: #666;">Sua lista está vazia. Adicione itens acima!</p>
</div>
<?php endif; ?>
<p style="margin-top: 20px; text-align: center; color: #999; font-size: 14px;">
⚠️ A lista é temporária e será apagada ao fechar o navegador.
</p>
</div>
</body>
</html>
3. Criar o arquivo export.php
<?php
// export.php - Exporta a lista de compras
session_start();
// Verifica se há itens na lista
if (!isset($_SESSION['lista_compras']) || empty($_SESSION['lista_compras'])) {
header('Location: index.php');
exit;
}
// Nome do arquivo
$nome_arquivo = 'lista_compras_' . date('Y-m-d_H-i-s') . '.csv';
// Headers
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $nome_arquivo . '"');
// Abre saída
$saida = fopen('php://output', 'w');
// BOM para Excel
fprintf($saida, chr(0xEF).chr(0xBB).chr(0xBF));
// Cabeçalhos
fputcsv($saida, ['Item', 'Quantidade']);
// Dados
foreach ($_SESSION['lista_compras'] as $produto) {
fputcsv($saida, [$produto['item'], $produto['quantidade']]);
}
fclose($saida);
exit;
?>






