我有这个代码,它发送一封包含一些信息的电子邮件。消息部分如下所示:
protected override void OnCreate(Bundle savedInstanceState)
{
...
global::Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>()
.UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
我有一个名为$ events的数组,我希望将其包含在邮件中,但我希望将其显示为表格,如下所示
$mail->Body =<<<MSG
<html>
<head>
<title>Statistics</title>
</head>
<body>
The start was: {$start} </br></br>
The end was: {$end} </br></br>
</body>
</html>
MSG;
你能帮我解释一下foreach的语法吗?
答案 0 :(得分:2)
在heredocs之前准备数据:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.Generics.Collections, Classes, System.Zip;
type
TPersonne = class
strict private
FNom : string;
FPrenom: string;
public
property Nom: string read FNom write FNom;
property Prenom: string read FPrenom write FPrenom;
end;
TFamille = class(TDictionary<string, TPersonne>)
private
procedure LoadFromStream(stream: TStream);
procedure SaveToStream(stream: TStream);
public
procedure LoadFromFile(const AFileName: string);
procedure SaveToFile(const AFileName: string);
end;
procedure TFamille.SaveToStream(stream: TStream);
var
i: Integer;
Personne: TPersonne;
Pair: System.Generics.Collections.TPair<string, TPersonne>;
Writer: TWriter;
begin
Writer := TWriter.Create(stream, 4096);
try
Writer.WriteListBegin;
for Pair in Self do
begin
Writer.WriteString(Pair.Key);
Writer.WriteListBegin;
Personne := Pair.Value;
Writer.WriteString(Personne.Nom);
Writer.WriteString(Personne.Prenom);
Writer.WriteListEnd;
end;
Writer.WriteListEnd;
finally
Writer.Free;
end;
end;
procedure TFamille.LoadFromStream(stream: TStream);
var
Personne: TPersonne;
Reader: TReader;
sKey: string;
begin
Clear;
Reader := TReader.Create(stream, 1024);
try
Reader.ReadListBegin;
while not Reader.EndOfList do
begin
sKey := Reader.ReadString;
Personne := TPersonne.Create;
Reader.ReadListBegin;
while not Reader.EndOfList do
begin
Personne.Nom := Reader.ReadString;
Personne.Prenom := Reader.ReadString;
end;
Reader.ReadListEnd;
Add(sKey, Personne);
end;
Reader.ReadListEnd;
finally
Reader.Free;
end;
end;
procedure TFamille.LoadFromFile(const AFileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(AFileName, fmOpenRead);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TFamille.SaveToFile(const AFileName: string);
var
stream: TStream;
begin
stream := TFileStream.Create(AFileName, fmCreate);
try
SaveToStream(stream);
finally
stream.Free;
end;
end;
var
Famille: TFamille;
Personne: TPersonne;
begin
try
Famille := TFamille.Create;
try
Personne := TPersonne.Create;
Personne.Nom := 'Julie';
Personne.Prenom := 'Albertine';
Famille.Add('1', Personne);
Famille.SaveToFile('D:\famille.txt');
finally
FreeAndNil(Famille);
end;
Famille := TFamille.Create;
try
Famille.LoadFromFile('D:\famille.txt');
if Famille.Count > 0 then
Writeln(Famille['1'].Nom + ' ' + Famille['1'].Prenom);
finally
FreeAndNil(Famille);
end;
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
答案 1 :(得分:1)
您希望将事件显示为电子邮件中的表格。 你的foreach本身是正确的,但你在里面做的有一些问题。对你的问题最简单的解决方案如下(我从未使用过你正在使用的技术......我很确定foreach不能以这种方式执行):
$mail->Body =<<<MSG
<html>
<head>
<title>Statistics</title>
</head>
<body>
The start was: {$start} </br></br>
The end was: {$end} </br></br>
<table>
foreach ($events as $event)
{
echo '<tr>';
echo '<td>' . $event . '</td>';
echo '</tr>';
}
</table>
</body>
</html>
MSG;
如评论中所述,最好使用普通字符串:
$body = '
<html>
<head>
<title>Statistics</title>
</head>
<body>
The start was: '.$start.' </br></br>
The end was: '.$end.' </br></br>
<table>';
foreach ($events as $event)
{
$body .= '<tr><td>' . $event . '</td></tr>';
}
$body .= '</table>
</body>
</html>';
$mail->Body = $body;
我假设您的代码段echo '<td>' . $event)[1] . '</td>'; echo '<td>' . $event)[2] . '</td>'; echo '<td>' . $event)[3] . '</td>';
是您第一次尝试处理数组。如果$events
是多维的(数组数组),则必须替换
$body .= '<tr><td>' . $event . '</td></tr>';
带
$body .= '<tr><td>' . $event[1] . '</td><td>' . $event[1] . '</td><td>' . $event[1] . '</td></tr>';
请记住,数组索引以0开头