I'm a PHP beginner and I need to code an HTML table with a while loop, but I don't want to loop the headers (First Name and Last Name) and I don't know how to break the loop in that line. You can find below my code so far.
<html>
<head>
<form method="post">
<input type="submit" class="button" name="displayNameDonation" id="displayNameDonation" value="Press to show largest donator" />
<br><br>
<input type="submit" class="button" name="sendData" id="sendData" value="Press to send max donation and largest donor to text file" />
</form>
</head>
</html>
<?php
// Open your file which is “r” only –search what r stands for, or …(explain)
//function fopen
$file = fopen("file.txt", "r") or die("Unable to open file!");
//store array
$valuesArray = array();
//Create loop to separate values and store in array.
//function feof
<table>
while(!feof($file))
{
//fgets function
$line = fgets($file);
//explode function
$value = explode(",", $line);
//stores the names and amounts into separate arrays.
$valuesArray[] = array($value[0], $value[1]);
//puts values from arrays
echo $value[0]. $value[1];
}
//fclose function
fclose($file);
//Create line counter.
$lineCounter = sizeof($valuesArray);
//Set variable to max value and convert to integer.
$maxDonation = (int) $valuesArray[0][1];
//create loop to read array elements
for($i = 0; $i <$lineCounter; $i++)
{
//Check to see if value in array is greater than the maxDonation
if ((int) $valuesArray[$i][1] > $maxDonation)
{
//Convert value from array to integer
$maxDonation = (int) $valuesArray[$i][1];
//Obtain donors name
$donationName = $valuesArray[$i][0];
}
}
//function to display $maxDonation & $donationName when button is clicked.
function displayNameDonation($donationName, $maxDonation)
{
echo $donationName . $donationName. ": " . $maxDonation ;
}
if(isset($_POST['displayNameDonation']))
{
displayNameDonation($donationName, $maxDonation);
}
//function to import data into separate file
function sendData($donationName, $maxDonation)
{
$var_str = var_export($donationName.$maxDonation, true);
$var = "The max donation was by = $var_str;\n\n";
file_put_contents('results.txt', $var);
}
if(isset($_POST['sendData']))
{
sendData($donationName, $maxDonation);
}
?>
And this is the table I need to add (but taking off the $a and $b variables and replacing them for the while loop above:
<?php
$a="John";
$b="Smith";
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>". $a ."</td>
<td>". $b ."</td>
</table>";
?>
Many thanks in advance.