# Perform maths based on a number in a text file

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I'm tyring to simplify the design of a web site which uses PHP, and hope so
meone can help me.  Basically I want to store a number in a file on a web s
ite, and display that number, or a variation of it, on a number of web page
s. Let's say the parameter is "A", and is 10 ps. Three different web pages
need to display this number, but in 3 different ways.

1) First page shows just A
2) Second page multiples A by 3
3) Third page multiples A by -2

So if A was 10

1) Page 1 should show 10
2) Page 2 should show 30
3) Page 3 should show -20.

If the value of A changes, I'd like to update the web site in one place, an
d not go around to every page updating them individually, as that will be e
rror prone.

Is there a way to put the value of A in a text file, and use the value in d
ifferent parts of the site? If so how?

Since there will actually be 15 or so parameters, A, B, C ..., is there a w
ay I can put all 15 parameters in a file (perhaps one per line, or with whi
te space between them), rather than have 15 different files?

If you look at this web page which is for an economimcal / low cost vector
network analyzer kit,

http://www.vnacalibration.co.uk/support/85033/Rohde+Schwarz/

you can see the numeric values in the form used by Rohde & Schwarz for thei
r vector network analyzers, whereas in this page which is a bit longer, und
er the section "Standard Definitions",

http://www.vnacalibration.co.uk/support/85033/HP/

one has basicaly the same parameters, but expressed in a different way. Ano
ther make of vector network analyzer wants the parameters in a different wa
y.

Any thoughts?

Dave

## Re: Perform maths based on a number in a text file

drkirkby@gmail.com wrote:

<http://php.net/file_put_contents
<http://php.net/file_get_contents

However, it would be easier to include a constant definition.

Yes.

Do your own homework.

--
When all you know is jQuery, every problem looks \$(olvable).

## Re: Perform maths based on a number in a text file

On 18/11/13 09:23, drkirkby@gmail.com wrote:

yes.

yes. Or a database table. Rather than a file.

This is not a big problem intrinsically, but the implementation detail
is where the work would be.

I would strongly recommend using a database: setting that up is a bit of
a faff, but once set up it provides the sort of 'update by one screen,
read buy many others' sort of access that you need. And wont need any
file level parsing - you will get your numbers as an array from a Mysql
type query.

You can even embed the mathematics in the query instead of doing it on
PHP, if you want.

But all this assumes you are willing to knuckle down and learn basic
SQL, mysql administration, and how to set up web forms and the like.

