dropfaith Posted May 23, 2012 Share Posted May 23, 2012 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/ Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1347984 Share on other sites More sharing options...
dropfaith Posted May 23, 2012 Author Share Posted May 23, 2012 yes display the info the other way right now im just echoing xml ie the entire feed until i logic out a better way to handle this Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348008 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 Parse it into an array and use array_reverse Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348016 Share on other sites More sharing options...
dropfaith Posted May 23, 2012 Author Share Posted May 23, 2012 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>"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348034 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 Well no. I meant parse each XML entry into some array-type format. You'll want to look up a XML parser and get that done first. It's pretty easy if you Google PHP XML Parser. Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348061 Share on other sites More sharing options...
dropfaith Posted May 23, 2012 Author Share Posted May 23, 2012 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348064 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 foreach (array_reverse($approved) as $approve) Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348076 Share on other sites More sharing options...
dropfaith Posted May 23, 2012 Author Share Posted May 23, 2012 Warning: array_reverse() [function.array-reverse]: The argument should be an array in /home/crimso/domains/crimsontrauma.com/public_html/template/rightnav.php on line 38 Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348090 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 Oh oops. Yeah, you are missing parts of code there. After you parsed the XML, loop through and store them into array instead of echo-ing it out. Then loop the array to print it out. Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348098 Share on other sites More sharing options...
dropfaith Posted May 23, 2012 Author Share Posted May 23, 2012 I wont lie i legit have no idea what your talking about on that lol. Im pretty much teaching myself this as i go i dont use php ever.. Started like last night ish Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348132 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348138 Share on other sites More sharing options...
dropfaith Posted May 23, 2012 Author Share Posted May 23, 2012 yay your awesome ty Quote Link to comment https://forums.phpfreaks.com/topic/262998-sort-and-limit/#findComment-1348148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.