<?php
// タスクを保存するファイル
$tasks_file = 'tasks.txt';
// タスクを追加
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_task'])) {
$new_task = trim($_POST['new_task']);
if (!empty($new_task)) {
$task_entry = $new_task . '|0'; // 0は未完了ステータスを示す
file_put_contents($tasks_file, $task_entry . PHP_EOL, FILE_APPEND);
}
}
// タスクの完了ステータスを変更
if (isset($_GET['toggle'])) {
$tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);
$index = $_GET['toggle'];
if (isset($tasks[$index])) {
$task_data = explode('|', $tasks[$index]);
$task_data[1] = $task_data[1] == '0' ? '1' : '0'; // ステータスを切り替える
$tasks[$index] = implode('|', $task_data);
file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);
}
}
// タスクを削除
if (isset($_GET['remove'])) {
$tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);
unset($tasks[$_GET['remove']]);
file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);
}
// 現在のタスクを取得
$tasks = file_exists($tasks_file) ? file($tasks_file, FILE_IGNORE_NEW_LINES) : [];
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>TODOリスト</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
}
form {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
input[type="text"] {
width: 300px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
border: none;
background-color: #5cb85c;
color: white;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #4cae4c;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #fff;
margin: 5px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
.completed {
text-decoration: line-through;
color: gray;
}
a {
text-decoration: none;
color: #d9534f;
margin-left: 10px;
}
a:hover {
color: #c9302c;
}
</style>
</head>
<body>
<h1>TODOリスト</h1>
<!-- タスクを追加するフォーム -->
<form method="post" action="">
<input type="text" name="new_task" placeholder="新しいタスクを入力" required>
<button type="submit">追加</button>
</form>
<!-- タスクを表示 -->
<ul>
<?php foreach ($tasks as $index => $task): ?>
<?php list($task_text, $status) = explode('|', $task); ?>
<li>
<span class="<?php echo $status == '1' ? 'completed' : ''; ?>">
<?php echo htmlspecialchars($task_text); ?>
</span>
<div>
<a href="?toggle=<?php echo $index; ?>">
<?php echo $status == '0' ? '完了にする' : '未完了に戻す'; ?>
</a>
<a href="?remove=<?php echo $index; ?>">削除</a>
</div>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
カテゴリー: programming
PHP else if
<?php
echo "Score? ";
$score = (int) fgets(STDIN);
if ($score >= 90) {
echo "A" . PHP_EOL;
} elseif($score >= 95){
echo "Super!" . PHP_EOL;
} else{
echo "C" . PHP_EOL;
}
PHP if-else 条件分岐
<?php
echo "Score?";
$score = (int) fgets(STDIN);
if($score >= 90){
echo "A" . PHP_EOL;
}else{
echo "Not A" . PHP_EOL;
}
Javascript 四角を描画
'use strict';
{
function draw() {
const canvas = document.querySelector('canvas');
if (typeof canvas.getContext === 'undefined') {
return;
}
const ctx = canvas.getContext('2d');
// ctx.fillRect(50, 50, 50, 50);
ctx.strokeRect(50, 50, 50, 50);
}
draw();
}
body {
background: #222;
}
canvas {
background: #fff;
}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>My Canvas</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<canvas width="600" height="240">
Canvas not supported.
</canvas>
<script src="js/main.js"></script>
</body>
</html>
MySQL 関数
DROP TABLE IF EXISTS posts;
CREATE TABLE posts (
id INT NOT NULL AUTO_INCREMENT,
message VARCHAR(140),
likes INT,
PRIMARY KEY (id)
);
INSERT INTO posts (message, likes) VALUES
('Thanks', 12),
('Merci', 4),
('Arigato', 4),
('Gracias', 15),
('Danke', 8);
-- + - * / %
SELECT
likes * 500 / 3 AS bonus,
FLOOR(likes * 500 / 3) AS floor,
CEIL(likes * 500 / 3) AS ceil,
-- ROUND(likes * 500 / 3) AS round
ROUND(likes * 500 / 3, 2) AS round
FROM
posts;
MySQL NULL
DROP TABLE IF EXISTS posts;
CREATE TABLE posts (
id INT NOT NULL AUTO_INCREMENT,
message VARCHAR(140),
likes INT,
PRIMARY KEY (id)
);
INSERT INTO posts (message, likes) VALUES
('Thanks', 12),
('Arigato', 4),
('Merci', NULL),
('Gracias', 15),
('Danke', NULL);
-- SELECT * FROM posts;
-- SELECT * FROM posts WHERE likes != 12;
-- SELECT * FROM posts WHERE likes != 12 OR likes IS NULL;
SELECT * FROM posts WHERE likes IS NOT NULL;
Java break、continue
import java.util.Scanner;
public class MyApp {
public static void main(String[] args) {
int[] scores = {70, -10, 80, 90};
for (int score: scores) {
if (score < 0) {
// break;
continue;
}
System.out.println(score);
}
System.out.println("Finished");
}
}
Java While
import java.util.Scanner;
public class MyApp {
public static void main(String[] args) {
String password;
do {
password = new Scanner(System.in).next();
} while (password.equals("d0t1nsta11") == false);
System.out.println("Password matched");
}
}
Java 配列と反復処理
import java.util.Scanner;
public class MyApp {
public static void main(String[] args) {
int[] scores = {70, 90, 80, 60};
// System.out.println(scores[0]);
// System.out.println(scores[1]);
// System.out.println(scores[2]);
// for (int i = 0; i < 3; i++) {
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}
}
Java for
import java.util.Scanner;
public class MyApp {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++){
System.out.println(i + " Hello");
}
}
}