所以我用下面的代码创建了一个文件:
$myfile = fopen("formData.txt", "a") or die("Kunne ikke tilgå filen");
$txt = '<div>
<div class="col s12 m6" style="margin-left: 15%; margin-right: 15%;" >
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<span class="card-title" style="text-align: center"><b> '.$_POST["navn"].'</b> alias '.$_POST["brugernavn"].'</span>
'.$_POST["date"].': <br/>'.$_POST["besked"].'</p>
</div>
<div class="card-action">
<a href="#">E_mail: '.$_POST["e_mail"].'</a>
<a href="#">Status: '.$_POST["status"].'</a>
<a href="#">Tel.: '.$_POST["mobil"].'</a>
</div>
</div>
</div>
</div>';
fwrite($myfile, $txt);
fclose($myfile);
现在我还有一个表格,可以发布一个新帖子,然后发布最新帖子下方。我知道如何创建一个按钮来从文件中删除整个内容。但我的目标是创建一个循环或其他东西来计算文件中当前div 的数量,然后给新添加的div 下一个数字的类,所以它就像
<div class="1">
...
</div>
<div class"2">
...
</div>
....
这样我就可以选择某个带有这样的东西的外部div/post
$file = 'formData.txt';
$lines = file($file);
$all_lines = implode('',$lines);
$entry = str_replace('<!-- find -->','<!-- replace -->',$all_lines);
我的问题是我该怎么做,如果我按下帖子本身中包含的带有其 ID/类的按钮,我就可以删除该单个帖子?我在想类似的事情
nth-child()
但我不确定如何...希望你们能帮帮我!
您可以在每个循环项目的末尾添加一个注释行,如下所示
<div class="1">
...
</div>
<!-- New form data added -->
<div class"2">
...
</div>
<!-- New form data added -->
然后,您可以计算下次更新文件时添加的注释行数。通过这种方式,您可以在下一个计数中添加新的 css 类。请注意,您每次都应添加相同的评论
注意:抱歉,无法正确格式化我的代码。从我的 mobile
我找到了解决我问题的纯 php 解决方案,所以我有以下文件:
<?php
$currentDate = date('Y-m-d');
if (isset($_SESSION["loggedin"]))
{
if (isset($_POST['save']))
{
if (!empty($_POST["navn"]) && !empty($_POST["besked"]))
{
if (strpos($_POST["besked"], "<") === false)
{
include 'connect.php';
$brugernavn = $_SESSION["navn"];
$query = "INSERT INTO beskeder (username) VALUES ('$brugernavn')";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
if ($result){
$query = "SELECT id FROM `beskeder` WHERE username='$brugernavn'";
if ($stmt = mysqli_prepare($conn, $query)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id);
while (mysqli_stmt_fetch($stmt)) {
$_SESSION["id"] = $id;
}
mysqli_stmt_close($stmt);
/*<form action="admin.php" method="post">
<input type="hidden" name="id" value="'.$_SESSION["id"].'"/>
<button class="waves-effect waves-light btn" name="delete" type="submit" value="Delete">Slet besked</button>
</form>'*/
$myfile = fopen("formData.txt", "a") or die("Kunne ikke tilgå filen");
$txt = '
<div id="'.$_SESSION["id"].'">
<div class="col s12 m6" style="margin-left: 15%; margin-right: 15%;" >
<div class="card teal lighten-2 class">
<div class="card-content white-text">
<span class="card-title" style="text-align: center"><b> '.$_POST["besked"].'</b></span>
Navn: '.$_POST["navn"].' / Brugernavn: '.$_POST["brugernavn"].' <br/>'.$_POST["date"].'</p>
</div>
<div class="card-action">
<a href="#">E_mail: '.$_POST["e_mail"].'</a>
<a href="#">Status: '.$_POST["status"].'</a>
<a href="#">Tel.: '.$_POST["mobil"].'</a>
</div>
</div>
</div>
</div>';
fwrite($myfile, $txt);
fclose($myfile);
$myfile = fopen("adminData.txt", "a") or die("Kunne ikke tilgå filen");
$txt = '
<div id="'.$_SESSION["id"].'">
<div class="col s12 m6" style="margin-left: 15%; margin-right: 15%;" >
<div class="card teal lighten-2 class">
<div class="card-content white-text">
<span class="card-title" style="text-align: center"><b> '.$_POST["besked"].'</b></span>
Navn: '.$_POST["navn"].' / Brugernavn: '.$_POST["brugernavn"].' <br/>'.$_POST["date"].'</p>
</div>
<div class="card-action">
<a href="#">E_mail: '.$_POST["e_mail"].'</a>
<a href="#">Status: '.$_POST["status"].'</a>
<a href="#">Tel.: '.$_POST["mobil"].'</a>
</div>
</div>
</div>
<form action="admin.php" method="get">
<input type="hidden" name="id" value="'.$_SESSION["id"].'"/>
<button class="waves-effect waves-light btn" name="delete" type="submit" value="Delete">Slet besked</button>
</form>
</div>';
fwrite($myfile, $txt);
fclose($myfile);
echo '
<p>Besked sendt<br/>
For at se beskeden skal du trykke "Læs beskeder"<br/>
<a href="javascript:history.go(-1)"><button class="waves-effect waves-light btn" type="submit" value="getmeout">Tilbage</button></a></p>
';
}
}
}
else {
echo '<p>Er du dum?</p>';
}
}
elseif (empty($_POST["navn"]) || empty($_POST["besked"]))
{
echo '<p>Alle felter skal fyldes ud, prøv igen<br/>
Har du endnu ikke sat et Navn kan dette gøres under din profil<br/>
<a href="javascript:history.go(-1)"><button class="waves-effect waves-light btn" type="submit" value="getmeout">Tilbage</button></a></p></p>';
}
}
elseif (isset($_POST['read']))
{
if ($_SESSION["navn"] == 'admin')
{
$myfile = fopen("adminData.txt", "r") or die("Kunne ikke tilgå filen");
echo fread($myfile,filesize("adminData.txt"));
fclose($myfile);
}
else {
$myfile = fopen("formData.txt", "r") or die("Kunne ikke tilgå filen");
echo fread($myfile,filesize("formData.txt"));
fclose($myfile);
}
}
elseif (isset($_POST['delete']))
{
$myfile = fopen("formData.txt", "w") or die("Kunne ikke tilgå filen");
$txt = '';
fwrite($myfile, $txt);
fclose($myfile);
$myfile = fopen("adminData.txt", "w") or die("Kunne ikke tilgå filen");
$txt = '';
fwrite($myfile, $txt);
fclose($myfile);
echo '<p>Filen blev slettet
For at se beskeden skal du trykke "læs beskeder<br/>
<a href="javascript:history.go(-1)"><button class="waves-effect waves-light btn" type="submit" value="getmeout">Tilbage</button></a></p></p>';
}
}
else {
echo'<p>Du skal være logget ind for at se denne side</p>
<br/>
<a href="javascript:history.go(-1)"><button class="waves-effect waves-light btn" name="return">Prøv igen</button></a>';
}
echo '</div>';
echo '</div>';
?>
这是从前一个页面完成按钮按下的页面,如果您以管理员身份登录以访问 adminData 并具有删除按钮,则可能会看到它,如果您正常登录,您将看不到它。如果管理员决定使用该 ID 删除帖子,则会将其发送到此页面
if ($_SESSION["navn"] == 'admin')
{
if (!empty($_GET["id"]))
{
$file = 'formData.txt';
$lines = file($file);
$all_lines = implode('',$lines);
$entry = str_replace('<div id="'.$_GET["id"].'">','<div id="'.$_GET["id"].'" style="display: none;">',$all_lines);
$fp = fopen($file,'w');
$fw = fwrite($fp,$entry);
fclose($fp);
$file = 'adminData.txt';
$lines = file($file);
$all_lines = implode('',$lines);
$entry = str_replace('<div id="'.$_GET["id"].'">','<div id="'.$_GET["id"].'" style="display: none;">',$all_lines);
$fp = fopen($file,'w');
$fw = fwrite($fp,$entry);
fclose($fp);
echo '<p>Beskeden blev slettet</p>';
}
echo '
<form action="beskeder.php" method="post">
<button class="waves-effect waves-light btn" name="delete" type="submit" value="Delete">Slet alle beskeder</button>
</form>
';
}
它浪费了资源,但工作很简单。感谢您的建议!