simplexml中的条件内部回声

时间:2018-04-19 02:35:42

标签: php simplexml

我使用simplexml从xml获取数据我有3个条件,类别必须是jazz,包必须是最好的,日期必须是今天。那个问题已经解决了我的问题是回应标题,因为广告标题的字段会根据广告而移动。

这是xml的样本

    <product>
    <export>
        <ad>
        <category>
            <name>Jazz</name>
        </category>
        <package>
            <name>Good</name>
            <start_date>2018-04-18</start_date>
            <adcopy>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nec consequat ante. Aenean suscipit imperdiet velit, sed congue felis congue vel. Maecenas non leo at tortor ornare iaculis.</adcopy>
        </package>
        </ad>
        <fields>
          <field>
            <name>Color</name>
            <value>white/blue</value>
          </field>
          <field>
            <name>AdTitle</name>
            <value>This is the Title of my 1st item</value>
          </field>
          <field>
            <name>Make</name>
            <value>Plastic</value>
          </field>
        </fields>
    </export>
    <export>
        <ad>
        <category>
            <name>RNB</name>
        </category>
        <package>
            <name>Good</name>
            <start_date>2018-04-17</start_date>
            <adcopy>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nec consequat ante. Aenean suscipit imperdiet velit, sed congue felis congue vel. Maecenas non leo at tortor ornare iaculis.</adcopy>
        </package>
        </ad>
        <fields>
          <field>
            <name>AdTitle</name>
            <value>This is the Title of my 2nd item</value>
          </field>
          <field>
            <name>Color</name>
            <value>white/blue</value>
          </field>
          <field>
            <name>Make</name>
            <value>Plastic</value>
          </field>
        </fields>
    </export>
</product>

这是脚本

    foreach ($data->export as $item)
{
    if ($item->ad->category->name == 'Jazz' && $item->ad->package->name == 'Good' && $item->ad->package->start_date == $currentday)
    {
        echo "Title: " . $item->fields->field->value . "<br>";
        echo "Description: " . $item->ad->package->adcopy->value . "<br>";
        echo "Date: " . $item->ad->package->start_date . "<br>";

    }
}

我该怎么做? 如果该字段的名称为AdTitle,请使用该字段并显示field-&gt;值(该广告的标题)

请帮忙

我尝试在下面添加,但它无效

echo "Issue" . (($item->fields->field->name == 'AdTitle') ? $item->fields->field->value :'')."<br>";

我能够在下面使用

{
            foreach ($item->fields->field as $field)
                {
                    if ($field->name == 'AdTitle')
                        {
                            echo "Title: " . $field->value . "<br>";

0 个答案:

没有答案