0

Hi there,

I'am developing a snippet for a website widget that will read/fetch data from an XML file, sort it in array's and then show it back to the user - formated as a sentence.

Example:

XML sample:

<row id="1">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>5000</won>
</row>
<row id="2">
<username>GGR1111</username>
<name>Isabelle</name>
<surname>Bonnici</surname>
<game>Lotto</game>
<won>1000</won>
</row>
<row id="3">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>2000</won>
</row>
<row id="4">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>200</won>
</row>

So... I read that XML file line by line and put the values in an array, do some functions and then show the formatted data back to the user:

Formatted data example:

Raymond Saliba won $5000 from Lotto
Isabelle Bonnici won $1000 from Lotto
Raymond Saliba won $2000 from Lotto
Raymond Saliba won $200 from Lotto

(notice that 'Raymond Saliba' appears 3 times in this example)

now the problem is this -> I need to only show 'Raymond Saliba' once to the user (each person is unique by his username) and the string that I show to the user must be the one that contains the highest ammount won.

Proper output Example:

Raymond Saliba won $5000 from Lotto
Isabelle Bonnici won $1000 from Lotto

So the blueprint behind my explenation is something like this:

Person 1 ------> $5000
                 $2000
                 $200
Person 2 ------> $1000

(each person can have alot of values, and I need to show that user with the highest value - ONCE)

Sounds easy right ? -> not for me... I'am not a professional developer with PHP but I tend to get along just fine, I have tried various methods for this snippet but so far I didn't find any luck :(
I tried multidimensional arrays, array_unique, and some sorting functions but all ended with no luck at all...

Can someone that understands what I need give me some advice/help please ? preferably with example code using the sample data that I gave to you in this post (because it makes me understand it better, sometimes I'm a slow learner sorry)

Thank you very much and I'm awaiting your reply :)
Raymond Saliba

4
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by salibaray
0

first of my suggestion to you that why not u chose mysql to storing your data because in mysql there are too easy to find only one record of same name in higher value.
i suggest you that first you choose the mysql for your data storing.

0

first of my suggestion to you that why not u chose mysql to storing your data because in mysql there are too easy to find only one record of same name in higher value.
i suggest you that first you choose the mysql for your data storing.

Hi Hemgoyal,

Unfortunatley I cannot use mySQL because the client has specificly told me that he needs to feed it data through XML, this will be fed twice per week approx, and he doesn't want to complicate things with a database....

(just saying what he told me..... I would prefer easier method and if mySQL was the solution and I could make use of it then I definatley would) ;)

Thanks for your suggestion ;)

0

but you may also export these data in XML structure from phpmyadmin.
and also inserting and deleting of data in phpmyadmin is too easy and more secure. and also phpmyadmin is already provided with CPanel.

0

but you may also export these data in XML structure from phpmyadmin.
and also inserting and deleting of data in phpmyadmin is too easy and more secure. and also phpmyadmin is already provided with CPanel.

Hi Hemgoyal

This isn't a one off job, data input would have to be done on a regular basis if I derive to this method, the client needs to just upload an xml file to the system and then the system does all the work for him (cause he doesn't have time to input data to the db or anything, he just wants to upload in the morning and get results.... :( )

and wouldn't the whole process be a bit slower than if we used arrays ? (cause of connection, sorting etc) because the client wants speed as a priority so that his homepage loads fast on mobile phones too.... (its abit complicated this site lol)

Thanks Hemgoyal :)

0

This is a crude way to do it. The data isn't formatted properly so you will run into problems when people have the same name.

<?php

$xml =<<<XML
<data>
<row id="1">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>5000</won>
</row>
<row id="2">
<username>GGR1111</username>
<name>Isabelle</name>
<surname>Bonnici</surname>
<game>Lotto</game>
<won>1000</won>
</row>
<row id="3">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>2000</won>
</row>
<row id="4">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>200</won>
</row>
</data>
XML;

$xml = simplexml_load_string( $xml );

$data = array();
foreach( $xml->row as $row ) {
	$name = "{$row->name} {$row->surname}";
	if ( !isset( $data[$name] ) ) {
		$data[$name] = array(
			'username' => $row->username,
			'name' => $row->name,
			'surname' => $row->surname,
			'game' => $row->game,
			'won' => $row->won
		);
	}
	elseif ( $row->won > $data[$name]['won'] ) {
		$data[$name]['won'] = $row->won;
	}
}

foreach( $data as $name => $row ) {
	echo "{$name} won \${$row['won']} from {$row['game']}<br />";
}

?>
0

This is a crude way to do it. The data isn't formatted properly so you will run into problems when people have the same name.

<?php

$xml =<<<XML
<data>
<row id="1">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>5000</won>
</row>
<row id="2">
<username>GGR1111</username>
<name>Isabelle</name>
<surname>Bonnici</surname>
<game>Lotto</game>
<won>1000</won>
</row>
<row id="3">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>2000</won>
</row>
<row id="4">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>200</won>
</row>
</data>
XML;

$xml = simplexml_load_string( $xml );

$data = array();
foreach( $xml->row as $row ) {
	$name = "{$row->name} {$row->surname}";
	if ( !isset( $data[$name] ) ) {
		$data[$name] = array(
			'username' => $row->username,
			'name' => $row->name,
			'surname' => $row->surname,
			'game' => $row->game,
			'won' => $row->won
		);
	}
	elseif ( $row->won > $data[$name]['won'] ) {
		$data[$name]['won'] = $row->won;
	}
}

foreach( $data as $name => $row ) {
	echo "{$name} won \${$row['won']} from {$row['game']}<br />";
}

?>

----------------------------------------

Hi Kkeith29

Would there be a way to make this work with the username instead of Name and Surname ? cause username is the unique ID.

Thanks

0

lol funny how things happen sometimes Keith :)

Edited by salibaray: n/a

0

Hi ardav

Hmm never hear of xsl before but sounds interesting, I'll check it out thanks :)

this just came ot my head: but is xsl for Microsoft based technologies only ? (cause th elink you pointed out is at MSDN so I think its for ASP and ASP.NET no )

thanks :)

Edited by salibaray: n/a

0

No xsl is independent of server side language - or should be.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.