Jump to content
silvestro

Foreach breaks with save()

Recommended Posts

Hello all,

I thought quite some time about this, and maybe it might be my basic (mis-)understanding of php, but I still can't get over the following scenario:

I'm trying to create multiple pages via code in a foreach loop, but after the save() function the loop seems to stop.

<?php
	foreach ($events as $event) {
		$evName = str_replace(' ', '-', $event['name']);
		$evName = str_replace('#', '', $evName);
		$evName = preg_replace( "/\r|\n/", "", $evName );
		$evName = strtolower($evName);
		
		$p = $pages->get("/events/$evName/");
		if (!$p->id) {
			$p = new Page();
			$p->template = 'basic-page';
			$p->parent = '/events/';
			$p->name = '$evName';
		}
		$p->of(false);
		$p->title = $event['name'];

		echo ('Done: ' . $evName); // I added this for debugging. It echoes only once!
		$p->save(); // If this is commented out, 'Done' + Event Name echoes for each Event!
	}
?>

Running this code, the first page for the $events-array is created, so that part seems to be working at least! :D

But, somehow the foreach loop seems to stop when the page is saved and the rest of the pages isn't created. Am I missing something crucial here?

 

Thank you for any suggestion!

Share this post


Link to post
Share on other sites
29 minutes ago, silvestro said:

$p->name = '$evName';

I may be wrong but the problem is most likely due to this line. With single quotes, $evName is interpreted literally, so you get errors trying to create the same page again, and don't notice it because you probably have the debug mode off as @Robin S pointed out.

Also,

<?php namespace ProcessWire;

foreach ($events as $event) {
    // you don't have to manually clean up strings to create page names, just use $sanitizer
    $evName = $sanitizer->pageName($event['name']);

    $p = $pages->get("/events/$evName/");
    if (!$p->id) {
        $p = new Page();
        $p->template = 'basic-page';
        $p->parent = '/events/';
        // $p->name = '$evName'; // single quotes -> interpreted as literal string
        $p->name = $evName;
    }
    $p->of(false);
    $p->title = $event['name'];
    $p->save();
}
?>

 

  • Like 4

Share this post


Link to post
Share on other sites

@abdus this was it - thank you so much!!!

The pages were being created with the same url "evname" - therefore at the second time, at the check if it exists it skipped the rest.

It's really entertaining how your mind is fixated on a cause for the problem that you don't think of the obvious :D

 

Thank you for your other comments regarding cleaner code! :-)

Share this post


Link to post
Share on other sites

I'm not sure if your editor highlights variables inside strings, but PhpStorm, for instance, makes it really easy to spot the difference. Check your editor/highlighting settings, there might be an option to enable it.

image.png.4b3709f95649d4fe4e2d32533ad2b627.png

And yeah, tunnel vision is definitely real.

 

  • Like 2

Share this post


Link to post
Share on other sites

Most editors should these days, eg. Sublime and even the Tracy Console panel :)

SublimeText

59f5090be5c71_ScreenShot2017-10-28at3_47_01PM.png.25f09b6483ea15c9492a94740b18766f.png

 

TracyDebugger Console

59f5090ca249e_ScreenShot2017-10-28at3_46_41PM.png.02e4f12107b31c60bd9574e26992c13b.png

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.

×
×
  • Create New...