jmp98 Posted September 11, 2015 Share Posted September 11, 2015 (edited) what part of this code do i need to edit in order for the funder with the second highest percentage to be returned?? function get_donated_list() { global $post; $content = '';$args = array('post_type' => 'funder','post_status' => 'publish', // optional 'posts_per_page' => -1); $lists = get_posts($args);$highest=0;$highest_id=0; $completed = array();$i=0;if($lists) {foreach($lists as $list) {$list_id = $list->ID;$goal = $this->get_goal_amount($list_id);$total = $this->get_amount_raised($list_id);$percent=($total/$goal)*100; if($highest < $percent && $percent < 100 ){$highest = $percent;$highest_id = $list_id;}if($percent==100){$completed[$i] = $list_id;$i++;}}} if($highest_id){$content=get_the_title( $highest_id )." ".round($highest, 2).'%'; } return $content;} i have tried changing a few parts in the code but it still returns the id witht the highest percentage EDIT i have had a look around myself and i think what i actually want is a foreach with a limit of 3 loops but i don't know where to add it. i want it to loop the process so that it returns the three highest percentages one error in the code is: if($percent==100) should have actually read if(0 < $percent) Edited September 11, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 11, 2015 Share Posted September 11, 2015 You could create a second set of variables to hold the second highest number. Then whenever there is a new higher number, the old highest number becomes the second highest. You could try something like this: $highest=0; $highest_id=0; $second_highest=0; $second_highest_id=0; //... if($highest < $percent && $percent < 100) { //THE HIGHEST NUMBER IS NOW THE SECOND HIGHEST $second_highest = $highest; $second_highest_id = $highest_id; //STORE NEW HIGHEST NUMBER $highest = $percent; $highest_id = $list_id; } Quote Link to comment Share on other sites More sharing options...
jmp98 Posted September 11, 2015 Author Share Posted September 11, 2015 (edited) will this return three values though. for example if i had a=10/100, b=2/10, c=20/50, d=1.5/10, e=3/5, f=5/100, g=33/100 i want the function to return content as e 60% c 40% g 33% this data is obviously just random example data where it would be $total/$goal in its current format it just returns e 60% Edited September 11, 2015 by jmp98 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 11, 2015 Share Posted September 11, 2015 this will get your top 3 class myItem { public $id; public $goal; public $total; public function __construct($id, $total, $goal) { $this->id = $id; $this->total = $total; $this->goal = $goal; } } // array of objects $list = array( new myItem('a', 10, 100), new myItem('b',2,10), new myItem('c',20,50), new myItem('d',1.5,10), new myItem('e',3,5), new myItem('f',5,100), new myItem('g',33,100) ); //sort the array by percent DESC usort ($list, function($a, $b) { $apc = 100*$a->total/$a->goal; $bpc = 100*$b->total/$b->goal; return $bpc - $apc; }); // get the first three elements $top3 = array_slice($list,0,3); // list result echo '<pre>'; foreach ($top3 as $item) { printf("%s %2.0f%%\n", $item->id, $item->total / $item->goal * 100); } echo '</pre>'; gives e 60% c 40% g 33% Quote Link to comment Share on other sites More sharing options...
jmp98 Posted September 12, 2015 Author Share Posted September 12, 2015 (edited) sorry i'm relatively new to php and i'm not sure how to integrate the code you have given me into the original code. my original code returns a variable value and manages to get the highest value without sorting the data. can someone explain to me how this works or how to integrate the codes together so that when the variable data is inputted from the below code, i get the highest three percentages outputted in the same format as the $content in the original code. $args = array( 'post_type' => 'funder','post_status' => 'publish', // optional'posts_per_page' => -1);$lists = get_posts($args); as you can see in the original code, $lists becomes the new array - or have i misunderstood something?? sorry if i'm horribly confusing!!! Edited September 12, 2015 by jmp98 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 13, 2015 Share Posted September 13, 2015 You are not expected to just copy my code and paste it into yours. The purpose of the code was to set up a situation similar to yours and show you an example of how you can process it. Quote Link to comment Share on other sites More sharing options...
jmp98 Posted September 14, 2015 Author Share Posted September 14, 2015 Yes i realise that, i have been trying to integrate parts of it but am struggling to get it to work successfully. i have tried a number of different ways getting different ideas from the code you provided but it never seems to work properly. Quote Link to comment Share on other sites More sharing options...
hansford Posted September 14, 2015 Share Posted September 14, 2015 define('br','<br />'); $lists = array( array('id'=>'g','total'=>33,'goal'=>100), array('id'=>'c','total'=>20,'goal'=>50), array('id'=>'e','total'=>3,'goal'=>5)); $mid_array = array(); $highest_id = ''; $i = 0; foreach($lists as $list) { $id = $list['id']; $total = $list['total']; $goal = $list['goal']; $percent = ($total / $goal) * 100; $mid_array[$i]['id'] = $id; $mid_array[$i]['percent'] = $percent; $i++; } usort($mid_array, function($a, $b){ $c = $a['percent']; $d = $b['percent']; if($c == $d) { return 0; } if($c < $d) { return 1; } return -1; }); foreach($mid_array as $arg) { echo $arg['id'] . ' ' . $arg['percent'] . '%' . br; } Quote Link to comment Share on other sites More sharing options...
Solution jmp98 Posted September 16, 2015 Author Solution Share Posted September 16, 2015 Hello everyone. thanks for your help on this topic. after trying a few different things and talking to some other people, this is the final code i came up with which works successfully global $post; $content = ''; $args = array( 'post_type' => 'funder', 'post_status' => 'publish', // optional 'posts_per_page' => -1 ); $lists = get_posts($args); $highest_ascend=array(); $highest=0; $highest_id=array(); $completed = array(); $i=0; $j=0; if($lists) { foreach($lists as $list) { $list_id = $list->ID; $goal = $this->get_goal_amount($list_id); $total = $this->get_amount_raised($list_id); $percent=($total/$goal)*100; if($percent < 100 && $percent > 0 ) { $highest_ascend[$j]= $percent; $highest_id[$j] = $list_id; $j++; } if($percent==100) { $completed[$i] = $list_id; $i++; } } } $num = count($highest_ascend); for($m=0; $m < $num; $m++ ) { for ( $n=$m; $n < $num; $n++ ) { if($highest_ascend[$m] < $highest_ascend[$n]) { $t1= $highest_ascend[$m]; $highest_ascend[$m]=$highest_ascend[$n]; $highest_ascend[$n]=$t1; $t2=$highest_id[$m]; $highest_id[$m]=$highest_id[$n]; $highest_id[$n]=$t2; } } } for($i=0;$i<3;$i++) { $content.=get_the_title( $highest_id[$i] )." ".round($highest_ascend[$i], 2).' % <br>'; } return $content; } thanks for your suggestions anyway - very helpful Quote Link to comment 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.