Php donations table with a while loop

Viewed 101

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.

2 Answers

Use variable $i to track if it is the first loop show the header

$file = fopen('data.txt', 'r');
$i=0;
echo "<table border='1'>";
while(!feof($file)) 
{
    if($i===0){
        echo "<tr><th>First Name</th><th>Last Name</th></tr>";      
    }
    $line = fgets($file);
    $fullName = explode(",", $line);
    echo "<tr><td>$fullName[0]</td><td>$fullName[1]</td></tr>";
    $i++;
}
echo "</table>";
fclose($file);

or just show the header of table before you start the loop and reading from file.

$file = fopen('data.txt', 'r');
echo "<table border='1'>";
echo "<tr><th>First Name</th><th>Last Name</th></tr>";
while(!feof($file)) 
{
    $line = fgets($file);
    $fullName = explode(",", $line);
    echo "<tr><td>$fullName[0]</td><td>$fullName[1]</td></tr>";
}
echo "</table>";
fclose($file);

file.txt contain data of donors with their amount of money seperated with comma

Boss,100
Steve,12
Everst,300
Alpha,200
    <form method="post">
        <input type="submit" class="button" name="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>

function generous() return generous donor name and amount of money he donate from file.txt

<?php
function generous(){
    $file = fopen('file.txt', 'r');
    for ($i=0; !feof($file) ; $i++) { 
        $line = fgets($file);
        
        $donation = explode(",", $line);
        $name = $donation[0];
        $amount = (int)$donation[1];
        
        if($i===0){
            $maxN = $name;
            $maxA = $amount;
        }
        

        if($maxA <= $amount){
            $maxN =  $name;
            $maxA =  $amount;
        }
    }
    return [$maxN,$maxA];
}
function displayNameDonation(){
     list($maxN, $maxA) = generous();
        echo $maxN.'='.$maxA;//Everst=300
}
if(isset($_POST['displayNameDonation'])){
    displayNameDonation();
}
function sendData($donationName, $maxDonation)
{
    return "The max donation was by $donationName with amount of $maxDonation dollars.\n";
}
if(isset($_POST['sendData'])){
    list($maxN, $maxA) = generous();
    file_put_contents('results.txt', sendData($maxN, $maxA)); 
}
?>
Related