PDA

View Full Version : Reading specific values from a txt file


Len Whistler
06-25-2007, 02:15 AM
I would like to read from a .txt file with php and display only column[3] and column[4] when there is a match in column[0] at a specific row.

Example:

currencies.txt
CAD / Canadian Dollar (CAD) / Canadian Dollars / .22500 / .03680
USD / US Dollar (USD) / US Dollars / .5400 / .5200
AUD / Australian Dollar (AUD) / Australian Dollars / .1324 / .09680
MXN / Mexican Peso (MXN) / Mexican Pesos / .3476 / .2482

There is a match in column[0] at row 3 with AUD, so only the values in column[3] and column[4] at row 3 are displayed (.1324 and .09680). I'm not using MySQL for the database because the txt file will only be about 10 rows, I can't figure this one out, any solutions? Thanks.

rfresh
06-25-2007, 03:58 AM
I would look at PHP's explode() function then use a for loop to check and then display the elements you want.

matak
06-25-2007, 04:24 AM
Maybe something like this?

<?php

$tekst = file_get_contents ('./text.txt');

$array = explode ("\n", $tekst);

$i = 0;

while ($i < count($array)){
$row1split = explode ('/', $array[$i]);
print "Currency: ".$row1split[0]." value1= ".$row1split[3]." value2= ".$row1split[4]."<br>";
$i++;
}
?>

Len Whistler
06-25-2007, 06:33 AM
Ok...thanks for the replies. Below is the code I have that will read all rows and display only column[3] and column[4], but I can't figure out the part to only display the values of columns 3 and 4 from the row that the $_POST['symbol'] variable matches in column[0].

<?php
$userfile= file_get_contents("./currencies.txt");
$users = explode("\n",$userfile);

foreach ($users as $user) {
list($symbol, $currency_name, $currency_plural,
$currency_buy, $currency_sell) = explode(" / ", $user);
echo "$currency_buy $currency_sell<br />";
}
?>

Below is the bit of code that I think will do what I want but can't figure out the exact syntax or if I'm even on the right track. If the user posts AUD then only the values $currency_buy and $currency_sell from row 2 are echoed.


if ($_POST['symbol']; == column[0]); {

echo "$currency_buy $currency_sell";

}

matak
06-25-2007, 07:47 AM
Can you write a way which you are trying to reach those rows.

It can be different weather you try to get values based on the number of row, or based on the value of POST input...

eg

GET[row1]

or is it

POST[CAD] ?

I see that you wrote POST[symbol] but i don't understand why...

To explain:
If you already have a text file, why don't you just echo something like href=?value=cad (eg. drop down box), and all your problems are solved. But if you REALLY need it to be $_POST, then OK :).

Mwnciau
06-25-2007, 08:28 AM
if ($_POST['symbol']; == column[0]); {

should be

if ($_POST['symbol'] == column[0]) {

matak
06-25-2007, 08:46 AM
How 'bout this?

<?php
$userfile= file_get_contents("./text.txt");

$users = explode("\n",$userfile);

#$value = $_POST['symbol'];
$value = "CAD";

foreach ($users as $user) {
list($symbol, $currency_name, $currency_plural, $currency_buy, $currency_sell) = explode(" / ", $user);

if ($value == $symbol){
echo "$currency_buy $currency_sell<br />";
}
}

?>

Len Whistler
06-25-2007, 04:39 PM
Ok thanks for the replies. I finally got the proper syntax and it's working