Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-05-2007, 09:58 PM   PM User | #1
steviecee
New Coder

 
Join Date: Apr 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
steviecee is an unknown quantity at this point
Problem getting unique values from an XML file

Hi,
I'm using an XML file to populate a form select box. the XML file has numerous values that I'm populating the form with, some of which are repeating themselves. I want to have unique values in the form so no values are repeated.
for example
In XML doc
Code:
<parent>
 <child>1</child>
 <child>1</child>
 <child>2</child>
 <child>1</child>
</parent>
XHTML

Code:
...
echo "<option value = ''>Please pick a number</option> ";
	foreach ($entries as $entry) 
	{
	   echo "<option value = '{$entry->nodeValue}'>{$entry->nodeValue}</option>";
         }
...
this will bring back the sequence 1 1 2 1 in the select box, is is possible for it to read 1 2, by removing / stopping duplication?


thanks in advance for any help you may be able to give!
Steve
steviecee is offline   Reply With Quote
Old 01-05-2007, 10:53 PM   PM User | #2
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
This is more of a PHP question than XML, IMO. But even knowing close to nothing about PHP I'll take a shot. :)

Code:
$i = 0;
foreach ($entries as $entry) {
  $arr[$i++] = $entry->nodeValue;
}
sort($arr);
$last_value = -1;
for ($j = 0; $j < $i; $j++) {
  if ($arr[$j] != $last_value) {
    echo "<option value = '{$arr[$j]}'>{$arr[$j]}</option>";
    $last_value = $arr[$j];
  }
}
No doubt there are all sorts of errors in there since it is about my first php code ever but this is one idea for how to strip out duplicates.

david_kw

Last edited by david_kw; 01-06-2007 at 10:21 PM.. Reason: saw a couple missing $s
david_kw is offline   Reply With Quote
Old 01-06-2007, 09:51 AM   PM User | #3
steviecee
New Coder

 
Join Date: Apr 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
steviecee is an unknown quantity at this point
David,
Thanks for that, its very close now, just a bit of playing around should get it right

cheers
Steve
steviecee is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:01 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.