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

JavaScript

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

JavaScript

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

JavaScript

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

JavaScript

and at the viewsubmission.php

JavaScript

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

JavaScript

and decoding at viewsubmission.php like that

JavaScript

hope this helps!

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