duan111112 2018-04-19 02:35
浏览 34

simplexml中的条件内部回声

I am using simplexml to get the data from an xml I have 3 conditions the category must be jazz the package must be best and the date must be today. That one is sorted out my problem is echoing the title since field that has the ad title moves depending on the ad.

here is a sample of the 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>

And here is the script

    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>";

    }
}

How can I do this? if the field has a name of AdTitle, use that field and show the field->value (which is the title of the ad)

Please help

I tried adding below but it's not working

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

I was able to make it work with below

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

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了