Composr CMS Random Image Mini Block

Let us talk about scripts, HTML, Perl, PHP, apache, etc.
User avatar
Stevyn
SysOp
Posts:1776
Joined:Mon Nov 09, 2009 10:03 am
Location:Japan
Contact:
Composr CMS Random Image Mini Block

Post by Stevyn » Sat Feb 16, 2019 10:58 am

Hello,

Wanted to share this and invite people to make it better… You can change this for your own use in yoru site to show random pictures, etc.

I own a bookshop and want to display books on my website, a random book cover throughout the site that points back to the gallery that has more information for the potential customer.

see example usage:

https://nandalow.com/index.php?page=wik ... owse&id=41

Notice the upper left nandalow bookshop box is displaying a book cover. Now scroll down a bit and on the lower left & right it will display a random book or comic cover. In the center of the page after the links it will display 5 random comicbook covers.

If you hover over the image the tooltip gives the title and invites customer to click for more info. Under the image is the title.

In sources_custom/miniblocks create a file called random-image.php

Code: Select all

<?php
// random-image mini block
// 
// 1 = fantasy___science_fiction
// 2 =
// 3 = comics
$loop = "1"; $pick = "";
$Product="'fantasy___science_fiction'";
if (mt_rand(1, 150) < 20) { $Product="'comics'"; }
if (isset($map['pick'])) {  $pick = $map['pick']; }
if (isset($map['loop'])) {  $loop = $map['loop']; }
if ($pick == 3) { $Product="'comics'"; }
if ($pick == 1) { $Product="'fantasy___science_fiction'"; }

for ($x = 1; $x <= $loop; $x++) {

$randomProducts = $GLOBALS['SITE_DB']->query("SELECT id,title,thumb_url FROM new_images WHERE cat like $Product ORDER BY RAND() LIMIT 1");
  foreach ($randomProducts as $randomP) {  
     }

echo  '<center><a href="https://nandalow.com/shop/index.php?page=galleries&type=image&id=' . $randomP['id'] . '"><img style="width:100%;" title="FOR SALE: ' . $randomP['title'] . ' is available from The Nandalow Bookshop in Chitose or via mail order. Click for more information & to see other items." src="https://nandalow.com/' . $randomP['thumb_url'] . '"><br>' . $randomP['title'] . '</a></center>';
}
  
USAGE:

on the left upper panel to display "fantasy___science_fiction" book cover:

Code: Select all

<comcode-box param="Nandalow Bookshop"><input class="cms_keep_ui_controlled" readonly="readonly" size="45" title="[block defer=&quot;1&quot; pick=&quot;1&quot;]random-product[/block]" type="button" value="random-product Comcode tag (dbl-click to edit/delete)" /></comcode-box>
In the center panel displaying 5 "comic" cover images:

This is actually in the WIKI template... so edit: templates/WIKI_PAGE_SCREEN.tpl

Place at the end, but before the ending DIV:

Code: Select all

{+START,IF,{$MATCH_KEY_MATCH,_WILD:wiki:id=41}} 
{+START,BOX,Nandalow Bookshop,,300}
{$BLOCK,block=random-product,pick=3,loop=6}
{+END}
{+END}
  • my wiki ID is 41 so it only shows for this wiki.
  • 300 is the width
  • pick=3 to show only comic covers
  • loop=6 to show 6 random comic cover images....
If LOOP is not set it will default to show one image.
if PICK is not set this will display a book cover most likely as default is a book (fantasy___science_fiction) but this code:

Code: Select all

if (mt_rand(1, 150) < 20) { $Product="'comics'"; }
will draw a random number and possibly display a comic picture.

THINGS TO DO:
  • Use an array for gallery PICK options.
  • Be able to select random image from various galleries
  • Prevent same image used twice in a loop or same page?
Discussion: https://compo.sr/forum/topicview/browse ... st_id=5589
Contact me directly: Ironfeatherbooks (@) gmail.com

Image

Post Reply