将生成的子数组数据映射到数组中每个顶层的 html 模板
Map generated sub array data to html template for each top level in the array
感谢 SO 社区的最出色帮助,我现在有一个自动生成的数组,看起来像这样..(看起来像真实数据但已匿名)
Array
(
[0] => Array
(
[0] => 15304
[1] => 15-04478-27247
[2] => mysuserid
[3] => eric smith
[4] => ericsfakeemail@gmail.com
[5] =>
[6] => 27 Any Close
[7] =>
[8] => Manchester
[9] => Greater Manchester
[10] => M14 2RB
[11] => United Kingdom
[12] => eric spilsbury
[13] => +44 7719 123456
[14] => 27 Any Close
[15] =>
[16] => Manchester
[17] => Greater Manchester
[18] => M14 2RB
[19] => United Kingdom
[20] => Array
(
[0] => 302804801330
[1] => 302804815489
)
[21] => Array
(
[0] => CAUTION DRIVEN BY A BIKER - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
[1] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
)
[22] =>
[23] => No
[24] => Array
(
[0] => 2
[1] => 1
)
[25] => Array
(
[0] => £3.90
[1] => £1.95
)
[26] => £0.00
[27] =>
[28] => £0.00
[29] => £0.00
[30] => £3.90
[31] => No
[32] => PayPal
[33] => 30-Jan-20
[34] => 30-Jan-20
[35] =>
[36] =>
[37] =>
[38] => 31-Jan-20
[39] => No
[40] =>
[41] =>
[42] => 5LD87965090833954U
[43] => Royal Mail 1st Class
[44] =>
[45] => Array
(
[0] => 1711351782020
[1] => 1711351781020
)
[46] =>
[47] => No
[48] =>
[49] => No
[50] =>
[51] => No
)
[1] => Array
(
[0] => 15121
[1] => 25-04445-36722
[2] => dopp69
[3] => mart podd
[4] => podfakeemail@gmail.com
[5] =>
[6] => Via Riccione N. 20/18
[7] =>
[8] => Sa Tiacca Quartu S. Elena
[9] => CA
[10] => 09847
[11] => Italy
[12] => Podd Mart
[13] => +39 342 000 9876
[14] => Via Riccione N. 30/32
[15] =>
[16] => Sa Tiacca Quartu S. Elena
[17] => CA
[18] => 09847
[19] => Italy
[20] => 302667176336
[21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
[22] =>
[23] => No
[24] => 2
[25] => £1.99
[26] => £3.14
[27] =>
[28] => £0.00
[29] => £0.00
[30] => £7.12
[31] => No
[32] => PayPal
[33] => 23-Jan-20
[34] => 23-Jan-20
[35] => 24-Jan-20
[36] =>
[37] =>
[38] => 24-Jan-20
[39] => Yes
[40] =>
[41] =>
[42] => 25C157351T667760U
[43] => Royal Mail International Standard
[44] =>
[45] => 1709118816020
[46] =>
[47] => No
[48] =>
[49] => No
[50] =>
[51] => No
)
[2] => Array
(
[0] => 15320
[1] => 20-04482-41274
[2] => wideboy3480
[3] => BEN ROYLES
[4] => wideboyemail@hotmail.co.uk
[5] => Yellow please
[6] => 10
[7] => Broomstick Close
[8] => Okehampton
[9] => Devon
[10] => EX20 1FT
[11] => United Kingdom
[12] => Ben Royles
[13] => +44 7876 401999
[14] => 105
[15] => Broomstick Close
[16] => Okehampton
[17] => Devon
[18] => EX10 1WT
[19] => United Kingdom
[20] => 302713568952
[21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
[22] =>
[23] => No
[24] => 1
[25] => £1.99
[26] => £0.00
[27] =>
[28] => £0.00
[29] => £0.00
[30] => £1.99
[31] => No
[32] => PayPal
[33] => 01-Feb-20
[34] => 01-Feb-20
[35] => 03-Feb-20
[36] =>
[37] =>
[38] =>
[39] => No
[40] =>
[41] =>
[42] => 7SD68673EE278782X
[43] => Royal Mail 1st Class
[44] =>
[45] => 1711667179020
[46] =>
[47] => No
[48] =>
[49] => No
[50] =>
[51] => No
)
<!---And so on--->
用于生成此代码的代码如下...
<?php
function readCSV($csvFile)
{
$line_of_text = [];
$file_handle = fopen($csvFile, 'r');
fgetcsv($file_handle);
fgetcsv($file_handle);
fgetcsv($file_handle);
while (!feof($file_handle)) {
$tmp = fgetcsv($file_handle, 1024);
if (isset($line_of_text[$tmp[0]])) {
foreach ($tmp as $k => $v) {
if (array_key_exists($k, $line_of_text[$tmp[0]])) {
if (!is_array($line_of_text[$tmp[0]][$k])) {
$kVal = $line_of_text[$tmp[0]][$k];
$line_of_text[$tmp[0]][$k] = [];
$line_of_text[$tmp[0]][$k][] = $kVal;
}
$line_of_text[$tmp[0]][$k][] = $v;
$line_of_text[$tmp[0]][$k] = array_unique($line_of_text[$tmp[0]][$k]);
$line_of_text[$tmp[0]][$k] = array_filter($line_of_text[$tmp[0]][$k]);
if (count($line_of_text[$tmp[0]][$k]) == 1) {
$line_of_text[$tmp[0]][$k] = array_values($line_of_text[$tmp[0]][$k]);
$line_of_text[$tmp[0]][$k] = $line_of_text[$tmp[0]][$k][0];
}
if (empty($line_of_text[$tmp[0]][$k])) {
$line_of_text[$tmp[0]][$k] = null;
}
} else {
$line_of_text[$tmp[0]][$k] = null;
}
}
$line_of_text[$tmp[0]][0] = $tmp[0];
} else {
$line_of_text[$tmp[0]] = $tmp;
}
}
fclose($file_handle);
return array_filter(array_values($line_of_text));
}
// Set path to CSV file
$csvFile = 'test.csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>
我想做的是使用这些自动生成的数组中的数据来自动生成我使用 html/php 标记制作的发票页。
我已经能够制作一个示例,以第一个条目 [0] 中的数据为目标,就像在这个示例片段中一样...
<td style="vertical-align: top; width: 100%; text-align:right;">
<b>Name: </b><br />
<b>Phone: </b><br />
<b>Email: </b><br />
<b>Paid Via: </b><br />
<b>Sold Via: </b><br />
<b>User ID: </b><br />
<b>Sales Rec: </b><br />
<b>Order No.: </b>
</td>
<td style="vertical-align: top; width: 311px;"><?php echo $csv[0][3]; ?><br />
<?php echo $csv[0][13]; ?><br />
<?php echo $csv[0][4]; ?><br />
<?php echo $csv[0][32]; ?><br />
eBay<br />
<?php echo $csv[0][2]; ?><br />
<?php echo $csv[0][0]; ?><br />
<?php echo $csv[0][1]; ?><br />
</td>
我想要做的是使用我为模板制作的 html 页面,并在 foreach 循环中回显来自每个订单的数据。我需要绘制出每个子数据元素在页面上的位置(如上例所示),但让它循环遍历每个订单,从我的模板(为每个订单)生成一个新页面,直到它到达末尾。希望这是有道理的,如果我说得不够清楚,请提出任何有助于澄清我想要实现的目标的问题。
注意:在我使用的示例数组中,第一个订单有 2 个项目,之后的 2 个订单每个订单有 1 个项目,这是典型的数据,一个订单可以有 1 个或多个项目。
你是说
<?php
foreach ($csv as $order) : ?>
<td style="vertical-align: top; width: 100%; text-align:right;">
<b>Name: </b><br />
<b>Phone: </b><br />
<b>Email: </b><br />
<b>Paid Via: </b><br />
<b>Sold Via: </b><br />
<b>User ID: </b><br />
<b>Sales Rec: </b><br />
<b>Order No.: </b>
</td>
<td style="vertical-align: top; width: 311px;"><?php echo $csv[0][3]; ?><br />
<?php echo $order[13]; ?><br />
<?php echo $order[4]; ?><br />
<?php echo $order[32]; ?><br />
eBay<br />
<?php echo $order[2]; ?><br />
<?php echo $order[0]; ?><br />
<?php echo $order[1]; ?><br />
</td>
<?php endforeach; ?>
顺便说一句,不要使用长期弃用的 <b>
标签(<strong>
是首选,如果那是你的意思;否则使用 CSS)和 <br>
不需要斜杠。
感谢 SO 社区的最出色帮助,我现在有一个自动生成的数组,看起来像这样..(看起来像真实数据但已匿名)
Array
(
[0] => Array
(
[0] => 15304
[1] => 15-04478-27247
[2] => mysuserid
[3] => eric smith
[4] => ericsfakeemail@gmail.com
[5] =>
[6] => 27 Any Close
[7] =>
[8] => Manchester
[9] => Greater Manchester
[10] => M14 2RB
[11] => United Kingdom
[12] => eric spilsbury
[13] => +44 7719 123456
[14] => 27 Any Close
[15] =>
[16] => Manchester
[17] => Greater Manchester
[18] => M14 2RB
[19] => United Kingdom
[20] => Array
(
[0] => 302804801330
[1] => 302804815489
)
[21] => Array
(
[0] => CAUTION DRIVEN BY A BIKER - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
[1] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
)
[22] =>
[23] => No
[24] => Array
(
[0] => 2
[1] => 1
)
[25] => Array
(
[0] => £3.90
[1] => £1.95
)
[26] => £0.00
[27] =>
[28] => £0.00
[29] => £0.00
[30] => £3.90
[31] => No
[32] => PayPal
[33] => 30-Jan-20
[34] => 30-Jan-20
[35] =>
[36] =>
[37] =>
[38] => 31-Jan-20
[39] => No
[40] =>
[41] =>
[42] => 5LD87965090833954U
[43] => Royal Mail 1st Class
[44] =>
[45] => Array
(
[0] => 1711351782020
[1] => 1711351781020
)
[46] =>
[47] => No
[48] =>
[49] => No
[50] =>
[51] => No
)
[1] => Array
(
[0] => 15121
[1] => 25-04445-36722
[2] => dopp69
[3] => mart podd
[4] => podfakeemail@gmail.com
[5] =>
[6] => Via Riccione N. 20/18
[7] =>
[8] => Sa Tiacca Quartu S. Elena
[9] => CA
[10] => 09847
[11] => Italy
[12] => Podd Mart
[13] => +39 342 000 9876
[14] => Via Riccione N. 30/32
[15] =>
[16] => Sa Tiacca Quartu S. Elena
[17] => CA
[18] => 09847
[19] => Italy
[20] => 302667176336
[21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
[22] =>
[23] => No
[24] => 2
[25] => £1.99
[26] => £3.14
[27] =>
[28] => £0.00
[29] => £0.00
[30] => £7.12
[31] => No
[32] => PayPal
[33] => 23-Jan-20
[34] => 23-Jan-20
[35] => 24-Jan-20
[36] =>
[37] =>
[38] => 24-Jan-20
[39] => Yes
[40] =>
[41] =>
[42] => 25C157351T667760U
[43] => Royal Mail International Standard
[44] =>
[45] => 1709118816020
[46] =>
[47] => No
[48] =>
[49] => No
[50] =>
[51] => No
)
[2] => Array
(
[0] => 15320
[1] => 20-04482-41274
[2] => wideboy3480
[3] => BEN ROYLES
[4] => wideboyemail@hotmail.co.uk
[5] => Yellow please
[6] => 10
[7] => Broomstick Close
[8] => Okehampton
[9] => Devon
[10] => EX20 1FT
[11] => United Kingdom
[12] => Ben Royles
[13] => +44 7876 401999
[14] => 105
[15] => Broomstick Close
[16] => Okehampton
[17] => Devon
[18] => EX10 1WT
[19] => United Kingdom
[20] => 302713568952
[21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
[22] =>
[23] => No
[24] => 1
[25] => £1.99
[26] => £0.00
[27] =>
[28] => £0.00
[29] => £0.00
[30] => £1.99
[31] => No
[32] => PayPal
[33] => 01-Feb-20
[34] => 01-Feb-20
[35] => 03-Feb-20
[36] =>
[37] =>
[38] =>
[39] => No
[40] =>
[41] =>
[42] => 7SD68673EE278782X
[43] => Royal Mail 1st Class
[44] =>
[45] => 1711667179020
[46] =>
[47] => No
[48] =>
[49] => No
[50] =>
[51] => No
)
<!---And so on--->
用于生成此代码的代码如下...
<?php
function readCSV($csvFile)
{
$line_of_text = [];
$file_handle = fopen($csvFile, 'r');
fgetcsv($file_handle);
fgetcsv($file_handle);
fgetcsv($file_handle);
while (!feof($file_handle)) {
$tmp = fgetcsv($file_handle, 1024);
if (isset($line_of_text[$tmp[0]])) {
foreach ($tmp as $k => $v) {
if (array_key_exists($k, $line_of_text[$tmp[0]])) {
if (!is_array($line_of_text[$tmp[0]][$k])) {
$kVal = $line_of_text[$tmp[0]][$k];
$line_of_text[$tmp[0]][$k] = [];
$line_of_text[$tmp[0]][$k][] = $kVal;
}
$line_of_text[$tmp[0]][$k][] = $v;
$line_of_text[$tmp[0]][$k] = array_unique($line_of_text[$tmp[0]][$k]);
$line_of_text[$tmp[0]][$k] = array_filter($line_of_text[$tmp[0]][$k]);
if (count($line_of_text[$tmp[0]][$k]) == 1) {
$line_of_text[$tmp[0]][$k] = array_values($line_of_text[$tmp[0]][$k]);
$line_of_text[$tmp[0]][$k] = $line_of_text[$tmp[0]][$k][0];
}
if (empty($line_of_text[$tmp[0]][$k])) {
$line_of_text[$tmp[0]][$k] = null;
}
} else {
$line_of_text[$tmp[0]][$k] = null;
}
}
$line_of_text[$tmp[0]][0] = $tmp[0];
} else {
$line_of_text[$tmp[0]] = $tmp;
}
}
fclose($file_handle);
return array_filter(array_values($line_of_text));
}
// Set path to CSV file
$csvFile = 'test.csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>
我想做的是使用这些自动生成的数组中的数据来自动生成我使用 html/php 标记制作的发票页。
我已经能够制作一个示例,以第一个条目 [0] 中的数据为目标,就像在这个示例片段中一样...
<td style="vertical-align: top; width: 100%; text-align:right;">
<b>Name: </b><br />
<b>Phone: </b><br />
<b>Email: </b><br />
<b>Paid Via: </b><br />
<b>Sold Via: </b><br />
<b>User ID: </b><br />
<b>Sales Rec: </b><br />
<b>Order No.: </b>
</td>
<td style="vertical-align: top; width: 311px;"><?php echo $csv[0][3]; ?><br />
<?php echo $csv[0][13]; ?><br />
<?php echo $csv[0][4]; ?><br />
<?php echo $csv[0][32]; ?><br />
eBay<br />
<?php echo $csv[0][2]; ?><br />
<?php echo $csv[0][0]; ?><br />
<?php echo $csv[0][1]; ?><br />
</td>
我想要做的是使用我为模板制作的 html 页面,并在 foreach 循环中回显来自每个订单的数据。我需要绘制出每个子数据元素在页面上的位置(如上例所示),但让它循环遍历每个订单,从我的模板(为每个订单)生成一个新页面,直到它到达末尾。希望这是有道理的,如果我说得不够清楚,请提出任何有助于澄清我想要实现的目标的问题。
注意:在我使用的示例数组中,第一个订单有 2 个项目,之后的 2 个订单每个订单有 1 个项目,这是典型的数据,一个订单可以有 1 个或多个项目。
你是说
<?php
foreach ($csv as $order) : ?>
<td style="vertical-align: top; width: 100%; text-align:right;">
<b>Name: </b><br />
<b>Phone: </b><br />
<b>Email: </b><br />
<b>Paid Via: </b><br />
<b>Sold Via: </b><br />
<b>User ID: </b><br />
<b>Sales Rec: </b><br />
<b>Order No.: </b>
</td>
<td style="vertical-align: top; width: 311px;"><?php echo $csv[0][3]; ?><br />
<?php echo $order[13]; ?><br />
<?php echo $order[4]; ?><br />
<?php echo $order[32]; ?><br />
eBay<br />
<?php echo $order[2]; ?><br />
<?php echo $order[0]; ?><br />
<?php echo $order[1]; ?><br />
</td>
<?php endforeach; ?>
顺便说一句,不要使用长期弃用的 <b>
标签(<strong>
是首选,如果那是你的意思;否则使用 CSS)和 <br>
不需要斜杠。