Hello, I a begginer in PHP. I was tasked to do an assignment and I am having a d
ID: 3725663 • 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!
you!!!
Explanation / Answer
First i am creating home.html which will accept the value as you have said and when submit is pressed it will go to index.php where function will generate random numbers.
<!DOCTYPE html>
<html>
<head>
<title>Generating Random Numbers</title>
</head>
<body>
<form action="index.php" method="post">
Input :<input type="text" name="inputNumber"/> (Value from 1-100)<br/>
<input type="submit" name="btnsubmit" value="submit"/>
</body>
</html>
Php code:
<?php
if(isset($_POST["btnsubmit"]))
{
$VAL = $_POST["inputNumber"];
for($i=0;$i<=$VAL;$i++){
$VALUE = mt_rand(1,2500);
$arr = array();
array_push($arr,$value);
}
$file = fopen(output.csv,"w");
foreach($arr as $random){
fputcsv($file,explode(',',$random);
}
fclose($file);
}
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.