/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] SXML: Finding the greatest value

Name is assigned by $name
If the greatest number is under 10 it is assigned 1
If it’s under 20 its 2
and 30 is 3

Error on line 7 for invalid arg
rest will output 3s but no $name
I doubt either is right, anyone
mind pointing me in the right direction?

sample xml

[code]
<document>
<row>
<Col0></Col0 >
<Col1>Anthony Shetler</Col1 >
<Col2>-</Col2 >
<Col3></Col3 >
<Col4>-</Col4 >
<Col5></Col5 >
<Col6>-</Col6 >
<Col7></Col7 >
</row>
<row>
<Col0></Col0 >
<Col1>Anthony Van Engelen</Col1 >
<Col2>-</Col2 >
<Col3></Col3 >
<Col4>-</Col4 >
<Col5></Col5 >
<Col6>135</Col6 >
<Col7>http://www./catalog/product/view/id/106494/category/1352/</Col7 >
</row>
<row>
<Col0></Col0 >
<Col1>Antwuan Dixon</Col1 >
<Col2>-</Col2 >
<Col3></Col3 >
<Col4>186</Col4 >
<Col5>http://couchtour./aurora_sched.htm?utm_source=zumiez&amp;utm_medium=zumiez&amp;utm_content=event-schedule&amp;utm_campaign=ct-video-event-schedule</Col5 >
<Col6>163</Col6 >
<Col7>http://www./catalog/product/view/id/33367</Col7 >
</row>
<row>
<Col0></Col0 >
<Col1>Arto Saari</Col1 >
<Col2>-</Col2 >
<Col3></Col3 >
<Col4>-</Col4 >
<Col5></Col5 >
<Col6>-</Col6 >
<Col7></Col7 >
</row>
</document>
[/code]

php

[code=php]
<?php
$file = ‘1.xml’;

if (file_exists($file)) {
$sxml = simplexml_load_file($file);

foreach ($xml->document->row as $x){

$name = (string)$x->col1;
$x = (string)$x->col2;
$y = (string)$x->col4;
$z = (string)$x->col6;

$$maxValue=max($x,max($y,$z));

$rank=NULL; // blank the rank

if($maxValue <= ’10’){ $rank=’1′; }
if($maxValue <= ’20’){ $rank=’2′; }
if($maxValue <= ’30’){ $rank=’3′; }
if($maxValue > ’31’) { $rank=’0′; }

$name = (string)$name;
$rank = (string)$rank;

echo “$name is $rank<br />”;
}

} else {
exit(‘Failed to open $file.’);
}
?>
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 14.2010 — You use $sxml at line 5, but $xml at line 7. I think you'll also have problems within the foreach loop, as you are creating an object named $x in each iteration, then clobbering it with a scalar value on this line:
[code=php]
$x = (string)$x->col2;
[/code]

So the lines that follow for $y and $z will fail.
Copy linkTweet thisAlerts:
@ehimeauthorJul 14.2010 — Still having an issue on line 7 Nog,

is it something to do with the document->row maybe?

[code=php]
<?php
$file = '1.xml';

if (file_exists($file)) {
$sxml = simplexml_load_file($file);

foreach ($sxml->document->row as $key){

$name = $key->col1;
$x = $key->col2;
$y = $key->col4;
$z = $key->col6;

$$maxValue=max($x,max($y,$z));

$rank=NULL; // blank the rank

if($maxValue <= '10'){ $rank='1'; }
if($maxValue <= '20'){ $rank='2'; }
if($maxValue <= '30'){ $rank='3'; }
if($maxValue > '31') { $rank='0'; }

$name = (string)$name;
$rank = (string)$rank;

echo "$name is $rank<br />";
}

} else {
exit('Failed to open $file.');
}
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 14.2010 — Yeah, you don't need the top-level element "document". Just use $sxml->row.
Copy linkTweet thisAlerts:
@ehimeauthorJul 15.2010 — Why do you think that $name does not pull from col1 and maxValue always ends up as 3?
Copy linkTweet thisAlerts:
@ehimeauthorJul 15.2010 — I hope this can help someone else, here's were I finished at:

[code=php]
<?php
$file = '1.xml';

if (file_exists($file)) {
$s = simplexml_load_file($file);

foreach ($s->row as $s){

$name = $s->Col1;

$x = ($s->Col2 != '-') ? $s->Col2 : NULL;
$y = ($s->Col4 != '-') ? $s->Col4 : NULL;
$z = ($s->Col6 != '-') ? $s->Col6 : NULL;

$maxValue=max($x,$y,$z) . ' ';

$rank=NULL; // blank the rank

if($maxValue < 30){ $rank=3; }
if($maxValue < 20){ $rank=2; }
if($maxValue < 10){ $rank=1; }
if($maxValue > 31){ $rank=0;}
echo "$maxValue :: Name: $name Rank: $rank<br />";
}

} else {
exit('Failed to open $file.');
}
?>
[/code]
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...