Jump to content

SimpleXML for loop help


devWhiz

Recommended Posts

<?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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.