views:

208

answers:

3

The application I'm building is a tennis draw, which has a list of matches organized by round. I've also given the matches a position number as a way to order them and manage matches that are at the top of the draw. In a 32 player draw there are 16 matches in the first round, with match positions ordered from 1 - 16.

The draw is laid out using html and isn't just a series of table rows, it has some complexity to it. So in order to display match players/scores in the view I need to place data in places that are out of sequence from a typical display using a series of rows.

How can I display 16 matches of data without using a foreach loop? Is it best for each variable in the controller be for a single record?

I had thought that in the controller I could have 16 variables, but I'd prefer to learn better dry approaches to programming as I learn php and cakephp.

Much appreciated.

A: 

Just display the array elements.

<?php echo $matches['Match'][0]['Player1']?>
<?php echo $matches['Match'][12]['Score']?>

If you want to have a good look at the data array, to figure out which bits are where, you can use pr().

<?php pr($matches)?>

You can just echo out a specific part of an array, they are no exclusivly tied to foreach loops. This is the approach that I would take. Although you need to be aware that obviously as your data changes between rounds you'll either need to update or create a new view for each round.

DavidYell
A: 

If you are really unable to iterate through the data in your view (consider also nested foreach loops), your are passing the wrong data to the view!

Refactor your controller to pass data in an iterable form. Containables are really awesome in CakePHP.

sibidiba
+1  A: 

Don't give up on foreach just yet.

I each row of data is "fairly complex", perhaps it's the best if you move it to an element.

If you need to display data outside of the typical table, maybe it would be better to extract that data using the awesome Set class, and then doing your calculations and whatnot.

Although, as sibidiba has already mentioned, it would be the best if you did all that in your model, so your views stay clean and clear. All the data you want to display outsite your standard table should already be ready for display, prepared in your model and passed on to your views by your controller. As mentioned, Set class is a powerful tool, see if you can use it.

dr Hannibal Lecter
If it's that complex you may also want to consider making it a helper, not just an element.I agree with the Set class stuff, as well as the Containables behavior. Whip your data into shape before you try and use it!
Travis Leleu