Mike Ashfield
Mike Ashfield

Reputation: 91

Reading file contents into a var and adding value from form to it

I have been working on this very simple (to somebody well versed) problem for a few hours now, Google throwing up no clear answers, and I can't find anything similar here, so please don't shoot me down for asking for help here.

I'm working on a simple page to show current donations (will refresh regularly) at an upcoming charity event.

Here's the file structure of what I'm working with

I've managed to get /index.php to output the donation total (here's the source for data.php:

<?php $data='0.00';?>

Index does the following, basically:

<?php
    $pound = htmlspecialchars("£", ENT_QUOTES);
    include 'assets/files/donation_total/data.php';
    echo '<h1>'.$pound.$data.'</h1>';
?>

I'm working on action_donations.php now which SHOULD take the value from data.php after using substr to get the total donations value (stripping 13 chars from left, and 4 from the right)

But it's not working. It outputs nothing. What am I doing wrong?

<?php
    // get contents of a file into a string
    $filename = "/assets/files/donation_total/data.php";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    echo $contents;
    $value = substr($contents, 13);
    $value_cleaned = substr($value, 0, -4);
    echo $value_cleaned;
?>

I simply need it to read data.php for the current total, take the value from the form, add the two together, then write that value back to data.php

Upvotes: 1

Views: 54

Answers (2)

Heiko Rothe
Heiko Rothe

Reputation: 331

If I understood that correctly, the whole purpose of the last script is to echo out the number that's 0.00 in your example - why don't you just do it like in your index.php and include data.php. Then you can just use $data and echo that out. Reading out a PHP file and using hardcoded character limits to strip away unimportant information is generally a bad idea.

Upvotes: 0

akr
akr

Reputation: 739

change the path to data.php to:
include_once('../assets/files/donation_total/data.php');

Upvotes: 1

Related Questions