2016-03-18 8 views
4

私は多少PHPに慣れていますが、ここで私を導いてくれたことを除いて、私が必要とするほとんど全てを作成しました。私は友人のシンプルなサイトを作成していますので、家賃の支払い状況、新しいテナントの入力、現在の残高の確認ができます。私は完璧に働くすべてを持っていた...私は思った。バランスをチェックするときに、テキストファイルを読み込んで適切な情報を出力するために、1つの単語入力だけでテストしました。しかし、私が見つけたのは、最初と最後の名前がその間にスペースを入れて保存されていれば、私はマッチを得ていないということです。入力されたテナント名の全体をスペースで読み込むコードが必要です。私は本当に理解できるものは何も見つけておらず、いくつかの夜を探していました。結果を検索して取得するための完全なコードは次のとおりです。PHPの空白を含むテキストファイルの2〜3語の一致方法

検索されているテキストファイルは、このようなものです:

John Doe,123 Main St,400.00,01/01/2016, 
Jane Doe,124 Main St,300.00,01/01/2016, 
John Doe,123 Main St,,01/03/2016,200.00 
<?php 
$lines = file('data.txt'); //here's the filename 
?> 
<center> 
    <table id="historytable" width="640" border="1"> 
    <caption>Billing & Payment History 
</caption> 
<tbody> 
<tr> 
<td width="40">Tenant</td> 
<td width="40">Address</td> 
<td width="40">Date</td> 
<td width="40">Rent</td> 
<td width="40">Payment</td> 
</tr> 
<?php 
$search = $_POST["name"]; //assigning a string to each piece of input data 
// Store true when the text is found 
$balance = 0; //assign initial value to balance 
$renttotal = 0; //assign initial value to renttotals 
$received = 0; //assign initial value to received 
$found = false; 
foreach ($lines as $line) { //this is the loop to read the txt file 
{ 
    if(strpos($line, $search) !== false) 
    { 
    $found = true; 
list($a,$b,$c,$d,$e) = explode(',', $line); //this assigns a variable name to each data item separated by a comma 
    $renttotal+= $c; //each time it loops, it gathers the value of c adding it to itself same for the two lines below 
    $received+= $e; 
    $balance = $renttotal - $received; //subtracts the final value of renttotal and received assigning the difference to balance 

    $line_array = explode(",", $line); //breaks each piece of data apart to be placed in a table } 
    echo "<tr> 
<td width=40>$a</td> 
<td width=40>$b</td> 
<td width=40>$c</td> 
<td width=40>$d</td> 
<td width=40>$e</td> 
</tr>"; 
}}} 
?> 
</tbody> 
</table> 
</center> 
<BR /> 
<BR /> 
<center> 
    <p>TOTAL RENTS CHARGED $<?php echo "$renttotal"?><br /> 
    TOTAL PAYMENTS RECEIVED $<?php echo "$received"?><br /> 
    BALANCE DUE $<?php echo "$balance"?></p> 

は、任意の助けを事前にありがとうございます。

$search = urldecode($_POST["name"]); 

そして、あなたが後で問題ないはずです。

+0

正直言って、私はあなたの問題を理解していないようです。どの文字列を比較する必要がありますか?さらに、変数 'a'、' b'などの名前を変える代わりに、 '$ name'、' $ address'、 '$ amount'などのようにわかりやすい名前を与えようとします。あなたのコードは3ヶ月でも同様です。 – Jan

+0

お返事ありがとうございます。変数は、コーディングを完全に調整する前に、すべてが正しく機能するようにするだけでした。比較する限り、ユーザーは$ search = $ _POST ["name"]に行くテナントのフルネームのフォームを送信します。名。この名前(現在の$ search)は、data.txtファイル内のすべてのインスタンスが検索されます。完全一致が成立した場合は、テーブルに氏名、住所、賃貸料、日付と支払いが返されます。私はこれを正しく説明したいと思う。私はここで何かが間違っているかもしれないと思う - if(strpos($ line、$ search)!== false)。再度、感謝します。 – SharonF

+0

"Jane"が$ searchの入力であった場合にのみコーディングが正しく一致しますが、 "Jane Doe"の場合は一致しません。 – SharonF

答えて

0

は単純にそうようなurldecode()を行います。

+0

ありがとうございます。私は完璧に動作します! – SharonF

関連する問題