Jump to content

Recommended Posts

Hi,

I have a Comma Delimited File called "houses.txt" with the contents of:

 

HA11QS, 200, house1.jpg, 4

HA22BR, 280, house2.jpg, 10

HA33AB, 390, house3.jpg, 3

HA44CD, 320, house4.jpg, 8

 

I have a web form "form2.html":

<html>
<head>

<title>Untitled Document</title>
</head>

<body>
<form action="any.php" method="post">

Please Enter Anything
<input type="text" name="any">

<input type="submit" value="Submit">
</form>
</body>
</html>

 

and PHP code of "any.php":

 

<?php

if (isset($_POST['any']))
{
$filename = "houses.txt";
$fileOpen = fopen($filename, "r");

$max = $_POST['any'];

$rowsArr  = file ($filename);
foreach ($rowsArr as $row)
	{
		$lineDetails = $row;
		$item_array = explode (",", $row);

		if (in_array($max,$item_array))
		{
			echo("Post Code - " . $item_array[0]. "<br>");
			echo("Price - " . $item_array[1]. ",000 <br>");
			echo("Picture - " . $item_array[2]. "<br>");
			echo("Number of Visits - " . $item_array[3]. "<br>");
			echo("<br>");
		}


	}
	fclose($fileOpen);
}
?>

 

What i need is for the user to input anything they wish for example:

4, which would search the array and find that the first house has had 4 visits or

HA44CD, to find the last house on the list etc ... however unfortunatley its not working for, if anyone can help me i would grateful

 

Thank You

 

 

 

however unfortunatley its not working for, if anyone can help me i would grateful

 

1. Please see below.

 

2. What you want to do is incredibly vague. What if someone enters 44? Do they want the house number, or the number of visits?

Lol, sorry about my bad english i was rushing and wasn't paying attention

 

Ok what exactly i am looking for is:

If the user enters: 4, this information is sent back to the array where it searches it, and the shows all the houses which have the number 4, in this case, e.g.

HA11QS, 200, house1.jpg, 4 since this house has had 4 visits,

 

If the user had entered, 320 then:

HA44CD, 320, house4.jpg, 8

 

if what the user enters is not in the array then it will return a message saying "Not found", however i havent added this yet, but can do

 

Hope this helps, Thanks

try

<?php
$test = array('HA11QS, 200, house1.jpg, 4',
'HA22BR, 280, house2.jpg, 10',
'HA33AB, 390, house3.jpg, 3',
'HA44CD, 320, house4.jpg, ');
$max = 4;
$nf = 'Not found';
foreach($test as $row){
    $row = trim($row);
    if(preg_match('/(^|, )'.$max.'($|,)/i', $row)){
        $item_array = explode (",", $row);
        echo("Post Code - " . $item_array[0]. "<br>");
echo("Price - " . $item_array[1]. ",000 <br>");
echo("Picture - " . $item_array[2]. "<br>");
echo("Number of Visits - " . $item_array[3]. "<br>");
echo("<br>");
              $nf = '';
    }
}
echo $nf;
?>

i use test array for your text file

Hi i tried using that code but i get really confused in understandng it

with the

(preg_match('/(^|, )'.$max.'($|,)/i', $row)

 

It didnt work for me unfortuantely, and also i need for it to be read from the file

 

Thanks for your help its much appreciated :)

you look for some value $max

befor this value is start of line (^ in patern) or comma and space (, ) in paterrn

after values is end of line ($) or comma (,)

 

php function file() load file in array

i'm, for testing, define array directly and setup value of $max manualy

 

if you want to use your code you must trim row before explode it and separator in your file is NOT ',' it's ', ' (comma and space)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.