Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp foreach loop only display the first letter from title

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 Answers

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.

like image 137
Jason Joslin Avatar answered Feb 18 '26 19:02

Jason Joslin