Jump to content

php basics? about Repeater print out


anderson
 Share

Recommended Posts

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.

repeatertest.jpg

Link to comment
Share on other sites

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

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()

Zfv1HnA.png 

  • Like 2
Link to comment
Share on other sites

9 hours ago, 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

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

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

Thanks for fast response.

17 minutes ago, adrian said:

code from the entire page? 

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.

 

21 minutes ago, adrian said:

character encoding issue?

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

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

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

20 minutes ago, 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 <?

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

37 minutes ago, 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.

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...