sms Posted August 20, 2022 Share Posted August 20, 2022 Hello, I'm trying to get this code working, but I always get the following error message: Oh snizzle… Error: Exception: Unknown Selector operator: '[empty]' -- was your selector value properly escaped? field='1023', value='', selector: '1023' (in wire/core/Selectors.php line 218) #0 wire/core/Selectors.php (259): Selectors->create('1023', '[empty]', '') #1 wire/core/Selectors.php (145): Selectors->extractString('') #2 wire/core/Selectors.php (131): Selectors->setSelectorString(Object(Page)) #3 wire/core/PagesLoader.php (353): Selectors->init(Object(Page)) #4 wire/core/Pages.php (287): PagesLoader->find(Object(Page), Array) What am I doing wrong? Here is my code so far: if ($input->post->submit) { $amounts = $input->post->product; $products = $pages->find("template=product, sort=title"); if ($amounts) { foreach ($amounts as $index => $amount) { $id = $products[$index]; $pages->get($id)->setAndSave('amount', $amount); } } } Thank you for your help. Have a great day! Link to comment Share on other sites More sharing options...
Jan Romero Posted August 20, 2022 Share Posted August 20, 2022 if ($input->post->submit) { //it is a mystery to us what this, and since it comes //from the client, it may also be a mystery to you $amounts = $input->post->product; //you’re looking up pages, so this will be a PageArray //with arbitrary numerical indices $products = $pages->find("template=product, sort=title"); if ($amounts) { //apparently we’re assuming the client sent us an //array with meaningful indices foreach ($amounts as $index => $amount) { //because $products is a PageArray, you’re going to //get NULL or a Page object when you access it like //this. In your case probably NULL unless your client //can somehow divine what pages result from the above //selector AND in what order $id = $products[$index]; //because $id is either NULL or a Page, this selector //cannot work. It will work if you put $id in quotes: //– if $id is NULL it will look for nothing and give you // a NullPage. //– if $id is a Page, PHP will call toString() on it, // resulting in its ID, so you’ll just get the same // Page back. //Both paths seem useless though? $pages->get($id)->setAndSave('amount', $amount); } } } This is my understanding from looking at it… Can you explain what $input->post->product contains? 1 Link to comment Share on other sites More sharing options...
sms Posted August 20, 2022 Author Share Posted August 20, 2022 Thank you, Jan! Putting $id in quotes works ? Here is the form with which I want to change a stock (should become a small stock management for the household): <form method="post"> <?php $products = $pages->find("template=product, sort=title"); foreach ($products as $product) { if ($product->amount == '') $product->amount = 0; $out = "<li>\n"; $out .= " <h3>{$product->id} <input type=\"number\" value=\"{$product->amount}\" name=\"product[]\" min=\"0\" style=\"width: 50px;\"> {$product->title}</h3>\n"; $out .= "</li>\n"; echo $out; } ?> <button type="submit" name="submit" value="update" style="width: auto;">Bestand aktualisieren</button> </form> There is probably a better way, but I am not a programmer. May you explain why $id doesn't work without quotes? Thank you very much for your answer and help. Link to comment Share on other sites More sharing options...
Jan Romero Posted August 20, 2022 Share Posted August 20, 2022 What you’re doing very much hinges on the stability of your products and the order they’re in between rendering the form and processing its submission. If you open the form in your browser and let it sit there for an hour while you or someone else adds, removes or modifies a product, you will update the wrong products when you get back to the form! This is because you’re using the products’ positions in the selector results to identify them. It would be better to use their IDs or their page names because those are much less likely to change. Consider this: <form method="post"> <?php $products = $pages->find("template=product, sort=title"); foreach ($products as $product) { ?> <li> <h3><?=$product->id?> <input type="number" value="<?=$product->amount ?: 0?>" name="product[<?=$product->id?>]" min="0" style="width: 50px;"> <?=$product->title?></h3> </li> <?php } ?> <button type="submit" name="submit" value="update" style="width: auto;">Bestand aktualisieren</button> </form> if ($input->post->submit) { $amounts = $input->post->product; $products = $pages->find("template=product, sort=title"); if ($amounts) { foreach ($amounts as $id => $amount) { $products->getPageByID((int)$id)?->setAndSave('amount', $amount); } } } 1 hour ago, sms said: May you explain why $id doesn't work without quotes? Well, one answer is “because Ryan didn’t program it that way”. You were giving $pages->get() a Page that you already had. It certainly could work, because all the necessary information is there, but the get() method wasn’t programmed for that situation, because if you have the Page object already, why would you need to get it again? You can still do so by converting the Page object to a string (which is always its ID) or just getting its ID explicitly. Because your Page object was named $id, that would have been $pages->get($id->id). 1 hour ago, sms said: type=\"number\" Here is an unrelated tip: you can just use single quotes instead of escaping the double quotes, because mercifully both are legal in HTML. You can even mix them in the same tag: <input type="text" value='call me ishmael'/> 1 Link to comment Share on other sites More sharing options...
sms Posted August 20, 2022 Author Share Posted August 20, 2022 Wow, thanks a lot, this looks much better than my attempt. And it works fine ? Actually, I was also planning to work with IDs, but I just didn't get to the point how to combine the two arrays in one loop. Thanks for the additional explanations. I didn't know getPageByID is available since 3.0.162. Thanks again, you saved my evening ? 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