如何将循环存储为变量中的字符串?

时间:2016-01-27 22:00:31

标签: php xml

我的代码是这样的:

<?php
        $HCode = 'IDJKT01';
        $CheckIn = '2016-02-01';
        $CheckOut = '2016-02-02';
        $Sgl = 1;
        $Dbl = 0;
        $Trp = 0;
        $RmGrade = 'DELUXE';

        $xml = "<?xml version='1.0' encoding='utf-8' ?>

                            <Request>

                                <BookingRequest>                   

                                    <HCode>".$HCode."</HCode>

                                    <RmGrade>".$RmGrade."</RmGrade>

                                    <CheckIn>".$CheckIn."</CheckIn>

                                    <CheckOut>".$CheckOut."</CheckOut> 

                                    <Sgl>".$Sgl."</Sgl> 

                                    <Dbl>".$Dbl."</Dbl> 

                                    <Trp>".$Trp."</Trp> 

                                    <Passenger Room='SGL'>

                                        <PassSex>M</PassSex>

                                        <PassLocalNm>Becks</PassLocalNm>

                                        <PassFirstNm> David </PassFirstNm>

                                        <PassLastNm> Beckham</PassLastNm>

                                    </Passenger>

                                </BookingRequest>

                            </Request>";
?>

我想在字符串中添加循环。

例如,如上所述, 如果$ sgl = 1,则乘客标签的数量为1

其他例子如$ sgl = 3,那么乘客标签的数量是3

其他例子如$ double = 1,那么乘客标签的数量是2

任何帮助非常感谢

干杯

1 个答案:

答案 0 :(得分:1)

尝试一下:

        $HCode = 'IDJKT01';
        $CheckIn = '2016-02-01';
        $CheckOut = '2016-02-02';
        $Sgl = 0;
        $Dbl = 1;
        $Trp = 0;
        $RmGrade = 'DELUXE';

        $xml = "<?xml version='1.0' encoding='utf-8' ?>\n".
                "\t<Request>\n".
                "\t\t<BookingRequest>\n".
                "\t\t\t<HCode>".$HCode."</HCode>\n".
                "\t\t\t<RmGrade>".$RmGrade."</RmGrade>\n".
                "\t\t\t<CheckIn>".$CheckIn."</CheckIn>\n".
                "\t\t\t<CheckOut>".$CheckOut."</CheckOut> \n".
                "\t\t\t<Sgl>".$Sgl."</Sgl>\n". 
                "\t\t\t<Dbl>".$Dbl."</Dbl> \n".
                "\t\t\t<Trp>".$Trp."</Trp>\n";
for($i=0; $i< ($Sgl ? 1 : ($Dbl ? 2 : ($Trp ? 3 : 0))); $i++) {
        $xml .= "\t\t\t<Passenger Room=".($Sgl ? "'SGL'" : ($Dbl ? "'DBL'" : ($Trp ? "'TRP'" : "'SGL'"))).">\n".
                "\t\t\t\t<PassSex>M</PassSex>\n".
                "\t\t\t\t<PassLocalNm>Becks</PassLocalNm>\n".
                "\t\t\t\t<PassFirstNm>David</PassFirstNm>\n".
                "\t\t\t\t<PassLastNm>Beckham</PassLastNm>\n".
                "\t\t\t</Passenger>\n";
}
$xml .= "\t\t</BookingRequest>\n".
        "</Request>";

结果:

<Request>
        <BookingRequest>
            <HCode>IDJKT01</HCode>
            <RMGrade>DELUXE</RMGrade>
            <CheckIn>2016-02-01</CheckIn>
            <CheckOut>2016-02-02</CheckOut> 
            <Sgl>0</Sgl>
            <Dbl>1</Dbl> 
            <Trp>0</Trp>
            <Passenger Room="DBL">
                <PassSex>M</PassSex>
                <PassLocalNm>Becks</PassLocalNm>
                <PassFirstNm>David</PassFirstNm>
                <PassLastNm>Beckham</PassLastNm>
            </Passenger>
            <Passenger Room="DBL">
                <PassSex>M</PassSex>
                <PassLocalNm>Becks</PassLocalNm>
                <PassFirstNm>David</PassFirstNm>
                <PassLastNm>Beckham</PassLastNm>
            </Passenger>
        </BookingRequest>
</Request>

就个人而言,我会像这样重新格式化XML:

<Request>
        <BookingRequest>
            <HCode>IDJKT01</HCode>
            <RMGrade>DELUXE</RMGrade>
            <CheckIn>2016-02-01</CheckIn>
            <CheckOut>2016-02-02</CheckOut> 
            <Sgl>0</Sgl>
            <Dbl>1</Dbl> 
            <Trp>0</Trp>
            <Passengers>
                <Passenger Room="DBL">
                    <PassSex>M</PassSex>
                    <PassLocalNm>Becks</PassLocalNm>
                    <PassFirstNm>David</PassFirstNm>
                    <PassLastNm>Beckham</PassLastNm>
                </Passenger>
                <Passenger Room="DBL">
                    <PassSex>M</PassSex>
                    <PassLocalNm>Becks</PassLocalNm>
                    <PassFirstNm>David</PassFirstNm>
                    <PassLastNm>Beckham</PassLastNm>
                </Passenger>
            </Passengers>
        </BookingRequest>
</Request>