Jump to content
anderson

php basics? about Repeater print out

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

Share this post


Link to post
Share on other sites

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 ";"

  • Like 1

Share this post


Link to post
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;?>

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
<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>

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.  

Share this post


Link to post
Share on other sites

another format:

    <? foreach($page->we as $member): ?>
        <? foreach($member->images as $image):
            echo '<img src="' . $images->url . '"  alt="">'    
        <? endforeach;?>    
    <? endforeach;?>

still similar result.

Share this post


Link to post
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.

 

Share this post


Link to post
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.

Share this post


Link to post
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...

Share this post


Link to post
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 <?

Share this post


Link to post
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! 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

You can also just change your login credentials or take the site offline 😉

Share this post


Link to post
Share on other sites
Just now, bernhard said:

You can also just change your login credentials or take the site offline 😉

Oh, good to know. 👍

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By CareerTeam GmbH
      Hi there,
      We are an executive search agency based in Germany looking for a freelancer (2-5 days per week) supporting us with the development and design of our websites. The position will be located in Hamburg, Germany and it would be great if you are on short call. German language knowledge is mandatory. 
      You can reach me via email jobs@careerteam.de.
      Thank you!
      Regards
      Annemie
    • By David Karich
      ProcessWire InputfieldRepeaterMatrixDuplicate
      Thanks to the great ProModule "RepeaterMatrix" I have the possibility to create complex repeater items. With it I have created a quite powerful page builder. Many different content modules, with many more possible design options. The RepeaterMatrix module supports the cloning of items, but only within the same page. Now I often have the case that very design-intensive pages and items are created. If you want to use a content module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      This module extends the commercial ProModule "RepeaterMatrix" by the function to duplicate repeater items from one page to another page. The condition is that the target field is the same matrix field from which the item is duplicated. This module is currently understood as proof of concept. There are a few limitations that need to be considered. The intention of the module is that this functionality is integrated into the core of RepeaterMatrix and does not require an extra module.
      Check out the screencast
      What the module can do
      Duplicate a repeater item from one page to another No matter how complex the item is Full support for file and image fields Multilingual support Support of Min and Max settings Live synchronization of clipboard between multiple browser tabs. Copy an item and simply switch the browser tab to the target page and you will immediately see the past button Support of multiple RepeaterMatrix fields on one page Configurable which roles and fields are excluded Duplicated items are automatically pasted to the end of the target field and set to hidden status so that changes are not directly published Automatic clipboard update when other items are picked Automatically removes old clipboard data if it is not pasted within 6 hours Delete clipboard itself by clicking the selected item again Benefit: unbelievably fast workflow and content replication What the module can't do
      Before an item can be duplicated in its current version, the source page must be saved. This means that if you make changes to an item and copy this, the old saved state will be duplicated Dynamic loading is currently not possible. Means no AJAX. When pasting, the target page is saved completely No support for nested repeater items. Currently only first level items can be duplicated. Means a repeater field in a repeater field cannot be duplicated. Workaround: simply duplicate the parent item Dynamic reloading and adding of repeater items cannot be registered. Several interfaces and events from the core are missing. The initialization occurs only once after the page load event Changelog
      1.0.4
      Bug fix: Various bug fixes and improvements in live synchronization Bug fix: Items are no longer inserted when the normal save button is clicked. Only when the past button is explicitly clicked Feature: Support of multiple repeater fields in one page Feature: Support of repeater Min/Max settings Feature: Configurable roles and fields Enhancement: Improved clipboard management Enhancement: Documentation improvement Enhancement: Corrected few typos #1 1.0.3
      Feature: Live synchronization Enhancement: Load the module only in the backend Enhancement: Documentation improvement 1.0.2
      Bug fix: Various bug fixes and improvements in JS functions Enhancement: Documentation improvement Enhancement: Corrected few typos 1.0.1
      Bug fix: Various bug fixes and improvements in the duplication process 1.0.0
      Initial release Support this module
      If this module is useful for you, I am very thankful for your small donation: Donate 5,- Euro (via PayPal – or an amount of your choice. Thank you!)
      Download this module
      > Github: https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate
      > PW module directory: https://modules.processwire.com/modules/inputfield-repeater-matrix-duplicate/
    • By gerritvanaaken
      Wouldn’t it be cool to have whole repeater items (or even repeater matrix items) language-specific. Some of my clients do have pages that are 90% identical in German and English, but there are some sections that should not appear in one or another language. I attached a concept screenshot.
      Would something like this be possible with some simple backend hooks and "hidden checkbox fields"? Or is it way more complicated?
       

    • By louisstephens
      I was really unsure of how to actually title this post, so I do apologize (if someone has a better idea, I will gladly edit it). I am using the profields: pagetable field to allow people to create their own "content" (copy, image, button, etc etc) and rearrange it. I also included a field called "column_size" using the RangeSlider set to (1-12).
      I guess I'll clarify a bit more on this. I am using flexbox where the "row" is <section></section> and the columns are <div class="column"></div> have given the "columns"  flex: 1 1 0; so no matter how many columns you have, the columns will automatically adjust for new content. Where my confusion is coming in: If a user has set up 3 copy items (with 12, 5, 7 respectfully for the column_size), how do I actually output this in my template? I was going to use a switch statement to handle the various items which I thought made it quite easy, but with closing sections and columns I have confused myself as I assume I need an if statement to check if the column size is > 12, or = 12 to determine the actual closing/opening of sections. I apologize if I have not made this very clear. I am a bit unsure how to word this let alone to go about this. 
      Im very appreciative of for any insight into this.
       
       
×
×
  • Create New...