我得到一个无限循环。我相信我在同一行输入中多次读取而不是进入下一行。从我能想出的来看,我需要在while循环结束时移动到下一个部分的另一段代码。我需要用strtok做这个,我的问题是怎么做的?
更新:必须使用前进循环,不得爆炸/插入。
“包含文件是bbs_2013invent.php并且读取:
<?php
$data =
"Sunscreen_SPF-55 BB-ss1 S 9.98 83 828.34 4.99 NA
Sunscreen_SPF-7000 BB-ss2 S 29.98 9 269.82 19 NA
SunHat-F BB-sh3 S 33.79 41 1385.39 25.5 NA
SunHat-M BB-sh8 S 41.79 18 752.22 29.5 NA
Sandals BB-sd4 S 19.38 32 620.16 4.75 NA
Towel-Large BB-tl S 45 57 2565.00 29.99 NA
Towel-Indiv BB-ts S 11.75 133 1562.75 5.9 NA
Beach_Chair BB-bc2 S 67.5 19 1282.50 47.5 NA
Umbrella BB-umb R 129 18 2322 398 0.4
Surfboard BB-surf2 R 135 27 3645 735 0.2
JetSki BB-jsk7 R 160.25 38 6089.50 18795.00 0.4
Outlaw20_Powerboat BB-pb20 R 298 11 3278 67850.00 0.3";
?>
其余代码如下:
<?PHP
//Imports a file
include 'bbs_2013invent.php';
//variable
$delimiters = "\n";
//Declare and fill array
$product = ['name' => rtrim(strtok($data, $delimiters)),
'serial' => rtrim(strtok($delimiters)),
'status' => rtrim(strtok($delimiters)),
'price' => rtrim(strtok($delimiters)),
'units' => rtrim(strtok($delimiters)),
'revenue' => rtrim(strtok($delimiters)),
'cost' => rtrim(strtok($delimiters)),
'factor' => rtrim(strtok($delimiters)),
];
//Column headers
printf("PRODUCT \tSERIAL# \tPRICE/RENTALCHG \tUNITS/DAYS \tREVENUE \tORIGCOST \tNOTES");
//while loop
while(['name'])
{
//Calculates the total days rented and revenue respectively
$totalDays += $units;
$totalRevenue += $revenue;
if ($key == 'status' && $value = S && "units" <800)
{
printf("\t L");
}
if ($key == 'status' && $value = R && "units" <20)
{
printf("\t LR");
}
//foreach loop
foreach($product as $key => $value)
{
printf($key, $value);
}
} //end of while loop
//displays the total number of days items have been rented
printf($totalDays);
//displays the total revenue of all products combined
printf($totalRevenue);
?>
答案 0 :(得分:0)
按照你的逻辑,我刚刚编辑了一些你的代码只是为了得到一个工作样本,你的代码在这里和那里都有各种各样的问题。我认为这可以帮助您自己遵循自己的方式。
$data =
"Sunscreen_SPF-55 BB-ss1 S 9.98 83 828.34 4.99 NA
Sunscreen_SPF-7000 BB-ss2 S 29.98 9 269.82 19 NA
SunHat-F BB-sh3 S 33.79 41 1385.39 25.5 NA
SunHat-M BB-sh8 S 41.79 18 752.22 29.5 NA
Sandals BB-sd4 S 19.38 32 620.16 4.75 NA
Towel-Large BB-tl S 45 57 2565.00 29.99 NA
Towel-Indiv BB-ts S 11.75 133 1562.75 5.9 NA
Beach_Chair BB-bc2 S 67.5 19 1282.50 47.5 NA
Umbrella BB-umb R 129 18 2322 398 0.4
Surfboard BB-surf2 R 135 27 3645 735 0.2
JetSki BB-jsk7 R 160.25 38 6089.50 18795.00 0.4
Outlaw20_Powerboat BB-pb20 R 298 11 3278 67850.00 0.3";
$delimiters = " ";
$totalDays = 0;
$totalRevenue = 0;
printf("PRODUCT \tSERIAL# \tPRICE/RENTALCHG \tUNITS/DAYS \tREVENUE \tORIGCOST \tNOTES <br>");
$data = explode("\n", $data);
while($data)
{
$line = array_shift($data);
$product = ['name' => rtrim(strtok($line, $delimiters)),
'serial' => rtrim(strtok($delimiters)),
'status' => rtrim(strtok($delimiters)),
'price' => rtrim(strtok($delimiters)),
'units' => rtrim(strtok($delimiters)),
'revenue' => rtrim(strtok($delimiters)),
'cost' => rtrim(strtok($delimiters)),
'factor' => rtrim(strtok($delimiters)),
];
$totalDays += $product['units'];
$totalRevenue += $product['revenue'];
if ($product['status'] == "S" && $product["units"] < 800)
{
printf("\t [L] ");
}
if ($product['status'] == "R" && $product["units"] < 20)
{
printf("\t [LR] ");
}
printf(implode("\t", $product) . "<br>");
}
printf("<br>totalDays: ".$totalDays);
printf("<br>totalRevenue: ".$totalRevenue);
答案 1 :(得分:0)
您可以尝试以下方式:
//variable
$delimiters = "\n";
$product = rtrim(strtok($data, $delimiters));
//Column headers
printf("PRODUCT \tSERIAL# \tPRICE/RENTALCHG \tUNITS/DAYS \tREVENUE \tORIGCOST \tNOTES");
//while loop
while($product)
{
printf($product);
list($name, $serial, $status, $price, $units, $revenue, $cost, $factor) = explode(' ', $product);
//Calculates the total days rented and revenue respectively
$totalDays += $units;
$totalRevenue += $revenue;
if ($status == 'S' && $units <800)
{
printf("\t L");
}
if ($status == 'R' && $units <20)
{
printf("\t LR");
}
$product = rtrim(strtok($delimiters));
} //end of while loop
//displays the total number of days items have been rented
printf($totalDays);
//displays the total revenue of all products combined
printf($totalRevenue);