...

View Full Version : PHP reading a file



biggy157
12-08-2010, 10:40 PM
I have a code that needs to read in a file and has a parameter file. I need to know how to put in <=,<,=,>=,> operators that will be in my parameter file.

Any help would be great


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php


// check to see if file uploaded, using the super-global array $_FILES
if (isset($_FILES['UploadedFile']['tmp_name']))
{
# $_FILES['fieldname']['name'] gives the uploaded file name
print ("<p>");
$filename = dirname(__FILE__).'/'.$_FILES['UploadedFile']['name'];
move_uploaded_file($_FILES['UploadedFile']['tmp_name'], $filename);
$OUTFILE = fopen($filename, 'r') or die("Can't open $filename for read");

//read the entire file into an array
$params=file('parameters.txt');

//after this $type will have "SPORT"
$search=explode(':',strtoupper($params[0]));
$searchColumn=trim($search[0]);
$searchValue=trim($search[1]);

//after this $columns[0] will have "MODEL" && $columns[1] will have "PRICE"
$columns=explode("~",strtoupper(trim($params[1])));

//read the first line of the csv
$data = fgetcsv($OUTFILE,1000, "~");

//now find which column corresponds to "TYPE"
$typeIndex=array_search($searchColumn, $data);

//now find out the column index for each of the columns specified in parameters.txt
foreach($columns as $i=>$v)
{
if(FALSE===($columns[$i]=array_search($v, $data)) )
{
echo ("Column $v does not seem to exist");
unset($columns[$i]);
}
}

echo('<table>');
do{


//make sure that the type value of the current $data row matches what
//was provided in $type (in parameters.txt)
if( strtoupper($data[$typeIndex]) == strtoupper($searchValue) )
{
echo('<tr>');
//now iterate only over the set of requested columns
foreach($columns as $dataIndex){
echo("\t<td>{$data[$dataIndex]}</td>\r\n");
}
echo('</tr>');
}

}while (($data = fgetcsv($OUTFILE,1000, "~")) !== FALSE);
echo('</table>');
fclose($OUTFILE);
}
?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<!-- <input type="hidden" name="MAX_FILE_SIZE" value=" " /> -->
<input name="UploadedFile" type="file" />
<input type="submit" value="UPLOAD" />

<h2> Soucre Code File</h2>
<p>
<?php
show_source(__FILE__);
?>
</p>

</body>
</html>


the file i will be reading in



MAKE~MODEL~TYPE~PRICE
Toyota~Camry~Sedan~18000
Toyota~Tacoma~Truck~19000
Ford~Mustang~Sport~21000
Chevrolet~Corvette~Sport~48000
Ford~F150~Truck~25000
Toyota~Highlander~SUV~35000

My Parameter file



Price<3500
MAKE~MODEL~PRICE

mlseim
12-09-2010, 12:31 AM
So, let me get this into my head.

You upload a file with a list of vehicles and price ... like this:
Toyota~Camry~Sedan~18000
Toyota~Tacoma~Truck~19000

And you have another file that tells what to search for ... like this:
Price<3500
Toyota~Tacoma~Truck

So the result would be all "Toyota~Tacoma~Truck" that are less than $3500?

==============

Why is there a "parameter.txt" file, and not a drop-down select (like a search)?

And can you use "wildcards", like:
Price<3500
Toyota~*~Truck

So it would display ALL Toyota Trucks less than $3500?

==========

I'm not sure of the purpose of your script.
Wondering if you're going about it in a different method.




.

idalatob
12-09-2010, 01:48 PM
Sometimes people over-complicate things.

Re-Evaluate your whole process, im sure you can come up with a simpler solution.

seco
12-09-2010, 02:17 PM
my chained select box generator would be perfect for you. check it out

http://www.blueicestudios.com/chained-select-boxes-using-php-mysql-ajax/

little video clip showing how it works.

DataTalk
12-09-2010, 03:51 PM
I like the generator but why don't you use a database instead of the text file
I can help you with that, I would like to see the finished product being
populated by the database

PS: Here are the most used escape codes on PHP:



\t tab echo ("separated\tby\ttabs");
\r return echo ("first line\r\nsecond line");
\n new line echo ("first line\r\nsecond line");
\\ backslash echo ("Backslash: \\");
\$ dollar sign echo ("Total Amount: \$210.00");
\" quotes echo ("\"yes\" - he replied");
\[ left bracket
\] right bracket
\{ left brace
\} right brace echo ("\[ \] and \{ \} are used");

seco
12-10-2010, 02:28 PM
I like the generator but why don't you use a database instead of the text file
I can help you with that, I would like to see the finished product being
populated by the database

it does use a database, its php, mysql and jquery.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum