Display random item from a pool of user-provided data

Permalink Browser Info Environment
How do I go about choosing a random item from the list as advertised?
Display random item from a pool of user-provided data...

Type: Discussion
Status: In Progress
dre
View Replies:
theblockery replied on at Permalink Reply
theblockery
You can use some code like this in your block's view.php file:

<?php    
   // Get repeating items
   $items = $controller->getRepeatingItems();
   // Randomize the items
   shuffle($items);
   // Display image from first item from the randomized array
   $items[0]->image->display();
?>

(This code assumes the handle of your image field is `image`... change the last line of code accordingly though if the field has a different handle.)

Note that if you have block caching enabled in your dashboard, it will cache the randomly-chosen image and keep re-displaying it until the cache is cleared. If this is a problem for you, there is a workaround -- let me know what the block handle you are using is and I can then give you specific instructions on how to do this.

Thanks,
Jordan

concrete5 Environment Information

# concrete5 Version
5.6.3.3

# concrete5 Packages
Cube Testimonials (1.0.2), Designer Content Pro (1.2.2), Designer Content Pro - Custom Block Storage (1.0), Formidable (2.0.11), Google Analytics 2.6.2.2 (2.6.2.2), Highslide Web Albums (3.1), Pro Events (11.0.6), Redirect (1.1), Revolution Slider (2.3.8), Search Block Templates (1.0), Traffic & Statistics (3.11), YouTube Pro (1.3.3).

# concrete5 Overrides
blocks/dcp_text_testimonials, blocks/autonav, blocks/formidable, blocks/search, js/ccm.app.js, languages/da_DK, languages/sv_SE, languages/vi_VN, languages/tr_TR, languages/nb_NO, languages/nl_NL, languages/he_IL, languages/fi_FI, languages/zh_TW, languages/sl_SI, languages/ro_RO, languages/ru_RU, languages/pt_PT, languages/cs_CZ, languages/fa_IR, languages/el_GR, languages/it_IT, languages/es_MX, languages/ar, languages/zh_CN, languages/es_PE, languages/de_DE, languages/fr_FR, languages/sk_SK, languages/es_ES, languages/es_AR, languages/ja_JP, themes/blc

# concrete5 Cache Settings
Block Cache - On
Overrides Cache - On
Full Page Caching - Off

# Server Software
Apache/2.2.24 (Unix) mod_hive/5.0 mod_ssl/2.2.24 OpenSSL/1.0.0-fips mod_auth_passthrough/2.1 mod_fastcgi/2.4.6 mod_bwlimited/1.4 mod_fcgid/2.3.6

# Server API
cgi-fcgi

# PHP Version
5.4.45

# PHP Extensions
bcmath, bz2, bz2_filter, calendar, cgi-fcgi, Core, ctype, curl, date, dba, dom, enchant, ereg, exif, fileinfo, filter, ftp, gd, gettext, hash, http, iconv, imap, intl, ionCube Loader, json, ldap, libxml, mailparse, mbstring, mcrypt, memcache, mhash, mysql, mysqli, openssl, pcntl, pcre, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, pspell, readline, Reflection, session, shmop, SimpleXML, soap, sockets, SPL, sqlite, sqlite3, ssh2, standard, sysvmsg, sysvsem, tidy, tokenizer, wddx, xml, xmlreader, xmlrpc, xmlwriter, xsl, Zend Guard Loader, zip, zlib.

# PHP Settings
max_execution_time - 45
log_errors_max_len - 1024
max_file_uploads - 20
max_input_nesting_level - 64
max_input_time - 60
max_input_vars - 2000
memory_limit - 128M
post_max_size - 32M
sql.safe_mode - Off
upload_max_filesize - 24M
http.persistent.handles.limit - -1
ldap.max_links - Unlimited
memcache.max_failover_attempts - 20
mysql.max_links - 60
mysql.max_persistent - Unlimited
mysqli.max_links - 60
mysqli.max_persistent - Unlimited
pcre.backtrack_limit - 1000000
pcre.recursion_limit - 100000
pgsql.max_links - 60
pgsql.max_persistent - Unlimited
session.cache_limiter - nocache
session.gc_maxlifetime - 7200
soap.wsdl_cache_limit - 5

Browser User-Agent String

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You may not request a refund that is not currently owned by you.