Welcome Guest, Not a member yet? Register   Sign In
Displaying Results -- Best Practice Question
#1

[eluser]maadmac[/eluser]
Here's one for the CI veterans out there: how do you pass results to a view? (I don't mean technically, I mean strategically/conceptually.)

In my example, I have a shopping cart-like app that involves selecting products, each with a lot of options. I'm working on the 'review order' page and pass the db results as an array to the view. What would be lovely is if I could just loop through the array, but the keys don't make good descriptors, so I end up doing it by hand. But sometimes an option doesn't exist or wasn't specified and I don't want a lot of display logic in the view.

For example, using a loop I get the array keys:

Code:
doc_ref_num: 123123

Instead of something like:

Code:
Document/Ref. Number: 123123

Does that make sense? Should I just bite the bullet and do the product-specific data formatting in the controller so that all I have to do in the view is just loop and output or is there an easier/more elegant way?

Thanks in advance!
#2

[eluser]nate_02631[/eluser]
I'm not *quite* clear on what you're getting at, but I don't see any reason why you couldn't just loop through the options in the view. Plus, I think it's a good idea to keep display logic in the view, no matter how much of it there is (though I don't think you'd need a lot).

I might set up an array to match the unfriendly array keys of the options with friendlier names, then use that in your view:
Code:
$niceNames = array ('doc_ref_num' => 'Document/Ref. Number'... );
Then, in the view, you can loop through the array as normal and use the niceNames... and of course, simple logic to display the option if it even exists..
Code:
<? while (list($key, $value) = each ($option)): ?>
  &lt;?= $value ? '<p>'.$niceNames($key).': '.$value.'</p>' : '' ?&gt;
&lt;? endwhile ?&gt;
Which would display the option only if it were set... Of course, if you kept the key's quasi-friendly, like document_reference_number, you could use a simple helper to convert them to friendly names and ditch the $niceNames array altogether...

P.S. Nice looking site... was thinking of moving to that area myself. Nice to know there's a demand for quality web services down there Wink
#3

[eluser]maadmac[/eluser]
[quote author="nate_02631" date="1195368036"]I might set up an array to match the unfriendly array keys of the options with friendlier names, then use that in your view:[/quote]

Of course, it's so simple! That's exactly what I'll do... I think I'd been staring at it too long! Thanks for the tip.

Quote:P.S. Nice looking site... was thinking of moving to that area myself. Nice to know there's a demand for quality web services down there Wink

You're too kind! We just launched it, actually, after much effort and wringing of hands, using ExpEngine to power the backend...

The triangle's a beautiful place to be and a hotbed of tech activity... lotta skilled designers/developers here. I think it has the potential to be a startup hub on par with Boston and the Bay area -- just without the waterfront views.
#4

[eluser]TSOL[/eluser]
How about this...

Model:
Option class
Product class with an array of Option objects as a property.

Controller:
Populate an array of Product objects (and it's related Options) from the db results, then pass your array of Product objects to the View.

View:
Iterate through your array of Product Objects to access the Product info; each Product will have its associated Options.

[EDIT] Forgot to mention that this approach makes it easier to read and manipulate what your view is displaying.

Example:
foreach($Products as $Product){
echo "Product Name is ". $Product->name." and has the following options:";
foreach($Product->Options as $Option){
echo $Option->name.'<br>';
}
}

Cheers.
#5

[eluser]maadmac[/eluser]
[quote author="TSOL" date="1195434138"]How about this...

Model:
Option class
Product class with an array of Option objects as a property.[/quote]

That's not a bad idea at all, and would work really well for a proper shopping cart. In my case we're only talking 6 'products' whose options are all virtually identical, so it didn't seem worth it to me to generate a different class per product/option. In your case, the product passed to the view would already have been formatted in the model, so I could simplify the logic in the view...

But my question in this case is, having pulled undistinguished data and passed it to the view, what would be the simplest way to format the presentation... Nate's option was the simplest and works great in my example.




Theme © iAndrew 2016 - Forum software by © MyBB