Skip to content
Advertisement

How to access specific array index and display it? [closed]

So in this project i have 3 PHP files, namely index.php, processor.php, and viewsubmission.php

My index.php and processor.php both works perfectly fine as I intended, my problem here now is in the viewsubmission.php.

when I run this file it only shows this:

enter image description here

is there a possible way where I could display a text before the data that comes from the text file? The expected output should be like this.

enter image description here

second problem: How can I display the new data without having to removed the first set of data that being displayed.

index.php //contains my form

<form action = "Processor.php" method = "post" enctype="multipart/form-data">
    Student ID:<input type="text" name="studentId"><br>
    Student Name: <input type="text" name="studentName"><br>
    Activity Number: <input type="text" name="activityNumber"><br>
    Comment: <input type="text" name="comment"><br>
    File Upload (doc,docx, & pdf files only):<br><input type="file" name="fileupload"> <br><br>
    <button type="submit">Submit File</button>
</form>

Processor.php //code all the inputs in the form gets process then write in the text file

//creating the name of the file
$newFileName = uniqid('',true) . "." . $FileExt;
//identify file destination
$fileDst = "uploads/" . $newFileName;
move_uploaded_file($TempFile, $fileDst);

//Write data in textfile
$file = fopen("uploads/data.txt", "a");
fwrite($file, $_POST['studentId'] . "n");
fwrite($file, $_POST['studentName'] . "n");
fwrite($file, $_POST['activityNumber'] . "n");
fwrite($file, $_POST['comment'] . "n");
fwrite($file, $fileDst . "n");
fclose($file);

echo "<br><br>>New file Location: " . $fileDst . "<br>";
echo "Record Saved...";

viewsubmission.php //access the file and read its contents and display it

<?php
    $filePath = "uploads/data.txt";
    $content = file_get_contents($filePath);

    $values = explode("n", $content);

    echo "<hr>";

    $numberofData = 0;
    foreach($values as $val){  
        echo $val . "<br>";
        $numberofData++;
    }
    $numberofData--;
    echo "<hr>";

Advertisement

Answer

Okay, to separate the rows from each other we need first to add any text as an identifier that this is the end of the row at the Processor.php then at viewsubmission.php you will explode by this identifier to get the rows, then explode each row to get the values inside it

In my example, I will use this word @@End@@ to detect the row end ~ feel free to use your own identifier

so at the Processor.php i will add this line fwrite($file, "@@End@@");

so the writing in the file will looks like this

//Write data in textfile
            $file = fopen("uploads/data.txt", "a");
            fwrite($file, $_POST['studentId'] . "n");
            fwrite($file, $_POST['studentName'] . "n");
            fwrite($file, $_POST['activityNumber'] . "n");
            fwrite($file, $_POST['comment'] . "n");
            fwrite($file, $fileDst . "n");
            fwrite($file, "@@End@@");
            fclose($file);

and at the viewsubmission.php

<?php

    $filePath = "uploads/data.txt";
    $content = file_get_contents($filePath);

    //------- seprate rows
    $rows = explode("@@End@@", $content);
    
    //ignoring last element as it will always be empty after exploading @@End@@
    for($i =0; $i< count($rows)-1 ; $i++){
        
        $row = $rows[$i];
        
        $values = explode("n", $row);      
        echo "<hr>";

        $numberofData = 0;
        foreach($values as $val){  
            echo $val . "<br>";
            $numberofData++;
        }
        $numberofData--;
        echo "<hr>";
    }

N.B we need to validate the inputs data & make sure that the identifier word is not allowed to be inserted by the user to avoid the break of logic, either by returning a validation error by trim it from the input value

Alternatively, you can save it in JSON as @Markus AO mentioned will be a better solution using json_encode and json_decode for saving & handling of data in JSON format instead of raw data

so the writing at Processor.php should be something like that

//Write data in textfile
            $row = [];
            $row["studentId"]       = $_POST['studentId'];
            $row["studentName"]     = $_POST['studentName'];
            $row["activityNumber"]  = $_POST['activityNumber'];
            $row["comment"]         = $_POST['comment'];
            
            $fileData = file_get_contents('uploads/data.json');
            $fileArray = json_decode($fileData);
            
            //if the file is empty or its the first record 
            if(!is_array($fileArray))
                $fileArray = [];
            
            array_push($fileArray, $row);
            
            $jsonData = json_encode($fileArray);
            file_put_contents('uploads/data.json', $jsonData);

and decoding at viewsubmission.php like that

$file_data = file_get_contents('uploads/data.json');
$rows = json_decode($file_data,true);

foreach($rows as $row){
    
    echo "<hr>";
    echo $row["studentId"] ."<br>";
    echo $row["studentName"] ."<br>";
    echo $row["activityNumber"] ."<br>";
    echo $row["comment"] ."<br>";
    echo "<hr>";
}

hope this helps!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement