-1

So I've created a file with the following code:

$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);

Now I also have a form which allows to post a new post which then goes below the latest one. I know how to create a button to remove the entire content from the file. But my goal is to make a loop or something which counts the current numbers of divs in the file, and then gives the newly added div a class of the next number, so it goes like

<div class="1">
...
</div>
<div class"2">
...
</div>
....

So that I can select a certain outer div/post with something like this

    $file = 'formData.txt';
    $lines = file($file);
    $all_lines = implode('',$lines);
    $entry = str_replace('<!-- find -->','<!-- replace -->',$all_lines);

My question is how do I do this so if I press a button included in the post itself with its ID/class, I can then delete that individual post? I'm thinking something like

nth-child()

but I'm not sure how... I hope you guys can help me out!

senty
  • 12,385
  • 28
  • 130
  • 260
  • See this answer: http://stackoverflow.com/questions/1947263/using-an-html-button-to-call-a-javascript-function Modify your button to refrence the element you want to delete. – RyanNerd Mar 23 '17 at 20:36
  • I get that part, but I'd still need to assign the div number or whatever, so that I could see which element I should delete/replace? Do you have any idea on how to search the file for number of outer divs (the posts), then loop through them and count them so it can assign a corresponding number to it? – guitarherokk Mar 23 '17 at 20:40
  • Can you make an example of what you expect to happen? e.g. with [phpfiddle](http://phpfiddle.org) – Sᴀᴍ Onᴇᴌᴀ Mar 23 '17 at 20:43
  • I got my page post.php, on this page you submit a form with the value "post", on the messages.php it checks if post has a value, if it does it'll do exactly like now and post the values into a text file, if you click the post it adds the message/post at the end of the file below the others. If however you view the messages.php where it prints the content of the file, there should be a button named "delete" next to each post (which ofcause was included in the write to file part). So when writing to file, I want a function which reads it through for the amount of divs, and then gives the – guitarherokk Mar 23 '17 at 20:45
  • total numbers of divs +1, and then the delete would be a button which has the div id(number), and only deletes that post and not the others – guitarherokk Mar 23 '17 at 20:46
  • The question is too broad. Please narrow it down to one specific aspect of the question. – Sᴀᴍ Onᴇᴌᴀ Mar 23 '17 at 21:46

2 Answers2

0

You could add a comment line at the end of each loop item like below

<div class="1">
...
</div>
<!-- New form data added -->
<div class"2">
...
</div>
<!-- New form data added -->

Then you can count the number of comment lines added when you update the file next time. By this way you can add new css class with next count. Please note that you should add the same comment everytime

Note: sorry, could not format my code properly. Posting from my mobile

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
manian
  • 1,418
  • 2
  • 16
  • 32
  • I think I explained it poorly, what I have now is basically a button which adds the content of $txt into the buttom of the Data file, what I want to have is a function which checks the file for the number of outer divs (each time a new $txt has been entered) so that I can select the
    , and only replace that one with empty text when I "delete" it, instead of replacing the whole file
    – guitarherokk Mar 23 '17 at 20:54
  • If I understand correctly now, you could possibly add one more common class to the same outer DIV and you can count this common class by using preg_match – manian Mar 23 '17 at 21:00
  • This would indeed get me the totalnumber of divs with that class. But if i aim to have a button included into that text file for each post made, it would require to have some sort of individual id. By getting the total number I'd still manually have to figure out what number it would be, and not just have a specific Id for each post – guitarherokk Mar 23 '17 at 21:07
  • Is it possible to try jQuery to find the n-th child and send this number to use AJAX to further process – manian Mar 23 '17 at 21:15
0

I've found a pure php solution to my issue, so I have the following file:

<?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>';
?>

This is the page which completes the button presses from the former page, making it possible if you are logged in as admin to acces the adminData and have the delete button, and if you are logged in normally you won't see it. If the admin decides to delete a post by using the id, they get sent to this page

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>

            ';
            }

Its quite a waste of ressources but does the job simply. Thanks for your suggestions!