// sample PHP to randomly output one of five quotes // set up main quotations array $quotations = array(); // set up first quote with second dimensions on the array $quotations[0] = array(); $quotations[0]["text"] = "Thinking about design is hard, but not thinking about it can be disastrous."; $quotations[0]["attribute"] = "Ralph Caplan"; $quotations[0]["backgroundColor"] = "#0000aa"; $quotations[0]["textColor"] = "white"; // set up next quote with second dimensions on the array $quotations[1] = array(); $quotations[1]["text"] = "Design is intelligence made visible."; $quotations[1]["attribute"] = "Alina Wheeler"; $quotations[1]["backgroundColor"] = "#aa0000"; $quotations[1]["textColor"] = "white"; // set up next quote with second dimensions on the array $quotations[2] = array(); $quotations[2]["text"] = "Design is the intermediary between information and understanding."; $quotations[2]["attribute"] = "Hans Hofmann"; $quotations[2]["backgroundColor"] = "#88cc88"; $quotations[2]["textColor"] = "#003300"; // set up next quote with second dimensions on the array $quotations[3] = array(); $quotations[3]["text"] = "The value of art is in the observer. When you find out what you like, you’re really finding out about yourself."; $quotations[3]["attribute"] = "Agnes Martin"; $quotations[3]["backgroundColor"] = "#bbb"; $quotations[3]["textColor"] = "#555"; // set up next quote with second dimensions on the array $quotations[4] = array(); $quotations[4]["text"] = "It's through mistakes that you actually can grow. You have to get bad in order to get good."; $quotations[4]["attribute"] = "Paula Scher"; $quotations[4]["backgroundColor"] = "#aa00aa"; $quotations[4]["textColor"] = "#eee"; // look for a number passed in the URL and, if present, convert it to an integer value // NOTE: code updated from demo video to address error handling differences among servers if ( isset( $_GET["quoteNumber"] ) ) $passedNumber = intval( $_GET["quoteNumber"] ); // try to convert submitted value to integer else $passedNumber = null; // no number passed // if the passed number is 0 or greater, but less than the total count of the $quotations array, use the passed number // otherwise select a random number if ( ($passedNumber != null) && ($passedNumber >= 0) && ($passedNumber < count($quotations) ) ) { $quoteNumber = $passedNumber; // used passed number } else { $quoteNumber = rand(0, ( count($quotations) - 1) ); // random number from 0 to one less than the array length } /* now let's output the page's HTML! note that PHP code is being echo'd in all of these: -
— = $quotations[$quoteNumber]["attribute"] ?>
(get new quote)