gpt4 book ai didi

带有 while 循环的 PHP 捐赠表

转载 作者:行者123 更新时间:2023-12-04 07:51:13 26 4
gpt4 key购买 nike

我是 PHP 初学者,我需要使用 while 循环编写 HTML 表,但我不想循环标题(名字和姓氏),而且我不知道如何在该行中断循环.到目前为止,您可以在我的代码下方找到。

<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);
}
?>
这是我需要添加的表(但去掉 $a 和 $b 变量并将它们替换为上面的 while 循环:
<?php
$a="John";
$b="Smith";


echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>". $a ."</td>
<td>". $b ."</td>
</table>";
?>
提前谢谢了。

最佳答案

使用变量 $i跟踪它是否是第一个循环显示标题

$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);
或者在开始循环并从文件中读取之前只显示表的标题。
$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);

关于带有 while 循环的 PHP 捐赠表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66966662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com