php日记本,包含添加,删除,保存
时间: 2024-05-22 13:15:53 浏览: 8
PHP日记本,包含添加、删除、保存功能的实现代码如下:
<?php
// 定义日记文件路径
define('DIARY_FILE', 'diary.txt');
// 获取日记列表
function getDiaryList() {
$list = array();
if (file_exists(DIARY_FILE)) {
$content = file_get_contents(DIARY_FILE);
$list = explode("\n", $content);
}
return $list;
}
// 添加日记
function addDiary($text) {
$time = date('Y-m-d H:i:s');
$line = "[$time] $text\n";
file_put_contents(DIARY_FILE, $line, FILE_APPEND);
}
// 删除日记
function deleteDiary($index) {
$list = getDiaryList();
if (isset($list[$index])) {
unset($list[$index]);
$content = implode("\n", $list);
file_put_contents(DIARY_FILE, $content);
}
}
// 保存日记
function saveDiary($index, $text) {
$list = getDiaryList();
if (isset($list[$index])) {
$list[$index] = $text;
$content = implode("\n", $list);
file_put_contents(DIARY_FILE, $content);
}
}
// 处理请求
if (isset($_POST['action'])) {
$action = $_POST['action'];
switch ($action) {
case 'add':
$text = $_POST['text'];
addDiary($text);
break;
case 'delete':
$index = $_POST['index'];
deleteDiary($index);
break;
case 'save':
$index = $_POST['index'];
$text = $_POST['text'];
saveDiary($index, $text);
break;
}
}
// 显示日记列表
$list = getDiaryList();
echo '<ul>';
foreach ($list as $index => $line) {
echo '<li>';
echo htmlspecialchars($line);
echo '<form method="post">';
echo '<input type="hidden" name="action" value="delete">';
echo '<input type="hidden" name="index" value="'.$index.'">';
echo '<input type="submit" value="删除">';
echo '</form>';
echo '<form method="post">';
echo '<input type="hidden" name="action" value="save">';
echo '<input type="hidden" name="index" value="'.$index.'">';
echo '<input type="text" name="text" value="'.htmlspecialchars($line).'">';
echo '<input type="submit" value="保存">';
echo '</form>';
echo '</li>';
}
echo '</ul>';
// 显示添加日记表单
echo '<form method="post">';
echo '<input type="hidden" name="action" value="add">';
echo '<textarea name="text"></textarea>';
echo '<input type="submit" value="添加">';
echo '</form>';
?>
以上代码实现了一个简单的PHP日记本,包含添加、删除、保存功能。具体实现方式是,定义了一个日记文件路径,通过getDiaryList()函数获取日记列表,addDiary()函数添加日记,deleteDiary()函数删除日记,saveDiary()函数保存日记。然后根据请求参数,调用相应的函数进行处理。最后,通过HTML表单实现添加、删除、保存功能的交互。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)