Jump to content

PHP If Statement using a comma delimted file


ibz786

Recommended Posts

Hi everyone,

 

Basically i have a little task to do and was wondering if anyone is able to help, i would very grateful

 

First task is for me to read a comma delimted file into PHP and store everything into an Array:

 

Comma Delimited File = "houses.txt" (As a reference it means, PostCode, Price ('000's), picture and houses visits)

HA11QS, 200, house1.jpg, 4

HA22BR, 280, house2.jpg, 10

HA33AB, 390, house3.jpg, 3

HA44CD, 320, house4.jpg, 8

 

my basic php for now is:

<?php

function getHouse ()
{
$filename = "houses.txt";
$fileRead   = fopen($filename, "r");

$rowsArr  = file ($filename);
for ($i=0; $i < count($rowsArr); $i++)
	{
		$lineDetails = $rowsArr[$i];
		print  $lineDetails . "<br>" ;
	}

	fclose($fileRead);
}
?>

<h3> Welcome </h3>

<p> PostCode Number Picture Visits </p>

<?php

echo getHouse();

?>

 

What i need to do next is to have a static web form where a user can input a maximum price e.g. 300

And that will return all the houses which are under 300 by reading the whole array

 

I have a basic form setup:

 

<form method="post" action="maxtest.php" >
<input type="text" name="userprice" value="" />

<input type="submit" name="button" value="Submit" />
</form>

 

and also i have a an idea that i need something along the lines of:

$userValue = $_POST['userprice'];

	if ($userValue == 300)

{
}

 

Im a bit stuck on how to incorporate the comma delimted array

 

Please help me, thank you

Link to comment
Share on other sites

Hi, ill have a look now.

But just saying that my php code for reading the comma delimited file works fine

It just ... using an if statement while reading the whole array

 

My apologies if i caused any confusion

 

However i am still grateful and thank you

Link to comment
Share on other sites

A rough start - un-proof-read, untested; however, should work...

 

<?PHP
/* PRESUMES the text file lines end with newline character, and that the images are in the same folder as the script */
$file = "houses.txt";

/* put all the lines into an array */
$houses = file($file);

/* test that it is infact an array */
echo "<PRE>";
print_r($houses);
echo "</pre>";

/* max price */
$max = 300;

/* count the number of houses in the array */
$num_houses = count($houses);

/* loop thru the array and split each house into its own array */
for($i=0;$i<$num_houses;$i++) {
$elements = explode(",", $houses[$i]);

/* check if price is <= $max */
if(((int) $elements[1]) <= $max) {
	/* echo out the info for each house */
	?>
	Post Code - <?PHP echo $elements[0]; ?><br/>
	Price - $<?PHP echo $elements[1]; ?>,000.00<br/>
	Visits - <?PHP echo $elements[3]; ?><br/>
	Picture - <img src="<?PHP echo $elements[2]; ?>" alt=""><br/>
	<hr>
}
<?PHP
}
?>



Link to comment
Share on other sites

Hmm i tried just using that current example and not even using my code, however it didn't work :(

 

The thing is that what i need is:

HA11QS, 200, house1.jpg, 4

HA22BR, 280, house2.jpg, 10

HA33AB, 390, house3.jpg, 3

HA44CD, 320, house4.jpg, 8 in an array which i have done in the code provided in the first post

 

However, when users are using a static web form:

<form method="post" action="maxtest.php" >
<input type="text" name="userprice" value="" />

<input type="submit" name="button" value="Submit" />
</form>

 

They may type in e.g. 300 and the results:

HA11QS, 200, house1.jpg, 4

HA22BR, 280, house2.jpg, 10

 

are returned to them :S

 

 

Link to comment
Share on other sites

Hmmm - working example - http://www.nstoia.com/houses.html

(BTW it may be better to CONTROL their price input - use a select list of valid choices)

 

the codes:

houses.html

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<form action="houses.php" method="post">
Select a maximum price: <select name="price">
	<option value="400">$400,000.00</option><br/>
<option value="300">$300,000.00</option><br/>
<option value="200">$200,000.00</option><br/>
<option value="100">$100,000.00</option><br/>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>

 

houses.php

<?PHP
if(isset($_POST['price'])) {
$max = $_POST['price'];
$file = "houses.txt";
$houses = file($file);
$num_houses = count($houses);
for($i=0;$i<$num_houses;$i++) {	
	$elements = explode(",", $houses[$i]);
	if(((int) $elements[1]) <= $max) {
		?>
		Post Code - <?PHP echo $elements[0]; ?><br/>
		Price - $<?PHP echo $elements[1]; ?>,000.00<br/>
		Visits - <?PHP echo $elements[3]; ?><br/>
		Picture - <img src="<?PHP echo $elements[2]; ?>" alt=""><br/><hr>
		<?PHP
	}	
}
?><a href="houses.html">Try Again</a><?PHP
}else{
header("houses.html");
exit();

}
?>

 

houses.txt

HA11QS, 200, house1.jpg, 4
HA22BR, 280, house2.jpg, 10
HA33AB, 390, house3.jpg, 3
HA44CD, 320, house4.jpg, 8

Link to comment
Share on other sites

try this

 

function getHouse ()

{

  $filename = "houses.txt";

  $fileRead  = fopen($filename, "r");

 

  $rowsArr  = file ($filename);

  foreach($rowsArr as $row)

      {

        $lineDetails = $row;

        $item_array = explode(', ',$row) // to turn the csv into an array

      }

     

      fclose($fileRead);

}

?>

once in $item_array the values are stored as follows.

$item_array[0]  is the postcode

$item_array[1] is the price

$item_array[2] is the visits

$item_array[3] is the pictures

Link to comment
Share on other sites

Thanks litebearer and also cgeisler515 for your help thus far all your efforts are very appreciated

 

litebearer, your code works thank you, just a one thing, why does it matter when php is:

<?PHP and <?php

I tried the code with <?php and i end up with errors however with <?PHP everything is fine

 

Also cgeisler515, thanks for that, just how would i use an if statement in there?

 

Thanks again

 

Link to comment
Share on other sites

Very simple much in the same way above answer.

 

just a bit easier to read

 

function getHouse ()

{

$filename = "houses.txt";

$fileRead  = fopen($filename, "r");

 

$rowsArr  = file ($filename);

foreach($rowsArr as $row)

{

$lineDetails = $row;

$item_array = explode(', ',$row); // to turn the csv into an array

 

if(((int) $item_data[1]) <= $GET['price'])

{

echo("Post Code -  " . $item_data[0] . "<br/>\n");

echo("Price - $" . $item_data[1] . ",000.00<br/>\n");

echo("Visits - " . $item_data[3] . "<br/>\n");

echo("Picture - <img src=\"" . $item_data[2] . "\" alt=\"\"><br/><hr>\n");

}

}

   

    fclose($fileRead);

}

Link to comment
Share on other sites

  • 2 weeks later...

Hi, sorry about not replying earlier i have been busy so i wasnt able to finish of my code until now. I would like to thank you both, you have been extremely helpful, without your help i would have struggled

 

Thank You

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.