simplexml Conditional inside echo

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
ninefox888
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Apr 19, 2018 4:14 am

Thu Apr 19, 2018 4:22 am

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 the element field that has the ad title moves depending on the ad.

here is a sample of the xml

Code: Select all

    <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

Code: Select all

 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[1]->value . "<br>"; <<-- issue
        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)

I tried adding below but it's not working

Code: Select all

echo "Issue" . (($item->fields->field->name == 'AdTitle') ? $item->fields->field->value :'')."<br>";
Appreciate if someone can advice many thanks

Post Reply