Forum

Thread tagged as: Runway

perch runway collection array echo error

Pulling a collection item with ajax based on url, works fine using an html template and perch_content tags but want to spit out the variables and echo out so when skip-template used array seems to have structure and content and can be seen in a print_r display but can't get to content using this kind of call -

$thetitle = $articleContent[0]['Title'];

Getting error - Notice: Undefined offset: 1 in ......

print_r returns array data -

Array ( [0] => Array ( [_id] => 5 [Title] => Physiotherapy [_title] => Physiotherapy [subhead] => Diagnosing, treating and rehabilitating injuries. [slug] => physiotherapy [page-image] => /site-admin/resources/physiotherapy-3.jpg [alt] => Patient receiving treatment [TextLeft] =>........

and so on....


$articleContent = perch_collection($parentTitle, [ 'filter' => 'slug', 'match' => 'eq', 'value' => perch_get('article'), 'skip-template' => true, 'return-html' => false, ]); print_r ($articleContent); if (!empty($articleContent)) { //set content vars $thetitle = $articleContent[0]['Title']; //test vars echo $thetitle; }

I have similar code working fine with perch_content_custom, getting array and echoing out the variables but can't get it to work with perch_collection

Any advice appreciated.

Thanks

Keith Winter

Keith Winter 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Getting error - Notice: Undefined offset: 1 in ......

In...? You left out the most important bit! :)

Ah sorry Drew

Array ( ) 
( ! ) Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/sqhc/site-admin/custom/get-article.php on line 27

So to clarify my print_r displays the array fine -


$articleContent = perch_collection($parentTitle, [ //'template' => 'article.html', 'filter' => 'slug', 'match' => 'eq', 'value' => perch_get('article'), 'skip-template' => true, 'return-html' => false, ]); print_r ($articleContent); Array ( [0] => Array ( [_id] => 5 [Title] => Physiotherapy [_title] => Physiotherapy [subhead] => Diagnosing, treating and rehabilitating injuries. [slug] => physiotherapy [page-image] => /site-admin/resources/physiotherapy-3.jpg [alt] => some text [TextLeft] => Some text [TextRight] => Some text [Highlight] => Fast, cost-effective, convenient physiotherapy services, focused on the needs of your claimant. [stat-header-left] => Stat 1 [stat-text-left] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. [stat-header-right] => Stat 2 [stat-text-right] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. [footer-text] => For more information about how our services can positively impact on your claims experience, please get in touch. Or visit our specialist site: [footer-link] => https://www.site/ [footer-btn] => Visit Website [_sortvalue] => 1000 ) )

As soon as I add

//set content vars
$thetitle = $articleContent[0]['Title'];

//test vars
echo $thetitle;

I get errors like the one above.

Hope that helps and thanks.

keith

Drew I now have it working, odd as I'm not sure why it is now when it wasn't earlier.

if (!empty($articleContent)) {

//set content vars
$thetitle = $articleContent[0]['Title'];
$thesubhead = $articleContent[0]['subhead'];
$theimg= $articleContent[0]['page-image'];
$thetextleft = $articleContent[0]['TextLeft'];
$thetextright = $articleContent[0]['TextRight'];


//test vars
echo $thetitle;
echo $thesubhead;
echo $theimg;
echo $thetextleft;
echo $thetextright;

}

Thanks again anyway. K

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great