How to display data from two model in one view the given way in cakephp 1.3?
I'd like to display the data from the array in the开发者_JAVA技巧 order as shown in the picture:
I will use nested foreach.
foreach($yourvariable as $data){
echo $data['Category']['title'];
foreach ($data['Product'] as $myproduct){
echo $product['whateverfield1'];
echo $product['whateverfield2'];
}
}
I would make a CakePHP Helper. Then you can make a method inside that Helper that will generate the list markup you need. Here's a quick-n-dirty...
class YourHelper extends AppHelper {
public function your_method(array $modelData) {
foreach ($modelData as $info) {
$markup = '<div>';
$markup .= '<h3>' . $info['Category']['title'] . '</h3>';
$markup .= '<ul>';
$allProducts = $info['Product'];
if (!empty($allProducts) {
foreach ($allProducts as $productInfo) {
$markup .= '<li>' . $productInfo['desc'] . '</li>';
}
} else {
$markup .= '<li>No products in this category!</li>';
}
$markup .= '</ul></div>';
}
return $markup;
}
Ensure to include the helper in your controllers or AppController
then you can use it in your views just like any other helper.
Obviously I haven't tested this out but with this method you should be able to have an arbitrary number of children
and then an arbitrary number of Products
within each of those.
Let me know if this works for you.
精彩评论