Hello, I a begginer in PHP. I was tasked to do an assignment and I am having a d
ID: 3725730 • Letter: H
Question
Hello, I a begginer in PHP. I was tasked to do an assignment and I am having a difficulty withthe problem. Thank you!!! What I want to see is a page divided in 2. On the top, there will be an input field where I can input an INTEGER 0-100 INCLUSIVE (0 and 100 are valid inputs!) that will be saved as $VAL and a submit button. When I hit submit on the top part I want PHP to calculate $VAL number of random integers (1-2,500 inclusive) and then create a new files *output.csv* and write all of the numbers into the new file. Each value should be separated by a comma, MANDATORY FOR SOLUTION: Must at least save file in file folder (ex. if the file is in www/index.php then the new file would be www/output.csv) BONUS POINTS SOLUTION: Find a way to both save it to local file AND have it download to the browser!
Explanation / Answer
If you need any assistance, give me a comment
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
if(isset($_POST['val'])){
//header for downloading file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="output.csv"');
$VAL = $_POST['val'];
$arr = [];
//generating random numbers
for($i=0; $i<$VAL; $i++){
$arr[] = rand(1, 2500);
}
$buffer = implode(",", $arr);
//write data into output.csv
file_put_contents("output.csv", $buffer);
echo $buffer;
}
else{
?>
<form action="" method="POST">
Enter Value: <input type="text" name="val" /><br />
<input type="submit" value="Generate & Download" />
</form>
<?php
}
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.