silvestro Posted October 28, 2017 Share Posted October 28, 2017 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! 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! Link to comment Share on other sites More sharing options...
Robin S Posted October 28, 2017 Share Posted October 28, 2017 Perhaps some exception or PHP error is occurring. Do you have debug mode on or (even better) Tracy Debugger installed to make sure you see error messages? 1 Link to comment Share on other sites More sharing options...
abdus Posted October 28, 2017 Share Posted October 28, 2017 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(); } ?> 4 Link to comment Share on other sites More sharing options...
silvestro Posted October 28, 2017 Author Share Posted October 28, 2017 @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 Thank you for your other comments regarding cleaner code! Link to comment Share on other sites More sharing options...
abdus Posted October 28, 2017 Share Posted October 28, 2017 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. And yeah, tunnel vision is definitely real. 2 Link to comment Share on other sites More sharing options...
adrian Posted October 28, 2017 Share Posted October 28, 2017 Most editors should these days, eg. Sublime and even the Tracy Console panel SublimeText TracyDebugger Console Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now