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
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
    • By Robin S
      Repeater Images
      Adds options to modify Repeater fields to make them convenient for "page-per-image" usage. Using a page-per-image approach allows for additional fields to be associated with each image, to record things such as photographer, date, license, links, etc.
      When Repeater Images is enabled for a Repeater field the module changes the appearance of the Repeater inputfield to be similar (but not identical) to an Images field. The collapsed view shows a thumbnail for each Repeater item, and items can be expanded for field editing.
      Screencast

      Installation
      Install the Repeater Images module.
      Setup
      Create an image field to use in the Repeater field. Recommended settings for the image field are "Maximum files allowed" set to 1 and "Formatted value" set to "Single item (null if empty)". Create a Repeater field. Add the image field to the Repeater. If you want additional fields in the Repeater create and add these also. Repeater Images configuration
      Tick the "Activate Repeater Images for this Repeater field" checkbox. In the "Image field within Repeater" dropdown select the single image field. You must save the Repeater field settings to see any newly added Image fields in the dropdown. Adjust the image thumbnail height if you want (unlike the core Images field there is no slider to change thumbnail height within Page Edit). Note: the depth option for Repeater fields is not compatible with the Repeater Images module.
      Image uploads feature
      There is a checkbox to activate image uploads. This feature allows users to quickly and easily add images to the Repeater Images field by uploading them to an adjacent "upload" field.
      To use this feature you must add the image field selected in the Repeater Images config to the template of the page containing the Repeater Images field - immediately above or below the Repeater Images field would be a good position.
      It's recommended to set the label for this field in template context to "Upload images" or similar, and set the visibility of the field to "Closed" so that it takes up less room when it's not being used. Note that when you drag images to a closed Images field it will automatically open. You don't need to worry about the "Maximum files allowed" setting because the Repeater Images module overrides this for the upload field.
      New Repeater items will be created from the images uploaded to the upload field when the page is saved. The user can add descriptions and tags to the images while they are still in the upload field and these will be retained in the Repeater items. Images are automatically deleted from the upload field when the page is saved.
      Tips
      The "Use accordion mode?" option in the Repeater field settings is useful for keeping the inputfield compact, with only one image item open for editing at a time. The "Repeater item labels" setting determines what is shown in the thumbnail overlay on hover. Example for an image field named "image": {image.basename} ({image.width}x{image.height})  
      https://github.com/Toutouwai/RepeaterImages
      https://modules.processwire.com/modules/repeater-images/
    • By Orkun
      Hi Guys
      I have a problem with a repeater field. Some weeks ago I had exported a repeater field from a dev installation and then imported it to a live installation. All just had worked fine as I remember (the field was created etc...). But today I got an email from the developer of our customer that he has a problem with two repeater fields. When he had changed the fields of another repeater it has also changed it in the other repeater field (which I had imported some weeks ago). I investigated this further and found out that exporting/importing of repeater fields doesn't work for my version of processwire (we are using 2.7.3) (see https://github.com/processwire/processwire-issues/issues/416). 
      So I decided to remove the repeater field and create it again manually on the live enviroment. I could remove the repeater field from the template but I can't delete the repeater field because it is referring to a false repeater template. How can I fix this issue so that I can delete the repeater field?
       
      The "event_dates" repeater field is referring to the "repeater_speciality_adresses" template which is wrong.

      When I try to delete the field I get this error:

       
      Do I need to manually remove the field from the db? What do I exactly need to do?
      Kind Regards
      Orkun
×
×
  • Create New...