Jump to content

Recommended Posts

Posted

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!

Posted
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
Posted

@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! :-)

Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...