devWhiz Posted May 16, 2011 Share Posted May 16, 2011 <?php $HitlistHeader = file_get_contents(url); $Hitlist = simplexml_load_string($HitlistHeader); foreach($Hitlist->xml->entry as $entry) { foreach($entry->target_user as $target) { $TargetID=$target->user_id; $TargetName=urldecode($target->mob_name); } foreach($entry->paid_user as $paiduser) { $PaidID=$paiduser->user_id; $PaidName=urldecode($paiduser->mob_name); } $Amount=$entry->amount; $TimeAgo=$entry->placed_time_ago; $Level=$entry->level; } for($cwb=1; $cwb!=count($entry); $cwb++) { echo $TargetID[$cwb]."\n"; echo $TargetName[$cwb]."\n"; echo $PaidID[$cwb]."\n"; echo $PaidName[$cwb]."\n"; echo $Amount[$cwb]."\n"; echo $TimeAgo[$cwb]."\n"; echo $Level[$cwb]."\n\n"; } sleep(100000); ?> it only echoes one character on only a few of the variables, I dont know what the problem could be.. Anyhelp to resolve this is appreciated Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/ Share on other sites More sharing options...
wepnop Posted May 16, 2011 Share Posted May 16, 2011 <?php $HitlistHeader = file_get_contents(url); $Hitlist = simplexml_load_string($HitlistHeader); foreach($Hitlist->xml->entry as $entry) { foreach($entry->target_user as $target) { $TargetID=$target->user_id; $TargetName=urldecode($target->mob_name); } foreach($entry->paid_user as $paiduser) { $PaidID=$paiduser->user_id; $PaidName=urldecode($paiduser->mob_name); } $Amount=$entry->amount; $TimeAgo=$entry->placed_time_ago; $Level=$entry->level; } for($cwb=1; $cwb!=count($entry); $cwb++) { echo $TargetID[$cwb]."\n"; echo $TargetName[$cwb]."\n"; echo $PaidID[$cwb]."\n"; echo $PaidName[$cwb]."\n"; echo $Amount[$cwb]."\n"; echo $TimeAgo[$cwb]."\n"; echo $Level[$cwb]."\n\n"; } sleep(100000); ?> it only echoes one character on only a few of the variables, I dont know what the problem could be.. Anyhelp to resolve this is appreciated try using for with count for all, it helped me in a few ocasions with xml simple objects and loops. Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1216098 Share on other sites More sharing options...
devWhiz Posted May 16, 2011 Author Share Posted May 16, 2011 what do you mean? Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1216280 Share on other sites More sharing options...
requinix Posted May 17, 2011 Share Posted May 17, 2011 What's your XML? Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1216300 Share on other sites More sharing options...
wepnop Posted May 17, 2011 Share Posted May 17, 2011 what do you mean? dont use this: foreach($entry->target_user as $target) use for($i=0; $i<=count($entry->target_user)-1; $i++) and then i think it was this to extract: $entry->target_user[$i]; ¿The reason? Another informatic and unsolved mistery, but i was to suspend a php exam with the foreach loop with simple xml and then i changued all to this and i passed it. It was giving me strange problems not iterating well. Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1216389 Share on other sites More sharing options...
dreamwest Posted May 17, 2011 Share Posted May 17, 2011 Your XML probably has CDATA in it http://www.wizecho.com/nav=php&s=xml Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1216404 Share on other sites More sharing options...
devWhiz Posted May 19, 2011 Author Share Posted May 19, 2011 the xml I am dealing with has multiple "entry"s here is the code <xml> <entry> <target_user> <user_id>467180156</user_id> <mob_name>Insuccesso%20your%2C%20Level%201000</mob_name> </target_user> <paid_user> <user_id>533455151</user_id> <mob_name>DONT%20DO%20IT30</mob_name> </paid_user> <amount>12001680000</amount> <is_npc>false</is_npc> <placed_time_ago>3 hours ago</placed_time_ago> <level>1000</level> <location_name>New York City</location_name> </entry> <entry> <target_user> <user_id>288050627</user_id> <mob_name>LITTLE%20PRIME%20TIME%2C%20Level%20993</mob_name> </target_user> <paid_user> <user_id>446419622</user_id> <mob_name>KG%20THE%20BODACIOUS</mob_name> </paid_user> <amount>14328480000</amount> <is_npc>false</is_npc> <placed_time_ago>3 hours ago</placed_time_ago> <level>993</level> <location_name>New York City</location_name> </entry> </xml> I need to put target_user->user_id into a loop and the same for paid_user->user_id etc The code in the first post it only prints the first letter and it confusing me, any help is appreciated Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1217471 Share on other sites More sharing options...
devWhiz Posted May 19, 2011 Author Share Posted May 19, 2011 anyone? Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1217572 Share on other sites More sharing options...
dreamwest Posted May 19, 2011 Share Posted May 19, 2011 XMLs are just like arrays, you point to the keys you want $data = @file_get_contents("wiz.xml"); $n_data = new SimpleXmlElement($data, LIBXML_NOCDATA); foreach ($n_data->xml->entry as $d) { $show .= "Userid: {$d->target_user->user_id} Mob: {$d->target_user->mob_name} Paid user: {$d->paid_user->user_id}<br>"; } echo $show; Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1217683 Share on other sites More sharing options...
devWhiz Posted June 5, 2011 Author Share Posted June 5, 2011 hmm, Well Ive tried to work with it but again I am still getting it to display only the first letter of each.. I need to be able to define each UserID has $UserID[$i] any ideas? Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1225366 Share on other sites More sharing options...
devWhiz Posted June 5, 2011 Author Share Posted June 5, 2011 anyone? Link to comment https://forums.phpfreaks.com/topic/236531-simplexml-for-loop-help/#findComment-1225394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.