Jump to content

Sort and limit


dropfaith

Recommended Posts

All i need now is to reverse the feed and cut it to like 20 posts.

http://box1.host1free.com/~crimso/index.php  this link is my live test i have the xml set in the way i want it looking but i cant for the  life of me reverse and limit it

 

<?php
function getPage($url="http://www.treasuretrooper.com/api/116fcbdb1cac242e789a892ebd2a0abb/approved/xml"){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_REFERER,'http://www.treasuretrooper.com/');
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$xml=curl_exec($ch);
if($xml==false){
  $m=curl_error(($ch));
  error_log($m);
}
curl_close($ch);
return $xml;
}
$xml=getPage("http://www.treasuretrooper.com/api/116fcbdb1cac242e789a892ebd2a0abb/approved/xml");

echo $xml;

?>

Link to comment
Share on other sites

Reverse the feed? Does that mean displaying the same list, but backwards? Like this?

 

Energy Conservation

Internet Phone Services

WML - Gas Card

Flash Forward

WS - Burger King

Samsung Galaxy Note

 

Also provide that code that you're using to display the feed. That's just the function to get the XML data.

Link to comment
Share on other sites

Okay So ive never had to build an array from an external source xml document So excuse this question.

I tried the code below

Got an array but as you can imagine the entire file flags as key 0.  Where as i need it to seperate the items  if i plot to limit and reverse its direction.

 

<?php
$xmlfile = getPage("http://www.treasuretrooper.com/api/116fcbdb1cac242e789a892ebd2a0abb/approved/xml");


$xmlf = array("$xmlfile");
print_r(array_reverse($xmlf));
foreach($xmlf as $Item => $Name) {
echo $Item . " " . $Name . "<br>";
}?>

Link to comment
Share on other sites

ehh  other then reversing it im good now  i think im still way off on the direction i should have gone tho

 

<?php
function getPage($url="http://www.treasuretrooper.com/api/116fcbdb1cac242e789a892ebd2a0abb/approved/xml"){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_REFERER,'http://www.treasuretrooper.com/');
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$xml=curl_exec($ch);
if($xml==false){
  $m=curl_error(($ch));
  error_log($m);
}
curl_close($ch);
return $xml;
}


$xmlfile = getPage("http://www.treasuretrooper.com/api/116fcbdb1cac242e789a892ebd2a0abb/approved/xml");
$approved =new SimpleXMLElement($xmlfile);
$i = 0;
echo <<<EOF
<h2>Approving Now</h2>
<ul>
EOF;

foreach($approved as $approve) // loop through our Offers
{  		
	$i++;
        echo <<<EOF
        <li>{$result->Name}</li>
EOF;
if($i >= 15)
	break;
}
echo <<<EOF
</ul>
EOF;


?>


Link to comment
Share on other sites

Okay no problem.

 

The point is that XML by itself can't be sorted the way you want. So you have to use another medium (like an array). It's more annoying than it is difficult.

 

1. Delete everything you have below $approved = new SimpleXMLElement($xmlfile);;

2. Create an array, let's say: $results = array();

3. Loop through $approved as you already had it: foreach ($approved as $approve)

4. Now for each $approve, store its data in $results. Preferably just the data you need, but to keep this simple, just add the whole $approve to $results as so: $results[] = $approve;

5. Finally, loop through the $results array, but in reverse order: foreach (array_reverse($results) as $result). Inside this for loop, you want put those echo statements that you had.

 

Does that help?

Link to comment
Share on other sites

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.