anderson Posted February 7, 2019 Share Posted February 7, 2019 Hi, For the purpose of learning, as shown in this photo, I created a repeater field "we", then a template, then a page. But in /templates/testrepeater.php, I has some problem, the "foreach" part does not work as expected. <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member); ?> <img src="<?php echo $member->images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Could anybody please help point out my error? Thanks in advance. Link to comment Share on other sites More sharing options...
jacmaes Posted February 7, 2019 Share Posted February 7, 2019 From a quick look, I see a typo here: <? foreach($page->we as $member); ?> It should be: <? foreach($page->we as $member): ?> Notice ":" instead of ";" 1 Link to comment Share on other sites More sharing options...
adrian Posted February 7, 2019 Share Posted February 7, 2019 You may also need to grab the ->first() image from the images field, or add another nested foreach to loop through all images, eg: <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member): ?> <? foreach($member->images as $image): <img src="<?php echo $images->url; ?>" alt=""> <? endforeach;?> <?php echo $member->wemember; ?><br> <? endforeach;?> Link to comment Share on other sites More sharing options...
bernhard Posted February 7, 2019 Share Posted February 7, 2019 I'd recommend using adrians great tracy debugger module. Then you can easily (and instantly) see the content of your variables. It has lots of helpful information so you'll learn a lot quicker and it has the console, where you can try out things quickly and easily. See what you get by a simple dump - using the short syntax d() 2 Link to comment Share on other sites More sharing options...
anderson Posted February 7, 2019 Author Share Posted February 7, 2019 Thanks so much !!! What can I get from this: Link to comment Share on other sites More sharing options...
anderson Posted February 7, 2019 Author Share Posted February 7, 2019 <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member): ?> <img src="<?php echo $member->Images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Link to comment Share on other sites More sharing options...
Gideon So Posted February 7, 2019 Share Posted February 7, 2019 Hi, 1. PHP notice is not error. Your code still valid. 2. Try change the line if displaying image to the following: <?php echo $member->images->first()->url; ?> Gideon Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 On 2/7/2019 at 3:58 PM, Gideon So said: Hi, 1. PHP notice is not error. Your code still valid. 2. Try change the line if displaying image to the following: <?php echo $member->images->first()->url; ?> Gideon Expand Thanks for your help. first() is recognized as unknow function? To make it clear, with @adrian's code, I removed everything else that's working, only kept: <? foreach($page->we as $member): ?> <? foreach($member->images as $image): <img src="<?php echo $images->url; ?>" alt=""> <? endforeach;?> <? endforeach;?> and the result output is: we as $member): ?> images as $image): that's always the same problem. Really don't know what is going on. Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 another format: <? foreach($page->we as $member): ?> <? foreach($member->images as $image): echo '<img src="' . $images->url . '" alt="">' <? endforeach;?> <? endforeach;?> still similar result. Link to comment Share on other sites More sharing options...
adrian Posted February 8, 2019 Share Posted February 8, 2019 Can you show the code from the entire page? Is it maybe a PHP short tags issue, or a character encoding issue? Do you have PW debug mode on? Does that code work in the Tracy Debugger console panel? Just trying to isolate it. Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 Thanks for fast response. On 2/8/2019 at 1:39 AM, adrian said: code from the entire page? Expand my last post IS the entire page of \site\templates\testrepeater.php I'm new to all this, I didn't create any shortcut of PHP, Wordpress or anything. On 2/8/2019 at 1:39 AM, adrian said: character encoding issue? Expand Just tried ANSI, Unicode, UTF-8. All the same. And yes, PW debug is ON. Not in the Tracy Debugger console panel. Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 (edited) And, I've migrated my local site to a free webhosting, I don't know if it'll be easier this way, if so, please take a look: http://tubezyx.000webhostapp.com/test-repeat/ admin login: admin pswd:147258369 Edited February 8, 2019 by anderson typo Link to comment Share on other sites More sharing options...
bernhard Posted February 8, 2019 Share Posted February 8, 2019 This does not help because tracy and the file editor panel do not work on this host. Also, you should never make any admin login data public, please remove it from your post, even if it is a temporary host. Did you do (and I mean DO, not READ) the hello world tutorial? https://processwire.com/docs/tutorials/hello-worlds/ PS: I got several 502 bad gateway errors on your install, so you might have server issues that cause several problems... Link to comment Share on other sites More sharing options...
zoeck Posted February 8, 2019 Share Posted February 8, 2019 i think it's a problem with php ? Check it with this code: <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <?php foreach($page->we as $member): ?> <img src="<?php echo $member->Images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <?php endforeach;?> </body> </html> you are using <? ... but with newer PHP Versions, this isn't working with the standard configuration, so you should always use <?php instead of <? Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 On 2/8/2019 at 10:53 AM, zoeck said: i think it's a problem with php ? Check it with this code: ...... you are using <? ... but with newer PHP Versions, this isn't working with the standard configuration, so you should always use <?php instead of <? Expand You saved my life !!!!!! This works: <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <?php foreach($page->we as $member): ?> <?php foreach($member->images as $image): ?> <img src="<?php echo $image->url; ?>" alt=""> <?php endforeach;?> <?php echo $member->wemember; ?><br> <?php endforeach;?> </body> </html> New PHP version is more tight? And I appreciate help of everybody here, so nice! Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 On 2/8/2019 at 10:42 AM, bernhard said: This does not help because tracy and the file editor panel do not work on this host. Also, you should never make any admin login data public, please remove it from your post, even if it is a temporary host. Expand OK, I'll remember that, and any Admin here please delete that info for me, guess I cann't do that on my own. And I find it hard to amend my post, only get a short window of one minute or so. And, yes, I DID the hello world tuts. Also I'm watching some youtube tuts by Ryan. Hope I could learn fast. Thanks. 1 Link to comment Share on other sites More sharing options...
bernhard Posted February 8, 2019 Share Posted February 8, 2019 You can also just change your login credentials or take the site offline ? Link to comment Share on other sites More sharing options...
anderson Posted February 8, 2019 Author Share Posted February 8, 2019 On 2/8/2019 at 12:03 PM, bernhard said: You can also just change your login credentials or take the site offline ? Expand Oh, good to know. ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now