-
Posts
45 -
Joined
-
Last visited
Everything posted by Reid Bramblett
-
Sorry for the confusion. I was using "field" generically, as a stand-in for whatever particular field you are after. Say you want to find all children that have the field "address" populated. In that instance this works to find all immediate child pages that have something in the "address" field: if ($page->child->address) ... but this does not work to do the same if you want to canvass all children, grandchildren, etc. down the page hierarchy: if ($page->children->address) Though this does: if ($pages->count("has_parent=$page, address!=0"))
-
Remove duplicates in a foreach loop (but count them)
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
Incidentally, @Macrura, your first suggestion is also now working. No idea why it failed yesterday, but the PageArray version is humming along smoothly now. (Each "interest" is, indeed, a unique page stored under an Interests page.) I will definitely use the PageArray version since it will allow me superior sorting options (er, once I figure out how to fiddle with arrays and do the counting part; as I said: PHP noob). Thanks again. -
Remove duplicates in a foreach loop (but count them)
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
OK, I hereby promise to be better about posting "it don't work!" replies at the end of the day when I'm too tired and rushed to take the debug time to notice a simple thing like a missing close parentheses. (Even as a PHP newbie I should have known this, as I often try to solve such problems by sticking semicolons and/or right curly braces at the ends of random lines of code until the thrown error page goes away, figuring I must have missed one somewhere.) The second code works perfectly now. Thanks all. -
Great for usage within a page, but what about for subpages? This works when all you are dealing with is immediate children : if ($page->child->field) ... but for some reason this doesn't work if you want to canvas all children, grandchildren, etc.: if ($page->children->field) Nor does the array version work: if (count($page->children->field)) I know I can get it elsewise, for example: if ($pages->count("has_parent=$page, field!=0")) ...but it just seems like the $page->children->field should work.
-
Remove duplicates in a foreach loop (but count them)
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
Thanks for all the speedy replies! Macrura, your first example ends up spewing out even more duplicate hits. Haven't tried troubleshooting that method much yet because I like the backwards logic of !count, but when I try that it throws an error: Parse error: syntax error, unexpected 'continue' (T_CONTINUE) in /Applications/MAMP/htdocs/test-pw/site/templates/_main.php on line 339 -
I am trying to create a list of all the "interest" tags associated with a set of pages. A page can have any number of tags, which are stored in an "interests" field. I can generate a list of all these interests from the requisite pages in a foreach loop, but of course I get all instances of all interests—so if a particular interest tag is on four of the pages, it shows up four times. My code: <ul><?php $kids=$pages->find("template=poi, has_parent=$page"); foreach ($kids as $k) { $ints=$k->interests; $ints->unique(); foreach ($ints as $i) { "<li><a href='interest/$i->name'>$i->title</a></li>"; } } ?> </ul> (I left the "$ints->unique()" line in there—though it is not actually doing anything—because I was convinced it would work. In fact, I played around with jamming "unique()" into more than a dozen places and variations, but perhaps I am misunderstanding that function; I am still a bit shaky on WireArray vs. PageArray.) My most pressing question is: How can I limit the list of results to just one instance of each interest tag. The irony is, I do actually care about how many times an interest tag is used, because on my wish list for this snippet of code is the ability to count that total number, use it to sort the list of interests (by frequency of occurrence), and then also pop it into the list beside each interest title, so the end result would look like this: Interest 1 (20) Interest 2 (16) Interest 3 (12) Interest 4 (8) ... only, you know, with better CSS styling. I've trawled the forum, but none of the solutions to similar situations I found here worked for me, and I poked around the sample blog profile (which has something a bit similar) for inspiration, but to no avail. Any ideas? Thanks.
-
Had I not taken a vow against using emojis, I would be employing the face-palm one right now. Thanks, @Macrura. I was so hellbent on figuring out where to stick the ! into my selector field(s), I forgot I can just "not" the whole string.
-
Sure, LostKobraki. As I said, I ended up abandoning this hack of the search method, which I only investigated as a workaround to get what I really wanted—and my new solution is far more useful and user-friendly on the front end. The fundamental building blocks of my site are the POIs—the items the user wants to see. Everything else is structural or organizational. (POI is an ugly acronym for the even uglier phrase "Point of Interest," but a useful catchall for a travel site like mine for museums, hotels, cafes, bios, and explanations of how the local buses work.) So, in a two-step process, I first needed to generate a list of "interests" tags associated with a given POI. Easy. What befuddled me for a few days was the second step: How a click on an "interest" would generate a page of results of links to all pages using the POI template, containing that interest 'tag,' and located in the same "place" as the original POI. (POIs live in the page tree structure under their given place—London, Paris—since that just makes life easier, even if a database normalizer would howl at me for not making "place" a separate table.) My eventual solution used url segments: In the _main.php (which populates the POI pages): <?php if ($page->interests) { ?> <div class='panel-plain'> <h3>Similar interests:</h3> <ul><?php foreach ($page->interests as $i) { echo "<li><a href='../../interest/$i->name'>{$i->title}</a></li>"; } ?> </ul> </div> <?php } ?> I then created a (hidden) interest.php to live at the root of the place folder: <?php if ($input->urlSegment1) { $int = $sanitizer->text($input->urlSegment1); $place = $page->parent->title; $pois = $pages->find("template=poi, place=$place, interests=$int"); $content .= "<h1>$place: $int</h1>"; $content .= renderNavPanel($pois); $browserTitle = "$place: $int"; $title = ""; } else { $content = $page->body;} (The renderNavPanel lives in my _func.php and makes up a fancy little playing card–like view for each POI.) Not only is this more extensible that sending all requests to the (or a) search page, but it is cleaner and more logical for the user and—hopefully—SEO. The url for the results, instead of being site/search.php, is now site/places/london/[interest]/.
-
Though I generally like to be a positive guy, I wish there were also a $page->isnot for certain situations. After all, sometimes 'tis easier to exclude only the two or three unnecessary templates than iterate through all 13 other templates you _want_ to echo the "something." (Also, I couldn't even get the "plain vanilla php" solution to work, either: <?php if ( ($page->template != 'foo') || ($page->template != 'bar') ) echo "something"; ?>
-
Can't puzzle this one out. I structure as you said (using categories/category/subcat, actually, but I'll stick to the geographic example to keep things simple), and it _looks_ as if it is working brilliantly. The proper set of cities pops up when I select a country. However, it won't save. After I click "save," the city select dropdown resets to nothing (in fact, it isn't even populated by any choices until I go back to the country dropdown and select something else), and the city info is not being saved to the database at all. I have painstakingly fiddled with each setting in the associated fields and templates and cannot get it working. Once idea: Could this possibly be because the template on which I am placing these dependent dropdowns belongs to pages that reside on a different branch of the page tree (not nested underneath, in this example, city)? Seems unlikely, but wanted to check before I pulled out still more hair over this.
-
How to use a variable from one template in another
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
Thanks, guys. A few tweaks to diogo's code got me the result I was hoping for. (I was simultaneously trying to figure out if I would instead hack a search function to get what I wanted—nice thing about PW is that you can find many avenues and answers to the same result—but this is far more useful and extensible.) And believe me, Adam. I have the whole thing intricately mapped out. Before I discovered PW, I had a labyrinthian MySQL database model all planned out, with all the one-to-many and many-to-many connections, and a spreadsheet of all the scripts to interconnect them. I;m just having to figure out how to apply PW's logic of pages, templates, template pages, and fields to the site I have in mind. -
Yeah, I caught the missing ; and the $gs for $qs. Still couldn't quite get it to work, but in the meantime managed to find another way to get the results I wanted. (One nice thing about PW: You can find many solutions to the same problem.) Your contributions did help me understand much more about how the search function works, and for that I thank you.
-
I have narrowed my problem down to the fact that I can search for two terms if both terms happen to show up in the same field (say, both are in the body), but not if one term lies in one field (say, title) and the other term in a second field (body). So If I search for "Free" I get all the pages that happen to have the word "Free" in the designated selector fields. If I search "London" I similarly get anything with "London" in those fields. But since "Free" lives in the interests field, and "London" in the places field, if I search "Free London" I get zero results. I suspect that answer lies in LostKobrakai's suggestion that I parse the terms into different selectors, but I have no idea how to do that. (Hey, no shame in admitting ignorance). I fiddled around with the code in Mats' suggested thread, but to no avail. (Again, I admit I was basically poking the code around blindly, putting stuff in different places, changing out operators, etc. and seeing what happened).
-
I am building off the default install, and noticed that, in practice, the included search template limits me to a single search term (or an exact phrase of more than one word). How can I get it to search a simple boolean-type string, with an AND or OR? The template PHP code: $q = $sanitizer->text($input->get->q); if($q) { $input->whitelist('q', $q); $q = $sanitizer->selectorValue($q); $selector = "title|body|interests~=$q, limit=50"; if($user->isLoggedin()) $selector .= ", has_parent!=2"; $matches = $pages->find($selector); if($matches->count) { $content .= "<h2>Found $matches->count pages matching your query:</h2>"; $content .= renderNavPanel($matches); } else { $content = "<h2>Sorry, no results were found.</h2>"; } } else { $content = "<h2>Please enter a search term in the search box (upper right corner)</h2>"; } I've tried every permutation of entering the data I can think of (x y; x AND y; x&y; x, y; "x" AND "y"; x + y; etc.; etc.) , but whenever I put in more than one search term, I get zero results—even if each of the words, when searched alone, does produce results. I even experimented replacing the two $sanitizer types with bool, but that threw ugly errors. About to pull my hair out over this. I am sure there is a simple answer, but as I haven't worked much with any sort of input features (built static sites for years), I cannot seem to crack it. Forgive me is this if Kindergarten-level. Thanks.
-
How to use a variable from one template in another
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
OK, after trying many permutations and substitutions, i think I am having a problem back at square one with Alex's solution. Where, exactly, do I stick that $url = "/interests/interest/{$page->parent->parent->id}/"; into my poi.php (or, rather, my _main.php that is being called into poi.php)? Is it in the foreach loop thusly: <?php foreach ($page->interests as $i) { $url = "/interests/interest/{$page->parent->parent->id}/"; echo "<li><a href='{$url}'>{$i->title}</a></li>"; ...because that doesn't work. It just sends me to a (non-existent) "interests/interest/1024" page (1024 is the id for London) at my localhost root. I've tried jamming it into every level of the site by prefacing the folder with varying strings of "../../," but it doesn't seem to be calling the interest.php template no matter where I send it, from the site root (siteroot/interests/interest/1024) all the way to a sub of the current POI page (siteroot/places/london/do/poi2/interests/interest/1024). I've also tried changing things up, but to no avail (I keep feeling that the {$page->parent->parent->id} really should be something more like interest->name, since that is what we're actually talking about, but figured this hack was how we were going to piggyback the place into the url to be played with later). -
How to use a variable from one template in another
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
Ack! In my excitement, I posted too soon. While it is, indeed, now limiting things to just the appropriate "place," it is rendering all pois in the cat (see, do, etc.), not filtering them by interest at all. Also, I get an unwieldy url: "places/london/so/interests/interest/london," when it woudl be far better to have "places/london/do/interests/interest/[name of interest]." I will tinker and see if I can't get it working properly. At least it is doing _something_ now! -
How to use a variable from one template in another
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
Oh, snap! Adam, your solution did, in fact, work. Before biting the bullet and doing a fresh install to try and track down my problem, I moused over the links one more time in hopes of a revelation... and got one! It was linking to interest/interest from the root of localhost, not from the site root. All I did was add a ".." before the initial slash in the $url call in poi.php and it worked! //poi.php $url = "../interests/interest/{$page->parent->parent->id}/"; //added ".." before /interests/interest Thanks! But the question remains: Will this be scalable, and work with the Grand Backend Architecture I mentioned above? I mean, I am about to put on my hardhat and descend into the coding mines to see if I can make it work, but if you Jedi masters can tell at a glance whether or not this padawan will be wasting his time, please do let me know. (Sorry; just finished ordering the just-released Millennium Falcon LEGO set for my son's 8th birthday next month; they had sold out of the old one before last Christmas, and it had been the only thing on his list, so he has been waiting an eternity, in seven-year-old time.) -
How to use a variable from one template in another
Reid Bramblett replied to Reid Bramblett's topic in Getting Started
Thanks, guys. I got the first halves of both suggestions working: Diogo's spoofing the system to think Interests was on the same level as Cat [see, Do, etc] (though I also had to remove the / before interests since my template's defaults are set to have trailing slashes and it was doubling them up), and Adam's fooling it into sticking the place at the end of an interests/interest url (though I tried a variation using the user-friendlier "name" instead of the nonsensical id number for place). The second half of each of your fine suggestions, however, only produced frustration. As I mentioned, I had no luck fiddling with urlSegments earlier, when I was trying to make a Categories tree work, and my failure stands. I am not sure why, and it looks as if I might have to solve this problem before being able to move forward on anything else. Might it have anything to do with the profile I am using? I didn't just do a "Blank" install, but rather started building from the Default profile. It was close to where I was heading anyway, already had a top nav and all, and had sliced things into a _func.php for renders and a _main.php for page content, leaving all the template-based phps little more than containers to string together brief calls of $content .= [something]. As I said, I am new to the structure (and mindset) of building a site based on including files from other files, so I was happy to have someone else (Ryan) do this for me so I could muddle through figuring out how it worked as I went. However, given my noobiness, I may be missing something here? Is that setup what is fouling my attempts to use urlSegments? (I've already turned them on for pretty much every template in my experimentations, just to be sure that setting its not what's tripping me up.) Diogo: I do not mind creating views for template levels. In fact, it might be best. I'd like the site to be navigable (no offense to the lovely "navigatable") in various ways, since no two people consume data the same way. Some want a hierarchy, others a flat map, still other visual cues. I just want to help people find the information they are looking for in whatever way makes the most sense to them. Besides, in the long run I would like to have the "Interests" results reflect whatever level of "places" sent the request. So if you clicked on "History" from a page within, say, Florence, it would show all Florence results with the History "Interest" tag, but if you did so from within its parent in the Places tree, Tuscany, it would show all Tuscan results, and from its parent, Italy, all Italian results. I tried to keep my original question limited because I was hoping that learning a simple way to carry a set variable ("where are we?") over to another template would give me the tool to build the more complex site I had in mind than the example I gave. However, just so you know where I am heading (and in case it changes your above suggestions), I envision these Interests acting kind of like sidebar filters, as you might see on an ecommerce site (size, brand, color, etc.). The user just ticks off all the ones she wants, and the site returns a page showing the POIs (if any) that match all her choices. I am imagining the overall backend architecture to have three main page trees: Places, Categories, and Interests, each with two levels underneath (Places has cities, and under that the POIs; Categories and Interests both have main cats and interest groupings, and then more finely sliced subcats and interests under that). These would all interact. This makes things tricky, but I am sure if can be done! The Places tree is where all the real contents lies; Categories helps organize it all; and Interests (and Categories and their subcats) are there to help users filter results more finely. (The Interest groupings are largely just to provide smaller subsections of Interest tags so that I don't end up with a single, leviathan list on the left sidebar!) Places London poi1 poi2 poi3 poi4... Oxford poi5... Categories See [cat] Monument [subcat] Museum [subcat] Church Do [cat] Tour [subcat] Shop Get active Relax Interests History [grouping] Ancient [interest] Medieval Renaissance Modern Gastronomy [grouping] Food [interest] Beer Wine Markets You get the idea... The reason there are separate Interest and Categories trees is that Interests are horizontal tags, applicable to any POI across all categories (the Interest "Family-friendly" would apply to a Museum under "See" or a B&B under "Sleep"), whereas Categories are vertical (a POI has only one unique Category and subcat). Thanks again for your help. -
I only stumbled across WP a few weeks ago, but have already been able to accomplish far more than over months spent banging my head against the baroque code of several shall-remain-nameless frameworks. I could weep thinking of how much time I have wasted elsewhere. I have, however, hit a speed bump--possibly due to the fact that I am still a bit shaky on how templates interact. My set-up (a sample travel site): Places Place1 see poi1 poi2 do poi3 poi4 sleep poi5 plan poi6 Place2 see poi7 do... Interests Interest1 Interest2 Interest3... "Interests” act a bit like tags assigned to the "poi"s (this is an internal tagging system, open to admin only; no free tags/users cannot assign). For the record, that odd "poi" nomenclature stands for "point of interest" (as a unique name, it's handier than using something vague like "item"). (Aside: Yes, I realize "see, do, etc." could exist instead as a third "Categories" tree. I fiddled with that, but in the end figured I'd just use the already powerful and straightforward parent/child nesting format of pages that PW provides. Also [ahem], I couldn’t get urlSegments to work; another time.) A simple foreach loop on my _main.php page creates a link-list of the Interests assigned to a given poi. <?php foreach ($page->interests as $i) { echo "<li><a href='{$i->url}'>{$i->title}</a></li>"; } ?> When you click on these generated links, it takes you to the appropriate "Interest" page, populated by all POIs assigned that Interest. Here is the relevant part of my Interest.php code: $int = $page->id; $pois = $pages->find("template=poi, interests=$int"); $content .= renderNav($pois); Problem: I do not want to see _all_ the POIs assigned that Interest. I need to limit it to other POIs in this particular Place (the grandparent of the referring POI). In other words, I only want to see other "Museums" (interest) in London, not those in Bath, Manchester, and Oxford as well. This is easy to do in absolute terms (I can limit to "has_parent=London" or something); but I need it to be some kind of $place variable instead, one assigned by the referring POI. For the life of me, I cannot figure out what code to use and where to stick it--in the foreach loop of the poi.php page? On the "Interests" template page? In short, how does this variable (the "Place" grandparent of a given POI) get assigned to the link and hence carry over to be able to be used in the functions being applied by another template (Interests)? I suspect the answer is so glaringly obvious only an interactivity noob like me would not know. That would explain why I canot seem to find the answer after a solid week of trolling every forum thread, tutorial, and wiki at PW. While I’ve built my own static HTML sites for 20 years (wow, that seems long; anyone else remember dividing text blocks with HRs, or the glorious day HTML3 finally allowed us to align GIFs so text could actually flow around them?) I am only a journeyman at PHP and MySQL. I've never even created a form. I know. Shameful. Thanks for your help.