Select Random Page Image with ProcessWire - php

I'm just starting out using the ProcessWire system and really enjoying it.
On my Home Page, I would like to display an image from a random page. The page can be ANY page as long as the it is the child of the parent page with ID '1010'.
Is it possible, and if so, how do I achieve this?
My current code for showing the home page image is this:
if($page->image) echo "<img src='{$page->image->url}'>"; however, I'd like to select a random image from any of the children pages of the above parent ID.
I found this, but wasn't sure whether it would be of any use.
Many thanks for any pointers :-)

You should try something like this in your template's code (assuming that your image field is called image):
/* Find all children of page with ID 1010 that include an image */
$allChildPages = $pages->find('parent=1010,image.count>0');
/* Select a page from all children in the PageArray randomly */
$randomChildPage = $allChildPages->getRandom();
if ($randomChildPage->image) {
echo "<img src='{$randomChildPage->image->url}'>";
}
Have a look at the relevant code:
$pages->find() returns a collection of Pages (matching the Selector) as a PageArray (that extends the WireArray class).
$anyWireArray->getRandom() returns a random element of itself.
Also have a look at this forum thread where several strategies to randomize images from different pages are discussed.

Related

Phpdocx: How to duplicate a page in template

I want to ask, is it possible working with a template, and duplicate a specific page in that template without using WordFragment or addExternalFile (I want the template to be 1 file only)?
As an example, I have 3 page, I want to duplicate (or repeating) page 2 for 4 times, so the result is that I have a 7 paged document with a second page repeating for 4 times? Is it possible to do it without WordFragment or addExternalFile? Because I want to make it easier to edit in the template.
My Phpdocx is a trial version.
What I have tried so far is using wordfragment, by using $wordfragment$ variable in document template (in second page), and do this in php:
$wf = new WordFragment($docx, 'document');
for($i = 0: $<4; $i++) {
/*here I put a code to make a text and table in wordfragment (sorry it's too long to write it here, it's about 500 lines)*/
$wf->addBreak(array('type'=>'page')); //it's to go to next page
} //so this loop will make 4 same pages with the content I want in that 500 lines of code.
$docx->replaceVariableByWordFragment(array('wordfragment'=>$wf), array('type'=>'block'));
Using a WordFragment like above is actually very powerfull, and it is have a very great result, but since it will be used by my user, and we always presume that the user is not good with code, so I cannot let the user to edit the code if he/she just want to edit the template, in example, if the user want to edit the table in template, in this code of mine, that user need to edit it in that 500 lines codes, and that's very not user friendly, what I wanted is to make it more simple, make the text and table in word (so the user could just edit it in there), and in the code I just need to repeat that page. Is it possible?
Oh and the other method, addExternalFile is actually quite good, but by using this, I need to have more than 1 template, in example, I need 3 document, named start.docx, content.docx, end.docx. With addExternalFile, I could just used it like this:
$wf = new WordFragment($docx, 'document');
$wf->addExternalFile(array('src'=>'start.docx'));
$wf->addBreak(array('type'=>'page'));
for($i=0;$i<4;$i++) {
$content = new CreateDocxFromTemplate('content.docx');
/*here I put a code to replace all text and table variable in template*/
$content->replaceVariableByText($variables, $parameter); //$variables and $parameter already included in my code
$content->replaceTableVariable($table); //$table already included in my code
$content->createDocx('newcontent.docx');
$wf->addExternalFile(array('src'=>'newcontent.docx'));
$wf->addBreak(array('type'=>'page'));
} //this loop will make 4 same page
$wf->addExternalFile(array('src'=>'end.docx');
$docx->replaceVariableByWordFragment(array('wordfragment'=>$wf), array('type'=>'block'));
With addExternalFile, I could write text, variables, and draw a table in document and not in wordfragment. It's quite user friendly, but the flaw is that this method need atleast 3 different document to be opened by user if they want to edit it. What I want is just 1 document, all text, variables, and tables are in document, and there's no text or table in php code. Is there any method to do this with phpDocx?

Contao: Frontend - Get theme section of another page - not the current page

I would need to reach a custom section by page id (or PageModel Object), but I don't find a way to get the FrontendTemplate. I would like to use it in a dropdown navigation to echo a custom section of the hovered (parent) page.
If somebody will search for, the answer is:
<?php
$articles = \ArticleModel::findPublishedByPidAndColumn($id, $column_name);
echo static::getArticle($articles[0]); // for ex. the first article
?>

session variable that records an onclick element

Lets say i have 5 pages which has some hierarchy and all pages are in same folder. When i click on one page its going to another page in same folder.
Example
fruits.php is main page and banana.php, apple.php, orange.php & pineapple.php. All the later 4 has links in main page. I need to display navigation in all the pages like if its main page just main page. If its a banana i should display as Main_Page>>Fruits>>Banana ans so on how can i achieve if i have n number of hierarchies lets say flowers(lily.php, rose.php), Eatables(chocolates.php, biscuits.php) etc and all pages in same folder.
make folders foreach category and have them in the main folder:
main/flowers
main/fruits
...
include php files in respective folders:
main/flowers/lily.php
main/fruits/apple.php
...
now use javascript to read the url of the current page:
var url = window.location;
var cat = url.split("/");
cat[cat.length - 1] = cat[cat.length - 1].split(".")[0]; // to remove ".php" part from "lily.php" and get "lily" to display
now you have those names in cat array, so you can display them in html as you need:
alert(cat[1]+"/"+cat[2]); // should popup something like fruits/Apple
First, what you are trying to make is called "breadcrumb". If you google it in this words you'll have too many hits.
There are too many ways to achieve this according to your site scheme. If you have a collection of your pages in your DB you can find the hierarchy of pages to show in breadcrumb by querying the DB. If you don't have this, I can advise you to make a folder hierarchy as:
Main
|--fruits.php
|--flowers.php
|--Fruits
|--apples.php
|--bananas.php
|--Flowers
|--lily.php
|--rose.php
Then on each page you can show your folder hierarchy as breadcrumb. This may be an easy way to start.

How do I get a galleries cover image in modx revolution?

How do I get a galleries cover image in modx revolution?
Currently I'm calling the gallery by ID and limiting it to one image ~ however, this does not garantee that the image will be the cover image.
this is what I am currently doing.
[[!Gallery? &album=`[[!+tv.vehiclegallery]]` &toPlaceholder=`gallery` &thumbTpl=`homepageVehicleThumbTpl` &limit=`1`]]
[[+gallery]]
The answer is way more simple! I was looking at this for hours and couldnt figure it out, then it hit me. The cover of every album is always called "cover.jpg".
And it will be stored in the assets folder with the corresponding Album-ID.
Path abstract:
assets/gallery/[album-id]/cover.jpg
For example:
assets/gallery/1/cover.jpg
So as long as you have the Album-ID you can get the cover.
Getting the Album-ID
If you want the Album-ID heres one way:
Create a Template Variable called gallery_album with the input type galleryalbumlist.
Make it accessible to your preferred template.
Open up, or create a resource with that template.
The Template Variable will be in the Template Variable Tab as a Dropdown containing
all the previously created albums.
Choose an album from the dropdownlist
when you know call [[+gallery_album]] in the template, it will output the Album-ID
Code example: <img src="assets/gallery/[[+gallery_album]]/cover.jpg"
Link to the modx extra Gallery:
https://docs.modx.com/current/en/extras/gallery/gallery/index
I would up doing this in a snippet instead:
<?php
//getgalleryAlbumCover
$output = '';
$sql = "select * from modx_gallery_albums mga where mga.id = $id";
$album = $modx->query($sql);
if (!is_object($album)) {return;}
$data = $album->fetch(PDO::FETCH_ASSOC);
// just in case ;)
$modx->toPlaceholders($data);
$output = $data['cover_filename'];
return $output;

How to make PHP code to check if title contains certain word? (Wordpress Shopp)

I'm using the WordPress Shopp plugin to sell products on my WordPress site. So far I love this plugin but I'm trying to do something custom and unfortunately I'm not good enough with PHP to figure this out on my own (I've done tons of research too!).
Here is what I am trying to accomplish...
To display the product title for each individual product Shopp uses the following code to reference the product title...
<?php shopp('product','name'); ?>
What I am trying to do is reference the product name and if for example the product name says something like 'FLIR H-Series Thermal Imaging Camera' I want my code to detect whether or not the product title contains the word 'FLIR' in it and if it DOES contain the word 'FLIR' I want it to echo a FLIR logo image (let's say the URL of the image is http://example.com/images/flir.jpg). Alternatively I would want it to echo a Thermal-Eye logo image anytime the word 'Thermal-Eye' is found in the product title, etc. How could I go about modifying the code to do this?
Hopefully this make sense, thanks for any help!
<?php
$keywords = array('FLIR', 'Thermal-Eye');
foreach($keywords as $word)
{
if(preg_match('/'.$word.'/i', shopp('product','name', 'return=true')))
echo $word;
}
?>
UPDATE: If you want to echo out a unique image based on the word, rather than the word itself, you can do something like:
if(preg_match('/'.$word.'/i', shopp('product','name', 'return=true')))
echo '<img src="'.get_bloginfo('stylesheet_directory').'/images/'.$word.'.jpg" />';
So in the case of FLIR, you just need to make sure FLIR.jpg exists within the images folder in your theme directory.
Best thing to do is to play around with it and see what works for you.

Categories