digitex Posted September 2, 2013 Share Posted September 2, 2013 I have stepped out of my league. I have a competition results page that I'm working on. It's an agricultural fair with a largest vegetable weighoff and since each grower entering the competition usually has multiple entries I opted to create a grower page with name, location, contact etc. for each grower and then a repeater to document each entry. The repeater includes a category page select in radio button form so staff can input the category of veg with a single click, plus a results field for the weight. Then if the same grower has another entry they just click the add item button and don't have to keep entering the grower's personal information. I need to output the data and organize by category first then the weight in descending order. Since the category is in a repeater I created a new array and added in the relevant data for all the growers and their entries. Then I used a trick I found here to sort using usort first by category then result (weight) and it works. Code is here: <?php $ag = $pages->find("template=grower, $selector"); $wor = array(); foreach($ag as $g) { foreach($g->$weighoff as $gent) { ++$i; $wor[$i]->cat = $gent->grower_category; $wor[$i]->res = $gent->grower_result; $wor[$i]->ln = $g->grower_lname; $wor[$i]->fn = $g->grower_fname; $wor[$i]->twn = $g->grower_town; $wor[$i]->pst = $g->grower_province; $wor[$i]->oth = $gent->grower_otherspecific; $wor[$i]->lth = $gent->length; } } if($weighoff == grower_entry_A) { echo "<h2>Saturday Weighoff results for {$fyear}</h2>"; } else { echo "<h2>Sunday Weighoff results for {$fyear}</h2>"; } echo "<table id=\"reslist\">\n"; echo "<tr><th>Category</th><th>Name</th><th>City, Prov/State</th><th>Result</th></tr>\n"; function sort_results_by_category(&$wor) { usort($wor, function($a, $b) { if($b->cat == $a->cat) return $b->res > $a->res; else return strcmp($a->cat, $b->cat); }); } sort_results_by_category($wor,array("cat","res")); foreach($wor as $result) { echo "<tr><td>"; if($result->oth) { echo "<span class=\"catem\">{$result->cat->title}: {$result->oth}</span>"; } else { echo "<span class=\"catem\">{$result->cat->title}</span>"; } echo "</td><td class=\"resname\">{$result->ln}, {$result->fn}</td><td class=\"resloc\">{$result->twn}, {$result->pst}</td>"; if($result->lth) { echo "<td class=\"resnum\"><strong>{$result->res} inches</strong></td></tr>\n"; } else { echo "<td class=\"resnum\"><strong>{$result->res} lbs.</strong></td></tr>\n"; } } echo "</table>\n"; ?> It displays like this: Category1, name, location, result Category1, name, location, result Category2, name, location, result Category2, name, location, result etc. The problem is they want it to display like this: Category1 name, location, result name, location, result Category2 name, location, result name, location result etc. And I've tried everything (in my limited arsenal) and I can't figure out how to organize the output and I can't even decide if it might be a simple thing I'm not seeing or if I have to rethink my whole approach. Can anyone see what I'm missing? Link to comment Share on other sites More sharing options...
adrian Posted September 3, 2013 Share Posted September 3, 2013 If I am understanding you correctly it is quite simple to do by setting a variable to the current category and then only echo out the category title if the category is different to the category from the last echo'd result. $current_category = ''; foreach($wor as $result) { if($result->cat != $current_category){ echo "<span class=\"catem\">{$result->cat->title}</span>"; } echo **all the name, location, result output** $current_category = $result->cat; } Hope that makes sense and works for you. 1 Link to comment Share on other sites More sharing options...
digitex Posted September 3, 2013 Author Share Posted September 3, 2013 Adrian. Thank you. That'll work just fine. An easy to implement solution but not one I would have thought of on my own. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now