jcadima
1/1/2018 - 4:52 PM

PHP CSV echo random csv row


https://stackoverflow.com/questions/12118995/how-to-echo-random-line-from-text-file


<?php
// important if line returned are all strings
ini_set('auto_detect_line_endings', true);

// Get POST options
$lastname  = $_POST['lastname'] ;

// CSV file path
$csvFile = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) . '/names/male_test.csv' ;

$fileData=fopen($csvFile,'r');


$lines = [] ;
 
 
if( $lastname == '' )  {
	echo "Please enter a last name";

}
else {
	
	$lines = file($csvFile);
	echo 'SelectedName: ' . $lines[array_rand($lines)];	
}