Displaying View on another View

Hello everyone

So I have been trying this for a while now, I will try my best to explain…should be simple

I have subcategories and Products, Most of my work is generated by yiic. Subcategories has a one-many relation with products. Now I need for when a user visits a specific subcategory to view the products for that subcategory. So for example: subcategory 1 > Product 1, Product 2, etc. I read the yii blg tutorial a few times already (please don’t refer me back). The best I got was to display the amount of products each subcategory has but not the actual products (like the yii blog).

Subcategories.php- I only changed the relations here as so…




public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'products' => array(self::HAS_MANY, 'Products', 'PID'),

			'sCAT' => array(self::BELONGS_TO, 'Categories', 'SCATID'),

			'productsCount' => array(self::STAT, 'Products', 'SCATID'),

		);

	}




Subcategories/view.php





<div id="products">

    <?php if($model->productsCount>=1): ?>

        <h3>

            <?php echo $model->productsCount . ' product(s)'; ?> // this displays No of products (correctly)

			 <?php echo $model->products; ?>// this just prints Array (literally)

        </h3>

 

// the following code does nothing just calls _products which I will show anyway (got it from yii blog)





        <?php $this->renderPartial('_products',array( 

            'subcategories'=>$model,	

            'products'=>$model->products,		

        )); ?>

    <?php endif; ?>

</div>






subcategories/view/_products.php - it does nothing, not even sure if it’s the right way to go about this







	<?php foreach($products as $products): ?>

	<div class="products" id="c<?php echo $products->pid; ?>">


		<?php echo CHtml::link("#{$products->pid}", $products->getUrl($subcategories)); ?>


		


		<div class="content">

		

			<?php echo(CHtml::encode($products->SHORTENAME)); ?>

		</div>


	</div><!-- products -->

	<?php endforeach; ?>




Not sure if you need to see anything else but these are the only things I edited to try and display all products belonging to a subcategory.

Thanks

Hello. There’s for sure something wrong with this line




…

	<?php foreach($products as $products): ?>

…



if $products is an array (I guess so, from the code above), you should have




…

	<?php foreach($products as $product): ?>

…



and then deal with [font="Courier New"]$product[/font] as an array item.

Thanks for the reply

That’s something I noticed but it makes little difference, just to be sure I changed the array to product omitting the s and done




…

	<?php foreach($product as $products): ?>

…



Never made a difference.

Thanks again for the reply, much appreciated.

Let me make myself clearer:

First, you have this in your [font="Courier New"]views/subcategories/view.php[/font]




 <?php $this->renderPartial('_products',array( 

            'subcategories'=>$model,    

            'products'=>$model->products,               

        )); ?>

Then in your [font="Courier New"]views/subcategories/_products.php[/font] (_products.php must be in the same folder than the former view.phph, by the way)


<?php foreach($products as $product): ?>

        <div class="products" id="c<?php echo $product->pid; ?>">

                <?php echo CHtml::link("#{$product->pid}", $product->getUrl($subcategories)); ?>

                <div class="content">

                        <?php echo(CHtml::encode($product->SHORTENAME)); ?>

                </div>

        </div><!-- products -->

        <?php endforeach; ?>

See? You’re are passing $products array to the _products.php view, so you loop into it by [font=“Courier New”]foreach($products as $someOtherVariableName)[/font]

Hi

I understand what you are saying, I changed it up as you suggested. Still brings no results, it runs but no output. What I want is to display my products on my subcategories page/view. Am I even going the correct way about this?

(_products.php is in the same folder as view.php typo error)

Thanks again

Well yes in order to display subcategories products, it’s the correct approach although I can’t tell in detail since I don’t know your db structure or your code.

Can you add the following code directly in your [font="Courier New"]views/subcategories/view.php[/font] and tell me what it returns?


<?php $products = $model->products;

foreach($products as $product) {

    var_dump($product);

} ?>

