> foreach ($popular[2]['Project'] as $project)
means you're going over the fields inside $popular[2]['Project'].
The first time $project will be "Gallery" (the first field inside
['Project']).
The next time it'll be "This is for testing...." and so on.
Then you're trying to echo $project['name'],
i.e. the ['name'] part of the string "Gallery".
Surely that'll have unintended consequences...
On 30 Oct 2008, at 03:48, Josey wrote:
>
> Thanks for your reply.
> When I run that bit of code I get the following error.
>
> Fatal error: Cannot use string offset as an array in /var/www/
> mortfolio/www.mortfolio.com/html/app/views/users/view.ctp on line 108
>
> Note that if I just print the name by passing $popular[2]['Project']
> ['name'] all is well, the problem comes when I insert it into a loop
> and have it increment through the records.
>
> On Oct 29, 1:02 pm, AD7six <andydawso...@gmail.com> wrote:
>> On Oct 29, 6:47 pm, Josey <josey.mor...@gmail.com> 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 cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment