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
https://forums.phpfreaks.com/topic/262998-sort-and-limit/
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
https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1347984
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
https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348034
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
https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348064
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
https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348138
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.