Yii Framework Forum: Displaying View on another View - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Displaying View on another View Rate Topic: -----

#1 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 08 April 2012 - 01:27 PM

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
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,111
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 April 2012 - 02:23 PM

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 $product as an array item.

This post has been edited by bennouna: 08 April 2012 - 02:25 PM

0

#3 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 08 April 2012 - 02:45 PM

View Postbennouna, on 08 April 2012 - 02:23 PM, said:

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 $product 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.
0

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,111
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 April 2012 - 03:37 PM

Let me make myself clearer:

First, you have this in your views/subcategories/view.php
 <?php $this->renderPartial('_products',array( 
            'subcategories'=>$model,    
            'products'=>$model->products,               
        )); ?>

Then in your views/subcategories/_products.php (_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 foreach($products as $someOtherVariableName)
0

#5 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 08 April 2012 - 03:54 PM

View Postbennouna, on 08 April 2012 - 03:37 PM, said:

Let me make myself clearer:

First, you have this in your views/subcategories/view.php
 <?php $this->renderPartial('_products',array( 
            'subcategories'=>$model,    
            'products'=>$model->products,               
        )); ?>

Then in your views/subcategories/_products.php (_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 foreach($products as $someOtherVariableName)


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
0

#6 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,111
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 April 2012 - 04:38 PM

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 views/subcategories/view.php and tell me what it returns?
<?php $products = $model->products;
foreach($products as $product) {
    var_dump($product);
} ?>

0

#7 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 09 April 2012 - 04:42 AM

[
<?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
0

#8 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,111
  • Joined: 05-January 12
  • Location:Morocco

Posted 09 April 2012 - 04:53 AM

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

This post has been edited by bennouna: 09 April 2012 - 04:54 AM

0

#9 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 09 April 2012 - 05:15 AM

View Postbennouna, on 09 April 2012 - 04:53 AM, said:

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
0

#10 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 09 April 2012 - 09:50 AM

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
0

#11 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,111
  • Joined: 05-January 12
  • Location:Morocco

Posted 09 April 2012 - 10:18 AM

Quote

So my problem was in my relations

Hmmm I missed it :) Glad you solved it.

Quote

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

From what I've understood you need something like
echo CController::createUrl('products/view', array('id'=>$product->PID));

Edit:

Quote

I guess it was one of those errors I should pay more attention to

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?

This post has been edited by bennouna: 09 April 2012 - 10:23 AM

0

#12 User is offline   ItsYii 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 24-January 12

Posted 09 April 2012 - 04:47 PM

View Postbennouna, on 09 April 2012 - 10:18 AM, said:



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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users