php - My randomizing code doesn't work off-line -
php - My randomizing code doesn't work off-line -
i'm php noob , made little script other scripts found on net. picks 3 random images folder named "random" , shows them.
while run script online works, when seek run offline on xampp error:
notice: undefined variable: random2 in c:\xampp\htdocs\sito\finaleasd2.php on line 69
it's line images given name variable. think it's due script doesn't folder name right when offline, i'm not sure: what's going wrong? :)
btw script , line 69 while (!$random2 || $random2 == $random1) { (i know, it's mess! :d)
thank help , time! :)
<?php function randomfile($folder='', $extensions='.*'){ // prepare path: $folder = trim($folder); $folder = ($folder == '') ? './' : $folder; // check folder: if (!is_dir($folder)){ die('invalid folder given!'); } // create files array $files = array(); // open directory if ($dir = @opendir($folder)){ // go trough files: while($file = readdir($dir)){ if (!preg_match('/^\.+$/', $file) , preg_match('/\.('.$extensions.')$/', $file)){ // feed array: $files[] = $file; } } // close directory closedir($dir); } else { die('could not open folder "'.$folder.'"'); } if (count($files) == 0){ die('no files found :-('); } // seed random function: mt_srand((double)microtime()*1000000); // random index: $rand = mt_rand(0, count($files)-1); // check again: if (!isset($files[$rand])){ die('array index not found! strange!'); } // homecoming random file: homecoming $folder . "/" . $files[$rand]; } //assegna nomi delle variabili ai file $random1 = randomfile("random"); while (!$random2 || $random2 == $random1) { $random2 = randomfile("random"); } while (!$random3 || $random3 == $random1 || $random3 == $random2) { $random3 = randomfile("random"); } //la parte dedicata alla creazione dei testi alternativi partendo da united nations file di testo $quotesfile = "quotes.txt"; //relative path , filename of file contains quotes. $array = @file("$quotesfile"); // crea united nations array con le citazioni $quote = rand(0, count($array)-1); $titolo = array_rand($array, 3); // la parte sotto crea united nations div con dentro due immagini statiche, lati della panchina, e quattro caricate caso. le immagini hanno // come titoli le variabili estratte casualmente dall' array di nome array preso dal file di testo di prima ?>
if problem undefined variable
, it's easy.
the server settings different on local , remote servers 1 returns error , other doesn't, when inquire !$random2
before exists homecoming error.
so set $random2
false
before while
loop.
the same goes $random3
.
php
Comments
Post a Comment