Jump to content

Query page based on a Checkbox Page Reference in a Repeater


Pip
 Share

Recommended Posts

Hi Everyone

Need some help on page reference (PR) field type in repeaters. I've checked other threads and it seems straightforward by referring to the object name and down to "title", "name" etc.

I tried it on my side and it seems "title" and "name" and "id" of the PR is blank. But if I refer to the object itself, the page ID is given no problem. Problem is when I try to query the page (ingredient template) using $pages->find, it returns none as well.  I did strval none (yes was that annoyed).

Some background:
 

Quote

 

batch (template)
   - menu (PR, checkbox for pizza template)
   - start date
   - end date

pizza (template)
    - topping (repeater)
         - ingredient (PR)
         - quantity (textfield) --- how many of the ingredient is being used in the pizza itself

ingredient (template)
     - name
     - quantity (textfield)  --- total number in stock

 

What I am trying to do:
I am trying to create an online order form for a pizza place where menu rotates and comes in batches. In the order form (page), it will check if today is in any batch. If yes, it will display all items in the menu BUT provided they have the available ingredients (ingredient->quantity > 0). 

My problem:
I can't query the page which is weird and I need to check if there's still some stocks. ? See codes below.
 

		<?php
			// Check if today is in a batch
			foreach ($pages->find("template=batch, batchdatestart<=today, batchdateend>=today") as $batch):				
		?>
			<br>
			Title: <?php echo $batch->name; ?><br>
			<?php 
				// Print pizzas available today
				foreach ($batch->menu as $menuitem): 
					echo $menuitem->title . "<br>&nbsp; &nbsp; &nbsp; Ingredient: <br>";
					// Print ingredients of this pizza
					foreach ($menuitem->topping as $pizza):
						// The problematic line belo. 
						// If $pizza->ingredient->title/name/id, none appears. 
						// If $pizza->ingredient, page ID appears BUT if you use in the $pages->find, it returns none.
						echo  $pizza->ingredient . "<br>";
					endforeach; 	
				endforeach; 
			?>
		<?php endforeach; ?>


 

 

Link to comment
Share on other sites

One thing I'd try is using Pages > Find to make the same selection, and then simplified version, and see if you get any results. If you have debug set to true in config.php, you can see the selector being used towards the bottom of the page.

Another thing would be to try displaying the results of the find using the selector – and variations of it – on a page using print_r(). Or if you have Tracy Debugger installed (highly recommended!), return the result using bd().

BTW, it might be more efficient and a little easier to debug if you find and loop through the pages something like this:

$batches = $pages->find("template=batch, batchdatestart<=today, batchdateend>=today");
foreach ($batches as $batch) ...

 

  • Like 1
Link to comment
Share on other sites

If you are getting ID of ingredient possible workaround could be this:

$ingredient = $pages->get($pizza->ingredient); 

Then use $ingredient as page, eg. $ingredient->title... 

Maybe check settings of your field in Pw if nothing is out of ordinary. 

  • Like 1
Link to comment
Share on other sites

11 hours ago, BillH said:

One thing I'd try is using Pages > Find to make the same selection, and then simplified version, and see if you get any results. If you have debug set to true in config.php, you can see the selector being used towards the bottom of the page.

Another thing would be to try displaying the results of the find using the selector – and variations of it – on a page using print_r(). Or if you have Tracy Debugger installed (highly recommended!), return the result using bd().

BTW, it might be more efficient and a little easier to debug if you find and loop through the pages something like this:


$batches = $pages->find("template=batch, batchdatestart<=today, batchdateend>=today");
foreach ($batches as $batch) ...

 

Debug is now on.

Thanks on the efficiency. Implemented it.


As for print_r, I got these.
 

Quote

 

ProcessWire\PageArray Object ( [count] => 1 [items] => Array ( [Page:0] => Array ( [id] => 1018 [name] => prosciutto [parent] => /ingredients/ [template] => ingredient [title] => Prosciutto ) ) [selectors] => )  


ProcessWire\PageArray Object ( [count] => 1 [items] => Array ( [Page:0] => Array ( [id] => 1040 [name] => mushroom [parent] => /ingredients/ [template] => ingredient [title] => Mushroom ) ) [selectors] => )   

 

 

Link to comment
Share on other sites

55 minutes ago, cehlo said:

Ok, so according to your previous post variable $pizza->ingredient is page array so you must use foreach also for ingredients. 

Let me face palm for a sec. LOL It works. So I needed to foreach another time so weird. Thank you so much!

Link to comment
Share on other sites

  • 2 weeks later...
On 6/2/2021 at 11:13 PM, fredrickk655 said:

Hello,

 i am new to this prompt ingredient as page for example. $ ingredient-> name ...

is this the correct structure?

It depends on how you use it. $ingredient is just a name.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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