Drupal: Accessing all rows in a view as an array - php

Apologies for the awkward wording in this question; I'm still trying to wrap my head around the beast that is Drupal and haven't quite gotten the vocabulary down yet.
I'm looking to access all rows in a view as an array (so I can apply some array sorting and grouping functions before display) in a display output. The best I can tell, you are able to access individual rows as an array using row-style output, but seemingly not in display output.
Thanks!

You have to change the Row style setting: to NODE.
Click on Theme Information.
Create an file with the name of one you find in the Display output point (I would use the second one eq. views-view--portfolio.tpl.php)
And now you can use your own Node Template and access the $node variable.

Ultimately, I had to use node_load on each item and load the results of that into an array. Inefficient, but it worked.

I found this thread on Drupal.org about this question, but those solutions don't quite work.
How to get a "result Array" with views_get_view() (as with views_get_current_view())
They return only the list of IDs, not the actual rendered fields.

Related

PHPWord Prevent rows from the same table appearing on different pages

thanks for help with previous PHPWord issue. I have another.
I'm creating documents with tables where each table is basically used as a stylized container for a list of items. For example, in my case i have a collection of legal definitions. Each definition has a code, title, and textual description that appears in a table. So when there's multiple definitions, each definition has its own table, and appears like so:
and so on. Each table isnt really a table, its more i'm kind of hijacking tables to stylize my document. The problem is when I have a long list of items, invariably some of the tables will be split between pages where the top row of the table will be at the very bottom of the page and bottom row of the table will be at the very top of the next page, like so
This is very undesired. Is there any way to tell PHP word that "hey, if this table is going to be split between pages, just put the whole table on the next page" ??
I'm also using PDFmake for making pdf's and it has a pageBreakBefore function that can be used for exactly this purpose. I notice that paragraphs have a pageBreakBefore style which can force each pragraph to appear on a new page, but this isnt what i'm looking for. Is there some way i can maybe get into how PHPWord builds the document to put a conditional test in maybe?
Any input is greatly appreciated thanks.
I had the same problem .... and found the answer today in "normal" MsWord documentation. I found the equivalent in the phpWord doucmentation, tried it and it works:
In your paragraph formats, set 'keepNext' => true

ModX revo 2.2x: How to use a TV value to set tpl?