[


<?php $products = $model->products;

foreach($products as $product) {

    var_dump($product);

} ?>

It runs but prints nothing, would you want me to show you my DB schema?

Thanks

Weird. Please give again the complete view.php (including the loop with var_dump) and you actionView as well

Subcategories/view/view.php





<?php

$this->breadcrumbs=array(

	'Subcategories'=>array('index'),

	$model->SCATID,

);


$this->menu=array(

	array('label'=>'List Subcategories', 'url'=>array('index')),

	array('label'=>'Create Subcategories', 'url'=>array('create')),

	array('label'=>'Update Subcategories', 'url'=>array('update', 'id'=>$model->SCATID)),

	array('label'=>'Delete Subcategories', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->SCATID),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Subcategories', 'url'=>array('admin')),

	);

?>


<h1>View Subcategories <?php echo  subcategories::model()->SCATENAME; ?></h1>


<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'SCATID',

		'SCATENAME',

		'SCATANAME',

		'SCATEDESC',

		'SCATADESC',

		'SCATSPHOTO',

		'SCATBPHOTO',

		'SCATDATE',

		'NOTE',

		'CATID',

		'image',

			

		

		

		

		

		array( 'label'=>'Service Pic',

'type'=>'raw',

'value'=>html_entity_decode(CHtml::image(Yii::app()->baseUrl . '/images/' . $model->SCATSPHOTO,'alt',array('width'=>341,'height'=>232))),

	

),





),		

	

)); ?>




 

		

		

		<div id="products">

    <?php if($model->productsCount>=1): ?>

        <h3>

            <?php echo $model->productsCount . ' product(s)'; ?>

			 <?php echo $model->products; ?>

        </h3>

 

        <?php $this->renderPartial('_products',array(

            'subcategories'=>$model,	

            'products'=>$model->products,		

        )); ?>

    <?php endif; ?>

</div>

		

		<?php $products = $model->products;

foreach($products as $product) {

    var_dump($product);

} ?>

  






actoinview for SubcategoriesController




public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}







actoinview for ProductsController





public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}






Just want to say I appreciate the effort your putting in.

Thanks

So my problem was in my relations




'products' => array(self::HAS_MANY, 'Products', 'PID'),



Should be




'products' => array(self::HAS_MANY, 'Products', 'SCATID'),



Now I get the list of products, only now I have an issue with linking to actual products page.

If I do




 <?php echo CHtml::link("#{$product->PID}", $product->getUrl($subcategories)); ?>




I Get it to link correctly but my approach just doesn’t seem right, very unprofessional.





public function getUrl($products=null)

	{

		if($products===null)

			$products=$this->products;

		return 'index.php?r=products/view&id='.$this->PID;

	}




Very Ugly coding, I tried deafult url manager, of course alot better but linking to my Javascript as so does not work





<script type="text/javascript" href="<?php echo Yii::app()->request->baseUrl; ?>/gallery/js/jquery.js"</script>




^ That will need more research, unless someone is willing to help…

To bennouna, thanks a lot for the help, I guess it was one of those errors I should pay more attention to. Always happens to me, once I was scratching my head for hours because I put a space on < ? PHP like that.

Thanks

Hmmm I missed it :) Glad you solved it.

From what I’ve understood you need something like


echo CController::createUrl('products/view', array('id'=>$product->PID));

Edit:

Personally I use comments in my DB scheme and rely on Gii/Giix to create relations. And I also stick to standard column names. id is always id whatever table it is :) and foreign id are always relationNameId. But it’s a matter of convention, comfort, and personal choice I guess.

PS I don’t understand what the jQuery problem is. Can you explain?

When I use the URL Manager built in config/main.php, I get nice URL’s but for some reason my javascripts don’t work anymore. I have a flash + Javascript slideshow but it only works when I comment out urlManager part.





<script type="text/javascript" href="<?php echo Yii::app()->request->baseUrl; ?>/gallery/js/jquery.js"</script>



^ That doesn’t work if I use the urlManager





'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),



^ I have to comment the above