If you are as you sound, a relative newcomer, this is a few weeks work :-(

However it's an ideal problem to cut your teeth on. :-)

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.

## Re: Perform maths based on a number in a text file

Why even bother with mysql. Use SQLite, where administration is zero.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

## Re: Perform maths based on a number in a text file

On 18/11/13 15:08, Tim Streater wrote:

Well I would have suggested it if I had ever used it myself, which I
haven't.

I wasn't aware it did file locking and so on and had a proper 'daemon'

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.

## Re: Perform maths based on a number in a text file

<snip>

It has locking and proper transactions (sqlite3 is better than sqlite2
in this regard) but no remote access.

When that is not required, it's an advantage rather than a disadvantage
in that there is one less thing to fail, one less service to manage, one
less login to remember and/or protect, and so on.

--
Ben.

## Re: Perform maths based on a number in a text file

It's not server-based, it's a useful replacement for fopen (). See:

<http://www.sqlite.org/whentouse.html

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

## Re: Perform maths based on a number in a text file

On Monday, 18 November 2013 13:19:50 UTC, The Natural Philosopher  wrote:

(

Hi,
thank you for your thoughs. To be honest, I'm not over keen on using a data
base. I'm not a web designer, but an engineer trying to get data about his
product on a web page. Maybe the constants will be updated in the future, b
ut perhaps they never will. It is certainly not something I expect to do on
a regular basis. The idea of spending 3 days working on this does not attr
act me.

To answer the question asked by someone else about how the constants will g
et updated. It will be me doing it manually - no program will update them.
Maybe file_get_contents will do what I want.

Dave

## Re: Perform maths based on a number in a text file

drkirkby@gmail.com wrote:

Another possibility would be to add the values as variables in a file and
then include() that file in scripts where the value(s) need to be used.

For example:

variables.inc
<?php
\$thisvar = 3;
\$thatvar = 7;
?>

And then in any script that needs to know these values something like:

<?php
//.....
include 'variables.inc';
\$new_thisvar = \$thisvar * 3;
//.....
?>

Add other ingredients to taste :-)

--
Cheers
David Robley

Useless Invention: Self stick frying pan.

## Re: Perform maths based on a number in a text file

On 20/11/13 11:50, drkirkby@gmail.com wrote:

It will, but instead of learning the database thingy, you need to learn
the file parse thingy...

Now it so happens I do both, so this is a code fragment that loads a
'one value per line' file of floating point numbers into PHP variables..

Its from this web page

http://www.gridwatch.templar.co.uk

and the results drive the dials there.

The file is itself generated by an asychronous process that scrapes data
from elsewhere.

The data comes in as megawatts: I wanted floating point gigawatts hence
the conversions.

------------------------
\$retval=file_get_contents("/tmp/nationalgrid");
\$results=explode("\n",\$retval);
\$demand=\$results[12];
\$freq=\$results[13];
\$nuclear=sprintf("%0.2f",\$results[4]/1000);
\$coal=sprintf("%0.2f",\$results[3]/1000);
\$ccgt=sprintf("%0.2f",\$results[0]/1000);
\$wind=sprintf("%0.2f",\$results[5]/1000);
\$hydro=sprintf("%0.2f",\$results[7]/1000);
\$pumped=sprintf("%0.2f",\$results[6]/1000);
\$french=sprintf("%0.2f",\$results[9]/1000);
\$irish=sprintf("%0.2f",\$results[10]/1000);
\$dutch=sprintf("%0.2f",\$results[11]/1000);
\$ew=sprintf("%0.2f",\$results[14]/1000);
\$oil=sprintf("%0.2f",\$results[2]/1000);
\$ocgt=sprintf("%0.2f",\$results[1]/1000);
\$other=sprintf("%0.2f",\$results[8]/1000);

---------------------------------------

If you can organise your file like that then this is about as crude and
simple as it gets.

This is a snapshot of the file

\$cat /tmp/nationalgrid
13978
0
0
17061
7533
5303
281
451
614
48
-252
598
45944
50.133
0.000
-----------------------------------------

Note that PHP will dependent on context look at a string like "3.72" and
interpret it as a string OR a floating point number, depending on the
operation you are doing on it. Dividing or multiplying such a string
forces it to be treated like a number. Printing it produces a string etc.

printf and sprintf are handy ways to format the number for display purposes.

explode is a handy way to split a string into an array of chunks
delimited by another string - in this case the end of line charracter.

ensure that the file you create is readable by the web server you are using.

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.

## Re: Perform maths based on a number in a text file

On 11/20/2013 6:50 AM, drkirkby@gmail.com wrote:

If they're only going to be updated very rarely, and done so manually, I
would just stick them in a .php file as defined constants, i.e.

# File constants.php
<?php
define ('CONST1', 42);
define ('CONST2', 53);
... etc.
?>

Then in the scripts where you need the constants, use:

# File script1.php

<?php
require_once('constants.php');

\$value1 = CONST1 * 3; // 126
\$value2 = CONST2 + 5; // 58

?>

The advantage of using constants is that a script cannot change them.

P.S. For personal preferences, I place all files like this outside of
the websites DOCUMENT_ROOT directory and access them relative to
DOCUMENT_ROOT.  For instance, if my DOCUMENT_ROOT directory is

/var/www/mysite/html

I would place it as

/var/www/mysite/constants.php

then access them everywhere with

require_once(\$_SERVER['DOCUMENT_ROOT'] . '/constants.php');

This has three advantages - it prevents the file from being accessed
directly from the web (not a real concern in this case), it keeps
included (only) scripts from cluttering up your directories with
web-accessible files, and keeps the include code the same for any
script, no matter where in your website directory.

The only real downside is you must update the file locally and upload it
to the server or ssh into the server and edit the file there.  (You
could build a web page to modify the script - but if it isn't going to
be changed often, why bother?).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Perform maths based on a number in a text file

drkirkby@gmail.com writes:

<snip>

There is a missing piece of information.  How will these numbers be
updated?  If they will change because you choose to alter them, then
just put them into a PHP source file that is included in each of the
pages.  They can be written as named constants or as an array indexed
in whatever way makes the accesses clearer.

If the updates are to be done by some page or other (by which I really
mean if the updates are done by some software on the server) then
putting them into a file will pay off.  Personally, 15 numbers is below
the threshold I have for setting up a database, but if I did choose a
database, I'd most likely use something very simple like sqlite.

For the programmed option you don't need much more than 'split' and
'file_get_contents' ('join' and 'file_put_contents' to save).

<snip>
--
Ben.

## Re: Perform maths based on a number in a text file

On Monday, 18 November 2013 14:46:04 UTC, Ben Bacarisse  wrote:

Thank you Ben.

The constants will be updated manually by me - not by any software.

Dave

## Re: Perform maths based on a number in a text file

On 11/18/2013 4:23 AM, drkirkby@gmail.com wrote:

Sure.  Look up the file functions - fopen, fread, fwrite, etc.  But you
do have to watch out that another script is not accessing the file while
you are writing it.  Additionally, when you do write the file, you will
need to write all values unless you're changing the last one.

Easier would be a database which solves these problems for you.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Perform maths based on a number in a text file

On Monday, 18 November 2013 16:38:29 UTC, Jerry Stuckle  wrote:

I can edit the file with the constant in it using just a text editor (vi).  No script or any other automated software will do it.

Since I know C, fopen, fread, fwrite probably have very similar meanings, and I could probably work that out.

Dave

## Re: Perform maths based on a number in a text file

On Mon, 18 Nov 2013 01:23:06 -0800, drkirkby wrote:

Yes, why do you want to store the data in a text file?

Is it possible that different users of your web site might have different
numbers at the same time?

What happens if you have 1 text file and 2 users have different numbers?

Do you want to store the number for a short period (seconds, minutes,
maybe hours) or for longer periods (days, weeks, months)?

If you're trying to store user specific math data for a short period, the
best storage method is probably to use the php session mechanism. This
allows you to associate a set of data variables with each user identified
by a session cookie on their browser (the cookie is basically a random
number that you generate once when a user starts a session).

You then have a php superglobal array called \$_SESSION which can then
contain any named variables you wish on a per user basis.

This may be an over-simplification of php sessions, for more information
look at the session functions especially session_start() in the php
documentation.

http://us1.php.net/session_start

--
Denis McMahon, denismfmcmahon@gmail.com