4

I am somewhat new to PHP but have created almost everything I need, except one thing which has led me here. I am creating a simple (for you probably but not for me) site for a friend so they can keep track of when they receive rent payments, input new tenants, and check current balances. I had everything working perfect...I thought. When checking the balance, I tested with only one word input to read the text file and output the proper info. But, what I found is if the first and last name are stored with a space between then, I am not getting a match. I need code to read the inputted tenant name in its entirety with the space between. I haven't found anything I could really comprehend and have been searching for several nights. Here is my full code for searching and retrieving results.

The text file being searched is like this:

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>

Thank you in advance for any help.

John Weisz
  • 30,137
  • 13
  • 89
  • 132
SharonF
  • 43
  • 3
  • To be honest, I do not seem to understand your problem. Which strings need to be compared? Additionally, instead of naming your variables `a`, `b` and so forth try to give them more understandable names like `$name`, `$address`, `$amount`, etc. Thus, you're likely to understand your code in three months as well. – Jan Mar 18 '16 at 23:19
  • Thanks for responding. The variables were merely to get everything working right before I adjust the coding perfectly. As far as comparing - the user submits a form with the tenant's full name going to $search = $_POST["name"]; name. That name - now $search - is searched for all instances in the data.txt file. When an exact match is made, it should return the full name, address, rent, date and payment in the table. I hope I'm explaining this correctly. I think I may have something wrong here - if(strpos($line, $search) !== false). Thanks again. – SharonF Mar 18 '16 at 23:39
  • The coding will only match properly if "Jane" was the input to $search, but if it's "Jane Doe", it won't match up. – SharonF Mar 18 '16 at 23:43
  • Your strings might be urlencoded, that is whitespaces are replaced by `+` - can you provide a sample of both the variables? That is, `echo $search;` and `echo $line;` and provide these in your question? – Jan Mar 18 '16 at 23:46
  • You're right. Here is a result of $search Jane+Doe The $line was the last entry for a test and obviously, it only had a single word name - noone,107 Warehouse St,400.00,02/01/2016,200.00 – SharonF Mar 18 '16 at 23:56

1 Answers1

0

Simply do a urldecode() like so:

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

And you should be fine afterwards.

Jan
  • 42,290
  • 8
  • 54
  • 79