Web Analytics Made Easy -
StatCounter Random/Rotating educational content (not slider) needed on front page of site - CodingForum

Announcement

Collapse
No announcement yet.

Random/Rotating educational content (not slider) needed on front page of site

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random/Rotating educational content (not slider) needed on front page of site

    I need a small news section on the front page of my site to include rotating/random educational blurbs. Each blurb will contain both an image of a featured educational aid as well as a text blurb next to it. There are 6 different blurbs total and we'd like to feature one each time a visitor appears (ie, not a slider that continually changes while the user is on the page but rather, each time the page reloads, one of the six blurbs randomly appears).

    First off, is there any way to do this easily HTML/CSS? Or any other options? My skillset is limited to HTML and CSS unless the instructions are idiot-proof.

    Second, if I end up having to use javascript, 1) any good tutorials, and 2) how can I accommodate people who do not have javascript (ie, can I leave a static image/text blurb for those people?)

    Many thanks!

  • #2
    Use php if its installed on your server, if so:

    Create your blurb files as your normal html without the header etc. (just the part you would call the blurb), but, save it using the extension php, e.g. "blurb-01.php"

    Then you can use the following script

    PHP Code:
    <?php
    //Save this file as 'blurb.php'

    //Declare array of files to choose from or in other words - create a list of files
    $blurbs=[
    'blurb-01.php',
    'blurb-02.php',
    'blurb-03.php',
    'blurb-04.php',
    'blurb-05.php',
    'blurb-06.php'
    ];//Note the comma at the end of each file-name except the last one, if you include more files to this list then you must put the comma at the end of the preceding / penultimate file-name

    //select a random file from the array
    include $blurbs[rand(0count($blurbs))];
    ?>
    Put the following line in your html code where you want the blurb information to appear.
    PHP Code:
    <?php include 'blurb.php' ?>
    example blurb file:
    PHP Code:
    <div><!--blurb-->
    <
    img src='blurb.jpg'>
    <
    p>This is a blurbone of six</p>
    </
    div
    The restrictions on this are that you must have all of these files in the same directory, but you can change this quite simply with your own knowledge of html; it should be obvious where to change the file names to suit.

    Note: Your server will loook for files from the root folder of your server, whereas html pages look to the root of the web site.
    e.g.
    PHP Code:
    #in a html file
    <img src='/blurb.php'>

    #is not the same location as

    include '/blurb.php'
    #in a php interpreted section 
    You said you wanted easy to understand so I hope to have included enough information to get you going, the above will work and should be easy to understand, maintain and do what you have in mind and can also be expanded.

    Comment


    • #3
      Thank you so much, wha ...

      The script works like a charm for much of the time but every few page reloads I get the following error:

      Warning: include(): Filename cannot be empty in /home/french/www/www/blurb.php on line 15
      Warning: include(): Filename cannot be empty in /home/french/www/www/blurb.php on line 15
      Warning: include(): Failed opening '' for inclusion (include_path='.:/usr/local/php56/lib/php') in /home/french/www/www/blurb.php on line 15
      Here is the text I am using in blurb.php:

      Code:
      <?php 
      //Save this file as 'blurb.php' 
      
      //Declare array of files to choose from or in other words - create a list of files 
      $blurbs=[ 
      '/home/french/www/www/blurb-01.php', 
      '/home/french/www/www/blurb-02.php', 
      '/home/french/www/www/blurb-03.php',
      '/home/french/www/www/blurb-04.php', 
      '/home/french/www/www/blurb-05.php',
      '/home/french/www/www/blurb-06.php' 
      ];//Note the comma at the end of each file-name except the last one, if you include more files to this list then you must put the comma at the end of the preceding / penultimate file-name 
      
      //select a random file from the array 
      include $blurbs[rand(0, count($blurbs))]; 
      ?>
      Any idea what I am doing wrong?

      PS The test page address is frenchteachers.org/defaulttest.php

      Comment


      • #4
        You get the warning when it tries to access the entry at $blurbs[count($blurbs)] which is the next position after the end of the array. You need to subtract 1 from the count to stay in the array (as 0 is the first position)
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment


        • #5
          Originally posted by felgall View Post
          You get the warning when it tries to access the entry at $blurbs[count($blurbs)] which is the next position after the end of the array. You need to subtract 1 from the count to stay in the array (as 0 is the first position)
          Thanks Stephen .. my knowledge of php is slim to none so I have a dim understanding of what you mean but don't know how to fix it. How do I subtract 1 from 0 ... and how would my code read on that? Do I put -1 in place of the 0?

          PS I just tried that ... still get errors.

          Many thanks,
          Linda
          Last edited by lindagal2; Sep 25, 2016, 11:51 PM.

          Comment


          • #6
            Ahhhh ... just googled "php, array, random content" and found a script similar ... the -1 goes between the two ending parentheses. Aha!

            Many thanks.

            Comment

            Working...
            X