Cannot get data to display from CSV File?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wobuck
Forum Newbie
Posts: 2
Joined: Mon Sep 15, 2008 3:18 pm

Cannot get data to display from CSV File?

Post by wobuck »

I am very new to php and suspect I have made a coding error.

1) I created an HTML form:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Serial Number</title>
</head>

<body>

<form method="GET" action="prop2.php">

<!--&beds=-->
Serial<br /><br />
<select class="formboxsize" size="1" name="serial">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>

</select><br /><br />

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

</body>

</html>

2) Then I created a PHP file;

<?php
$file = "xfile.csv";

$serial = $_POST['serial'];


if ( !empty ( $_GET['serial'] ) ) {

$serial = $_GET['serial'];

$file_content = file_get_contents ( $file );
$file_arr = preg_split ( '/\n/' , $file_content );
$found = false;

foreach ( $file_arr as $item ) {
$data = explode ( ';' , $item );
if ( $data[0] === $serial ) {
$found = true;
$date = $data[1];

}
}
if ( $found ) {
print "Date: " . $date;
}
else {
print "Not found";
}
}
?>

3) The xfile.csv file contains:

serial date Warranty
1 01/01/2008 01/01/2009
2 24/02/2008 23/01/2009
3 10/10/2008 09/09/2009

4) When I run the form it returns "Not found"

If I change the php file command $found = false; to true it displays the word Date: as expected, but no data from the file?

:(

Any help would be greatly appreciated?

Wobuck

:)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Cannot get data to display from CSV File?

Post by Luke »

you know, php has csv support...

fgetcsv, fputcsv

And then there's this little library I built :)

http://code.google.com/p/php-csv-utils/downloads/list

/shameless plug
wobuck
Forum Newbie
Posts: 2
Joined: Mon Sep 15, 2008 3:18 pm

Re: Cannot get data to display from CSV File?

Post by wobuck »

Apologies for delay in response - yes you are correct problem now soved.

Many thanks!
Post Reply