Page 1 of 1

get an info from a text file and display it

Posted: Sat Jan 17, 2015 3:25 pm
by summer_roberts
hi there, i'm new to php so please bear with me :?

i'm trying to get an info from a text file and display it

[info.txt]
miko,male,japan
kate,female,england
akira,female,japan

i want to display the names only depending on which info i choose in a form

if i choose male and japan, it will display miko
if i choose female and england, it will display kate
if i choose female and japan, it will display akira

here's my code:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method=post>
Gender: <input type="text" name="gender">
Location: <input type="text" name="location">
<input type="submit" name="submit" value="SUBMIT">
</form>

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$gender = $_POST['gender'];
	$location = $_POST['location'];
	$selected = $gender . ',' . $location;

	$lists = file('info.txt');
	$search = $selected;
	$found=false;
	foreach ($lists as $list)
	{
		if(strpos($list, $search) !== false)
		{
			$found=true;
			echo 'name'; //this is where i don't know how to insert a name from the info.txt
		}
	}
		if(!$found)
		{
			echo "No match";
		}
}
?>
Thanks a lot :)

Re: get an info from a text file and display it

Posted: Sat Jan 17, 2015 4:07 pm
by Celauran
What if you explode each line into its own array?

[RESOLVED] get an info from a text file and display it

Posted: Sun Jan 18, 2015 9:03 am
by summer_roberts
Hi Celauran,

I did what you suggested and it worked. Thanks! :D

here's my code:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method=post>
Gender: <input type="text" name="gender">
Location: <input type="text" name="location">
<input type="submit" name="submit" value="SUBMIT">
</form>

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$search_gender = $_POST['gen'];
	$search_location = $_POST['loc'];
	$sorry = true;
	$lists = file('info.txt');
	$number_of_Lists = count($lists);
	for ($index=0; $index<$number_of_Lists; $index++)
	{
		list ($name, $gen, $loc) = explode (',',$lists[$index]);
		$loc = trim($loc);
		$found = ($gen == $search_gender) && ($loc == $search_location);
		if ($found)
		{
			echo 'Found ', $name;
			$sorry = false;
		}
	}
	if ($sorry)
	{
		echo "No result";
	}
}
?>