3

i want to display data from database and also i have created function in model file which is showing data from database but all values are shown in the array format.

problem is that when i print echo $values['title']; in foreach loop it is showing only first letter from title array??

model code

 function reviewcitypage()  
 {

 $cacheKey = 'city_page';
 GigaCache::set(array('duration'=>"+1 minutes",'path'=>CACHE));
 $cachedCategoryData = GigaCache::read($cacheKey);

    if($cachedCategoryData && !cr('DynamicPage.field'))
    {   
        $recentactivity = $cachedCategoryData;
    }else
    {
        $recentactivity= $this->find("list",array("conditions"=>array("status"=>1),'fields'=>array('title','body','rating'),'recursive'=>-1,'limit'=>10));  
        //dont't set cache if dynamic field
        if(!cr('DynamicPage.field'))
        {
          GigaCache::set(array('duration'=>"+1 minutes",'path'=>CACHE));
          GigaCache::write($cacheKey,$recentactivity);
        }
    }


    return $recentactivity;

}

view file

$ReviewObj = cri('Review');
      $recentactivity = $ReviewObj->reviewcitypage();

      foreach ($recentactivity as $name => $value){ 

          foreach($value as $values)
            {             
                echo $values['title'];
            }
      }

**problem is solved now thanks for support **

i have changed the code in model file and it is woking now

$recentactivity= $this-
>find("all",array("conditions"=>array("status"=>1),'recursive'=>-1,
'limit'=>10));  

1 Answers1

3

Your find() query is preparing the data as a 'list'. in cake lists are always key => value pair arrays. so in your view when you use the second foreach loop you are saying foreach character in a string...do.....

in your example $value can only be a string. foreaching it can only make $values a single char.

Let me know if you still unsure what i mean. not the best at explaining what i mean

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list

Because you are after 3 fields I suggest using either first or all in place of list as the first argument in the find() method.

Jason Joslin
  • 1,154
  • 8
  • 23
  • jason you have any example how can i show data in view file from model file ?? –  Apr 21 '16 at 10:14
  • 1
    well because you are after 3 fields I suggest using either `'first'` or `all` in place of `list` as the first argument in the find() method. What is your rationale around choosing `list` as your return type? – Jason Joslin Apr 21 '16 at 10:18
  • well im facing problem is that i want to display data in view file ?? do you have skype? –  Apr 21 '16 at 10:21
  • Please tell us the resolution to your problem – Jason Joslin Apr 21 '16 at 10:27
  • i have changed the code in model file and it is working now `list to all ` –  Apr 21 '16 at 10:34
  • Because I was technically right about `list` being incorrect can you mark my answer as correct? – Jason Joslin Apr 21 '16 at 10:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109795/discussion-between-farhan-dharsi-and-jason-joslin). –  Apr 21 '16 at 10:42