On Oct 29, 6:47 pm, Josey <[EMAIL PROTECTED]> wrote:
> Here's an interesting problem I've run into today:
>
> I've created a query in my controller that will go out and grab the 3
> most viewed projects each user has.
> Here is my find as well my variable for the view:
>
>                 $popular = $this->Project->find('all',
>                         array(
>                                 'conditions'=>array('Project.portfolio_id' 
> =>$user['Portfolio']
> ['id']),
>                                 'order'=>'Project.visits DESC',
>                                 'fields'=> array('Project.name', 
> 'Project.description',
> 'Project.visits', 'Project.slug', 'Project.cover'),
>                                 'recursive'=>0,
>                                 'limit' => 3
>                         )
>                 );
>
>                 // add name to option
>                 $popular        = array('User'=>'This Users 3 most viewed 
> Projects') +
> $popular;
>                 $this->set('popular', $popular);
>
> As you can see I find all projects that have a Portfolio ID set to the
> same Portfolio ID of the user.
> It will display the records by how often they've been viewed (visits)
> in descending order.
> I chose which fields to display as well as limiting it to just the top
> 3.
>
> This works fine. If I print  the variable is set (popular) to the view
> my array looks perfect.
>
> The problem I'm having is that when I create a loop to loop through
> the three records it only prints the 1st character of each field.
>
> Here's my loop (Just a small one for debugging).
>
>                 <?php
>                         //$i = 0;
>                         foreach ($popular[2]['Project'] as $project) { ?>
>                                 <?php echo $project['name']; ?>
>                                 <br />
>                                 <?php //$i++; ?>
>                 <?php } ?>
>
> The incrementer that I created isn't being used for this example. I'm
> just pulling the data for the 3rd record in the array.
>
> I suppose I should also provide you with the 3rd item in my array so
> here it is.
>
>     [2] => Array
>         (
>             [Project] => Array
>                 (
>                     [name] => Gallery
>                     [description] => This is for testing purposes,
> this project will be removed after I have fine-tuned the image display
> scripts.
>                     [visits] => 16
>                     [slug] => gallery
>                     [cover] => Sexy-Party.jpg
>                     [id] => 41
>                 )
>
>         )
>
> So when I view the page after running the loop this is what is
> displayed.
> G
> T
> 1
> g
> S
> 4

You are treating a string as an array. compare
debug ($popular);
debug($popular[2]['Project']);
foreach ($popular as $project) { <- note the difference.
     debug ($project['Project']['name']); die;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to