227 lines
6.5 KiB
Markdown
227 lines
6.5 KiB
Markdown
간단한 가계부 웹 애플리케이션을 HTTP, CSS, JavaScript, MySQL을 사용해 만들 수 있습니다. 이 애플리케이션은 수입 및 지출을 입력하고, 해당 내역을 데이터베이스에 저장하며, 화면에 표시하는 기본적인 기능을 포함합니다.
|
|
|
|
### 프로젝트 구조
|
|
```
|
|
/gagyeebu
|
|
├── index.html # 메인 페이지
|
|
├── style.css # CSS 파일
|
|
├── script.js # JavaScript 파일
|
|
└── server.php # 서버 스크립트 (PHP)
|
|
```
|
|
|
|
### 1. `index.html` - 메인 페이지
|
|
HTML 구조를 만들고, 수입 및 지출을 입력하는 폼과 기록을 보여줄 테이블을 포함합니다.
|
|
|
|
```html
|
|
<!DOCTYPE html>
|
|
<html lang="ko">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>가계부</title>
|
|
<link rel="stylesheet" href="style.css">
|
|
</head>
|
|
<body>
|
|
<h1>개인 가계부</h1>
|
|
|
|
<form id="transaction-form">
|
|
<label for="date">날짜:</label>
|
|
<input type="date" id="date" required>
|
|
|
|
<label for="description">항목:</label>
|
|
<input type="text" id="description" placeholder="항목" required>
|
|
|
|
<label for="income">수입:</label>
|
|
<input type="number" id="income" placeholder="수입 금액">
|
|
|
|
<label for="expense">지출:</label>
|
|
<input type="number" id="expense" placeholder="지출 금액">
|
|
|
|
<button type="submit">추가</button>
|
|
</form>
|
|
|
|
<table id="transactions-table">
|
|
<thead>
|
|
<tr>
|
|
<th>날짜</th>
|
|
<th>항목</th>
|
|
<th>수입</th>
|
|
<th>지출</th>
|
|
<th>잔액</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<!-- 기록이 여기에 표시됩니다 -->
|
|
</tbody>
|
|
</table>
|
|
|
|
<script src="script.js"></script>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
### 2. `style.css` - 스타일링
|
|
간단한 CSS로 페이지를 스타일링합니다.
|
|
|
|
```css
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 20px;
|
|
padding: 0;
|
|
background-color: #f4f4f4;
|
|
}
|
|
|
|
h1 {
|
|
text-align: center;
|
|
}
|
|
|
|
form {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
label {
|
|
margin-right: 10px;
|
|
}
|
|
|
|
input {
|
|
margin-right: 20px;
|
|
}
|
|
|
|
button {
|
|
padding: 5px 15px;
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
border: none;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button:hover {
|
|
background-color: #45a049;
|
|
}
|
|
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
th, td {
|
|
padding: 10px;
|
|
text-align: left;
|
|
border-bottom: 1px solid #ddd;
|
|
}
|
|
```
|
|
|
|
### 3. `script.js` - 클라이언트 사이드 스크립트
|
|
JavaScript로 폼 제출을 처리하고, 서버와 상호작용하는 코드를 작성합니다.
|
|
|
|
```javascript
|
|
document.getElementById('transaction-form').addEventListener('submit', function(event) {
|
|
event.preventDefault();
|
|
|
|
const date = document.getElementById('date').value;
|
|
const description = document.getElementById('description').value;
|
|
const income = document.getElementById('income').value || 0;
|
|
const expense = document.getElementById('expense').value || 0;
|
|
|
|
const data = { date, description, income, expense };
|
|
|
|
fetch('server.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(data)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
addTransactionToTable(date, description, income, expense, data.balance);
|
|
} else {
|
|
alert('Error: ' + data.error);
|
|
}
|
|
});
|
|
});
|
|
|
|
function addTransactionToTable(date, description, income, expense, balance) {
|
|
const table = document.getElementById('transactions-table').getElementsByTagName('tbody')[0];
|
|
const newRow = table.insertRow();
|
|
|
|
const dateCell = newRow.insertCell(0);
|
|
const descriptionCell = newRow.insertCell(1);
|
|
const incomeCell = newRow.insertCell(2);
|
|
const expenseCell = newRow.insertCell(3);
|
|
const balanceCell = newRow.insertCell(4);
|
|
|
|
dateCell.textContent = date;
|
|
descriptionCell.textContent = description;
|
|
incomeCell.textContent = income;
|
|
expenseCell.textContent = expense;
|
|
balanceCell.textContent = balance;
|
|
}
|
|
```
|
|
|
|
### 4. `server.php` - 서버 사이드 스크립트 (PHP + MySQL)
|
|
PHP를 사용해 MySQL 데이터베이스와 통신하여 데이터를 저장하고 불러옵니다.
|
|
|
|
```php
|
|
<?php
|
|
$servername = "localhost";
|
|
$username = "username";
|
|
$password = "password";
|
|
$dbname = "gagyeebu";
|
|
|
|
$conn = new mysqli($servername, $username, $password, $dbname);
|
|
|
|
if ($conn->connect_error) {
|
|
die("Connection failed: " . $conn->connect_error);
|
|
}
|
|
|
|
$data = json_decode(file_get_contents("php://input"), true);
|
|
|
|
$date = $data['date'];
|
|
$description = $data['description'];
|
|
$income = $data['income'];
|
|
$expense = $data['expense'];
|
|
|
|
$sql = "INSERT INTO transactions (date, description, income, expense) VALUES ('$date', '$description', '$income', '$expense')";
|
|
|
|
if ($conn->query($sql) === TRUE) {
|
|
$result = $conn->query("SELECT SUM(income - expense) AS balance FROM transactions");
|
|
$row = $result->fetch_assoc();
|
|
$balance = $row['balance'];
|
|
|
|
echo json_encode(['success' => true, 'balance' => $balance]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => $conn->error]);
|
|
}
|
|
|
|
$conn->close();
|
|
?>
|
|
```
|
|
|
|
### 5. MySQL 테이블 생성
|
|
데이터베이스에 테이블을 생성하는 SQL 명령어입니다.
|
|
|
|
```sql
|
|
CREATE DATABASE gagyeebu;
|
|
USE gagyeebu;
|
|
|
|
CREATE TABLE transactions (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
date DATE NOT NULL,
|
|
description VARCHAR(255) NOT NULL,
|
|
income DECIMAL(10, 2) DEFAULT 0.00,
|
|
expense DECIMAL(10, 2) DEFAULT 0.00
|
|
);
|
|
```
|
|
|
|
### 요약
|
|
- `index.html`: 가계부의 메인 페이지로, 사용자가 데이터를 입력할 수 있는 폼과 기록을 보여줍니다.
|
|
- `style.css`: 페이지 스타일링을 위한 CSS 파일입니다.
|
|
- `script.js`: 폼 입력을 처리하고 서버에 데이터를 보내는 JavaScript 코드입니다.
|
|
- `server.php`: 데이터를 MySQL에 저장하고 현재 잔액을 계산하는 PHP 스크립트입니다.
|
|
|
|
이 구조를 활용해 웹 기반의 간단한 가계부를 구축할 수 있습니다. 이 프로젝트를 로컬에서 실행하려면, PHP 서버 및 MySQL 서버가 필요하며, 각 파일을 해당 서버에 배치해야 합니다. |