Search the Community
Showing results for tags 'wirearray'.
-
I have made a photo gallery using PW and I'm quite pleased with myself as it works pretty much as I want and I consider myself a basic programmer. Basically my templates use Pageimages field to hold uploaded images. Now I want to make a template to share some of my images online. I intend to cherry pick a bunch of my images and store the selection in a WireArray. Then I want to be able to send a link to someone which will use this to display the selection of images. The bit that I don't know is how to save this WireArray of selected images. I don't want to use Pageimages because it will store again the image on disc, what I want is a reference to the already saved images so that I can use that in my template field, but I'm not sure what field to use. Is it one of the Page Reference fields in the drop list for fields? I've tried looking this up in the forums but most of it seems to be to map things to front end (AsmSelect etc. which I don't really understand!) whereas I just want to save my list of selected image references to the database. I could probably use the cache class to save the WireArray as it says it can save arrays... but any pointers in the right direction would help. Many thanks!
-
I would like to create pages from a json feed. So i decode my json and create them via API. $jsonData = json_decode($jsonFromOtherWebsite); foreach($jsonData as $jsonDataItem) { $pageTitle = $jsonDataItem->name; $p = new Page(); $p->template = 'import_page'; $p->parent = $pages->get(xxxx); $p->title = $pageTitle; $p->save(); } Let's say the source (json) changes and i have to do another import. Then I want to compare the new json with the existing pages to see if there are new ones and if there some aren't there anymore. Is there a way to compare the new JsonData with my existing pw-pages with the API. Something like foreach($jsonData as $jsonDataItem) { // check if a page with this title exist if($pages->find("template=import_page, title=$jsonDataItem->name") { // update existing field values $getExistingPage = $pages->find("template=import_page, title=$jsonDataItem->name"); // update value $getExistingPage->setAndSave('field', $jsonDataItem->x); } else { // create new page $pageTitle = $jsonDataItem->name; $p = new Page(); $p->template = 'import_page'; $p->parent = $pages->get(xxxx); $p->title = $pageTitle; $p->save(); } } // search for pages wich are not anymore in the json and hide/delete them // …
-
I want to have filters with month names in german. I fetch them from date-fields with strftime('%B', $timestamp); But i'm not able to add them correctly to a wireArray() What's the right way to do that? $ms = wireArray(); $m1 = strftime('%B', 1579643232); // Januar $m2 = strftime('%B', 1583107200); // März $ms->prepend($m1); $ms->prepend($m2); foreach($ms as $name) { echo "$name "; // result: Januar M�rz }
-
Hello, Still in my 'teaching game'. Here's my problem : I 'find' all players with a request like $allPlayers = $pages->find("template=player"); [/code Then, I limit to players belonging to the team of the logged in player with [code] $teamPlayers = $allPlayers->find("team=$loggedPlayer->team"); No problem so far. But my scoreboards rely on either : $allPlayers->getItemKey($loggedPlayer); or $teamPlayers->getItemKey($loggedPlayer); to find the logged player's position in the charts. On the 'global' newboard with scoreboards based upon $allPlayers, everything works as expected. BUT on my 'team' newsboard, even though I'm using $teamPlayers, the returned indexes are based upon $allPlayers. Am I clear ? In other words, I have a total of 125 players, and my logged player is 61 out of 125 regarding the number of places he freed. But in his particular team of 25 players, he sould be 15 whereas he's still 61 I'd like to reset my indexes (and start back from 0), but I can't find my way out of this... If someone has a hint to help, I'd appreciate. I have a second part in my worry : I had a way around it by simply making another 'raw' request : $teamPlayers = $pages->find("team=$loggedPlayer->team"); Then my team indexes were right, but I faced another issue : Reordering my wirearray according to the scoreboard I want usually worked fine (simple sort() based upon an integer field, for example, player's coins, player's karma...) and indexes were updated BUT resorting with places.count ('places' field is a pageArray) doesn't update the indexes returned by getItemKey and my logged player is always at the position he was when I first did my initial $pages->find() query So my way around found its limit and that's why I'm posting here, after struggling with this for a couple of hours... Thanks in advance for the help.
-
While ProcessWire and WireArray does not have support for array_chunk, there is a simple way to achieve this. With array_chunk() you can easily add DIVs to a foreach loop, without having to set up counters when using general PHP (hat-tip to Laurance over at StackOverflow). The idea in a ProcessWire context is to use array_chunk() to split an array into chunks - and use eq() when looping page results. Simple example that will split a WireArray into three columns. Before we begin, you should know the array_chunk syntax: array_chunk($array, $chunk_size, $preserve_keys=true|false). <?php $p = $pages->get('/news')->children('limit=15, template=article, sort=-sort'); ?> <div class="row"> <?php foreach (array_chunk(range(0,14),5) as $chunk): ?> <div class="col"> <?php foreach ($chunk as $i): ?> <h5><a href="<?=$p->eq($i)->url?>"><?=$p->eq($i)->title?></a></h5> <?php endforeach; ?> </div> <?php endforeach; ?> </div> A more realistic example: <?php $p = $pages->get('/news'); $pp = $p->children('limit=15, template=article, sort=-sort'); ?> <h2><a href="<?=$p->url?>"><?=$p->title?></a></h2> <div class="row"> <?php foreach (array_chunk(range(0,14),5) as $chunk): ?> <div class="col"> <?php foreach ($chunk as $i): ?> <h5> <a href="<?=$pp->eq($i)->url?>"><?=$pp->eq($i)->title?></a> </h5> <?php endforeach; ?> </div> <?php endforeach; ?> </div>
- 2 replies
-
- 3
-
-
- array_chunk
- chunk
-
(and 2 more)
Tagged with:
-
In a module-context, as we know, stuff like single file or image fields return 'arrays', i.e. become iterable. This means that directly using WireArray::Iterable() to check whether a field is iterable can be misleading. Other than to temporarily set a page's output formatting to true (still within a module context), checking iterability of a field on the page, then reverting the page's output formatting to false after the check, is there another/a better way to achieve the same feat? Thanks.
-
Hi, I build a new module and try to convert data into WireArray to get the PW API benefits (find('selector'), get('selector'), ...). Some code to my tests... Define a custom WireArray class class CustomWireArray extends WireArray { public $toStringString = ''; public function __toString() { return $this->toStringString; } public function __get($key) { return $this->$key; } public function __set($key, $value) { $this->$key = $value; } } Create a new CustomWireArray $customWireArray = new customWireArray(); Create a new array item $item = new customWireArray(); // set properties to $item... $customWireArray->add($item); // add to the customWireArray So far it works fine. No problem to find() / get() properties from the $customWireArray, but I need sub-items Tested it with a sub-customWireArray and also a simple stdClass, but it won't work with PW find() / get(). Is there a way to get sub-properties work with something like that? $result = $customWireArray->find('property.subfield=MyValue');
-
So I'm outputting an image field array, I believe this is known as WireArray (bare with me, I only started developing for ProcessWire Yesterday). The cheatsheet has to be one of the best things ever! Not only does it show how simple ProcessWire is and how well crafted the API is, kudos to whoever came up with the cheatsheet. One thing I would like to see is example usages when you click more because at the moment, more doesn't really tell you anything more. I can tell it's inspired by jQuery's API documentation so I have no doubt this will be coming. So, moving on to the problem I'm having, looking at the cheatsheet $a->shift() will remove the first item for the array and then return it. So I have $gallery = $page->gallery; // A multi-upload image field $featured = $gallery->first()->size(800,550)->url; // Works fine $slider = $gallery->shift() // Doesn't work $slider = $gallery->pop() // Doesn't work $footer = $gallery->last()->size(800,1100)->url; // Works fine So my page layout goes like this: ___________ |__________ | <-- $featured // First Image | | | $slider // Image Gallery --> | | | <-- $page->content // Body Content Field |_____|____ | |__________| <-- $footer // Footer Image I have no idea if that example helps, but for usability that's how I want the gallery to work, so the user can just drag all their images in at once, move the featured to the front and the footer to the back. However $gallery->shift() returns nothing and $gallery->pop() returns nothing. $gallery->slice(1) get's rid of the first one in that array but I want to get rid of the first and last. Thanks, Tom
- 2 replies
-
- WireArray
- Image Field
-
(and 2 more)
Tagged with:
-
When I'm using wireArray, both with repeater and image field with more than one image, I have a problem getting the content of the array. For example, I have the following code where info_tabs is a repeater of textarea fields. $texts = $page->info_tabs; foreach ($texts as $tab){ echo "<div>; echo $tab; echo "</div>; } Instead of echoing the text, the page is echoing numbers, it is echoing the numbers 1029-1032. why is that?
-
came across a strange edge case; I'm creating a new WireArray to collect images from various pages, which all get output into a gallery. In this particular site, the client had several images all with the exact same name on different pages, and when those were imported into the WireArray, they are de-duplicated, even though they are different images on different pages, so those images were not showing. I worked around it by renaming the images on the fly, and i can use the custom upload names to prevent this from happening in the future, but it does seem like someone else might run into this, not realizing that the wireArray is assuming the same named images are the same on import; i guess maybe i could have imported them into the wireArray using this syntax? $key = 0; foreach($p->product_images as $image) { $productImages[$key] = $image; $key++; }
-
Hello, I'm back . So I am currently doing some more work on a template that holds virtual exhibitions for my gallery project. The way that the template currently functions like so. An exhibition contains artworks and artwork sections. The artwork can either be a child of the page, a child of a subsection (when this happens there will be a page division and headline text), or it can be specifically chosen via a multipage selector inside of the template. What I would like to do is implement pagination into this. Currently I grab all children items by using a selector , and then combine everything from the pagefield to a variable $items by executing $items->import. Is there a simple way to limit the items displayed once everything has been combined. Something like $items("limit=35")? This is my current logic: if ($page->Artwork_in_Inventory){ $inventoryItems = $page->Artwork_in_Inventory; } $items = $inventoryItems; $items->import($page->children);
-
A recent post on PageArray find() made me try and fix a couple of things discovered there (and in some earlier posts, I think). What I came up with is a few modifications to WireArray class (base class for PageArrays): sort() accepts multiple fields to sort by. Fields can be given as a comma separated string "-modified, title" or an array of strings array("-modified", "title") find() supports start=n and multiple sort=field selectors some optimizations for sorting and limiting even quite big arrays without losing performance in certain conditions The new implementation seems to give same results as the old one where applicable (given only one field to sort by and no start selector for find()), but more tests are definitely needed. I've tested this on a site with more than 15000 pages and find()'ing 3000 of them at once for sorting/finding with the API calls. Performance is about the same as it was before. Even giving more than one field to sort by doesn't make it noticeably slower if limit is being used as well (those optimizations actually work). The replacement for wire/core/Array.php is attached if anyone's interested in trying it out. Array.php I tried some different methods of sorting as well, but just to find out Ryan had a good reason for all the little things I couldn't get a hold of at first . PHP sorting methods being unstable must have turned much of my hair gray in the last couple of days... While testing this I found out that sorting by a field with empty values at some records would act differently when done at initial $pages->find() (database query that is) compared to $aPageArray->find(). While this isn't affected by the WireArray code, I may dig deeper into it later. Giving sort=random works as it did before, but the code doesn't handle the situation it's not the only sort field given. Left a TODO-note in the comments on this (and the missing trackChange() call as well). @ryan: I will try to make a pull request as well just for the fun of it (being new to git). Do whatever you like with the code if you see any potential in it. You may want to rename function stableSort() and its last argument to something else at least. I know I'm not too happy with them.
-
Hello, still trying to understand everything ... I have an array of page IDs, now I want to push the pages with those IDs into a wireArray to use the PW methods on it, like limit and sort. How can I create a new wireArray to hold those pages? I tried $rp = $pages->makeNew(); but get an error ("Error Exception: Method Pages::makeNew does not exist or is not callable in this context") Thanks for help! thomas