How to get the 1st element from an array (in XML format)?

Archived from the Xataface Users forum.

angie — Fri Mar 13, 2009 10:07 am

Hi Steve,

I have a phone_numbers array (in XML format), I tried to get the 1st element from this array, but I failed.

I also tried to loop through the phone_numbers array:

{foreach from=$person.phone_numbers item=number key=type}

{$number.name}: {$number.number}

{/foreach}

I did not get any data.

Could you please advise me on both issues?

Thank you very much.

Angie


angie — Fri Mar 13, 2009 2:27 pm

In my phone_numbers field, I have the following information:

<XML\_Serializer\_Tag> Phone 778-782-9999 </XML\_Serializer\_Tag> <XML\_Serializer\_Tag> Fax 778-782-1111 </XML\_Serializer\_Tag>

I want to get: Phone 778-782-9999 only.

I would appreciate if anyone could give me a hint on this.

Thanks.

Angie


shannah — Fri Mar 13, 2009 2:59 pm

The best way to retrieve this information is via the Dataface_Record object. But in your example, you’re just working with an array that you loaded from the database.

In the Physics_Site class where you load the rows, you need to wrap the row in a Dataface_Record object to retrieve the values as an array.

e.g.

$rec = new Dataface_Record(‘people’, $row);

$phoneNumbers = $rec->val(‘phone_numbers’);


angie — Fri Mar 13, 2009 3:49 pm

Hi Steve,

Thank you for your quick reply. I have fixed it.

Angie