PHP Random Array Content From Flat Text File Database Tutorial

PHP Random Array Content From Flat Text File Database Tutorial

Listening Video

In this exercise we put several built in PHP functions to use in order to turn an idea into an application. We take the described logic and translate it into script by accessing knowledge we have of PHP's built in functions and applying them. Some people have to use text files in place of MySQL for data management so this lesson is another one that deals with text file data handling for dynamic purposes on a website.
Lesson Code
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<html>
<body>
<h2><?php echo $randPhrase; ?></h2>
</body>
</html>

<!-- Below is the content inside of 'flatFileDB.txt' for referencing -->
<!--
Hello and Welcome!
Hi how are you today?
Welcome Pilgrims!
Hey There Weenis!
Howdy Partner!
Hola Amigos!
Hallo Vrienden!
Bonjour Les Amis!
Cairde Dia duit!
Cras Venenatis! 
-->

0 Response to "PHP Random Array Content From Flat Text File Database Tutorial"

Post a Comment