I'm trying to figure out a way to render a resource with a specific tpl based on a template variable(tv) value. here is my use case:
I have setup a modx installation for a basic site with a homepage and a blog (I used the articles add-on). I want to display blog posts on the home page, outside of the articles container. From my experience the easiest way to do this is with getResource. However for this specific project I would like to change the tpl of the getResources results based on whatever the tv value is for each result.
The template variable can be looked at as a "post type". if you choose "text" it would have tplA, if you choose "multimedia" it would have tplB etc...
now based on my research you would use the properties 'tplCondition' and 'conditionalTpls' to achieve this in your getResources call, something like this:
[[getResources?
&tplCondition=`tv.blogPostType`
&conditionalTpls=`{"1":"tplA","2":"tplB","3":"tplC"}`
&tpl=`defaultTpl`
]]
The problem is, this does not seem to work with template variables :( it even says it only uses resource fields in the documentation.....which is a REAL bummer, as i have no idea to pull this off otherwise. Based on my limited knowledge, you can maybe create a snippet or something that does this, But i have no clue.
Does anyone have an elegant solution to this problem?
to sum up what im trying to do, again:
-assign a "post type" to blog posts in my articles container via template variable.
-use the template variable value to set a specific tpl based on that value.
any help is highly appreciated. thanks
EDIT: okay I got a reply on the forums and have come across a"solution" to this.
you can accomplish this with css. in the tpl, you do something like:
<article class="[[+tv.post-type]]">....</article>
this will output the post type selected in the tv as the actual element class used, and you can then use css to give the output different looks based on the tv. its actually so simple im a little embarrased i didnt see it before. In my situation it 100% solves my problem, however if you needed to chasnge html im guessing javascript would need to be involved or another method with php. just leaving this here in case someone needs it!
If you need to do major changes to the html you could still probably solve it with css if you have a nice markup. However if thats not enough i would solve it by using one tpl for the getresources call, and letting that tpl handle the switching. If you could have your TV output the value symbolize the name of the tpl that should be used in each case, your tpl would only need this:
[[$[[+tv.yourtv]]]]
Your tv will be evaluated first, and then it will be processed as a chunk. That way you dont need a nasty switch or if-clause.
This is assumin you want toally different tpls, and not just need to change some small part of the standard tpl, in that case you could still use this tecnique though!
Conditional templates seem to work fine with pdoResources (part of pdoTools). (I have to put "tv." in front of the TV in the tplCondition but not in the other TVs)
James is correct.
[[pdoResources?
&includeTVs=`blogPostType`
&tplCondition=`tv.blogPostType`
&conditionalTpls=`{"1":"tplA","2":"tplB","3":"tplC"}`
&tpl=`defaultTpl`
]]

php xpath run multiple queries on xml file and then match results

I have an XML Feed with deals and I need to get the titles and the images.
I use xpath and this is an example: /deals/deal/deal_title (for deal title)
/deals/deal/deal_image (for deal image)
The problem is that some deals don't have the deal image set at all so when I link deal title with deal image I sometimes get the wrong image.
In order to track down the problem I created two separate arrays: one with titles and the other one with images.
The weird thing that causes the problem is that on the images array the empty instances are moved to the end of the array.
For example if we assume that "deal title2" has no image and "deal title3" has image the "deal title3" image is used for "deal title2".
Use this link to see the code I made: http://pastebin.com/HEuTJQjZ
The interesting part starts from: $doc = new DOMDocument();
Basically what it does is to execute many xpath queries to get titles, images, prices etc and then it adds them to the database.
The problem starts when a deal doesn't have a tag set so it just uses the next value.
I don't understand how it magically moves all the empty instances to the bottom. Xpath isn't supposed to order the results, right?
I have even tried to use the [] operators to get the specific image but doesn't help since the results are sorted the wrong way.
Example feed: http://www.clickbanner.gr/xml/?xml_type=deals&affiliate_ID=14063
EDIT:
The real problem is that xpath does not order the results by document order and modifies the expected order. Is this a bug or something or is there a way to force the results to order by document order? See also: XPath query result order
Thank you in advance.
Evaluate the following two XPath expressions for any values of $k in the interval [1, count(/deals/deal)]:
/deals/deal[$k]/deal_title
and
deals/deal[$k]/deal_image
In this way you know whether an image was selected, or not.
For example, if count(/deals/deal) is 3, then you will evaluate these XPath expressions:
/deals/deal[1]/deal_title and deals/deal[1]/deal_image
/deals/deal[2]/deal_title and deals/deal[2]/deal_image
/deals/deal[3]/deal_title and deals/deal[3]/deal_image
I think you should try this way:
Walkthrough /deal/deal_id tag values
When search for a pair of tags: /deal[/deal_id="$deal_id"]/deal_title and /deal[/deal_id="$deal_id"]/deal_image (using real deal_id) in place of $deal_id
You will get pairs of deal_title and deal_image for each deal and they would match each over correct

Ideas for importing text data using PHP array functions

I am new to php and am asking for some coding help. I have little experience with php and have gone to the php.net site and read couple books to get some ideas on how to perform this task.
There seems to be many functions and I am confused on what would be the best fit. (i.e. fgetcsv, explode(), regex??) for extracting data in the file. THen I would need assistance printing/display this information in orderly fashion.
Here is what I need to do:
import, readin txt file that is
delimited (see sample)
The attributes are not always ordered and some records will have missing attributes.
Dynamically create a web table (html)
to present this data
Sample records:
attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;
How do I go about this? What would be best practice for this? From my research it seems I would create an array? multidimensional? Thank you for your time and insight and i hope my question is clear.
Seems like homework, if so best to tag it as such.
You will want to look into file(), foreach() and explode() given that it is delimited by ;
The number of attributes should not matter if they are missing, but all depends on how you setup the display data. Given that they are missing though, you will need know what is the largest amount of attributes to setup the table correctly and not cause issues.
Best of luck!
i would first use the file() method, which will give you an array with each line as an element. Then a couple of explodes and loops to get through it all,first exploding on ';', then loop through each of these and explode on '='.

Multi-Level Bullets Implementation

What is the best way of implementing multi-level lists/bullets in php?
What I'm trying to implement is a system to take in short phrases, and categorize them within sections. However, at the same time, I would like to have each section to be collapsible and capable of having sub-sections within them.
For Example:
Section 10
Section 10.X
Section 10.X.X
I was thinking of having each sub-layer as an array, but I realize that I've seen the same type of collapsibility in many places, and they all seem to be similar. Maybe there already is a package or library that does this? And if there is, how should my data-structure for keeping each entry be structured?
Your bullets are being done in HTML, as PHP merely generates HTML code. Consider using nested <ul> tags.
The collapsing is most probably going to be javasscript. (Although I wonder if the CSS :active psuedoclass would work in supportive browsers...)
Regarding the package, I'm not sure. You essentially need a recursive list generating function.
For the kind of structure you mentioned, i think you need what is know as Tree View. Check it out on how to implement that.
Screenshot:
Anything to do with toggling, display lists, indenting etc should be achieved relatively easily with HTML/CSS/Javascript.
Inside your category table, you could create a parent_id field which matches to your primary ID. Then run loops to display your TLD as well as another loop to display the categories within the parent.

Categories