FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Perform maths based on a number in a text file
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Perform maths based on a number in a text file [message #183747] Mon, 18 November 2013 09:23 Go to next message
drkirkby is currently offline  drkirkby
Messages: 4
Registered: November 2013
Karma: 0
Junior Member
I'm tyring to simplify the design of a web site which uses PHP, and hope someone can help me. Basically I want to store a number in a file on a web site, and display that number, or a variation of it, on a number of web pages. 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, and not go around to every page updating them individually, as that will be error prone.

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

Since there will actually be 15 or so parameters, A, B, C ..., is there a way I can put all 15 parameters in a file (perhaps one per line, or with white 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 their vector network analyzers, whereas in this page which is a bit longer, under the section "Standard Definitions",

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

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

Any thoughts?

Dave
Re: Perform maths based on a number in a text file [message #183748 is a reply to message #183747] Mon, 18 November 2013 10:57 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
drkirkby(at)gmail(dot)com wrote:

> […]
> 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,
> and not go around to every page updating them individually, as that will
> be error prone.
>
> Is there a way to put the value of A in a text file, and use the value in
> different parts of the site? If so how?

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

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

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

Yes.

> Any thoughts?

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 [message #183750 is a reply to message #183747] Mon, 18 November 2013 13:19 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 18/11/13 09:23, drkirkby(at)gmail(dot)com wrote:
> I'm tyring to simplify the design of a web site which uses PHP, and
> hope someone can help me. Basically I want to store a number in a
> file on a web site, and display that number, or a variation of it, on
> a number of web pages. 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, and not go around to every page updating them individually, as
> that will be error prone.
>
> Is there a way to put the value of A in a text file, and use the
> value in different parts of the site? If so how?
>

yes.

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

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

> 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 their vector network analyzers, whereas in this page which is a
> bit longer, under the section "Standard Definitions",
>
> http://www.vnacalibration.co.uk/support/85033/HP/
>
> one has basicaly the same parameters, but expressed in a different
> way. Another make of vector network analyzer wants the parameters in
> a different way.
>
> Any thoughts?
>
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. :-)



> Dave
>


--
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 [message #183755 is a reply to message #183747] Mon, 18 November 2013 14:46 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
drkirkby(at)gmail(dot)com writes:

> I'm tyring to simplify the design of a web site which uses PHP, and
> hope someone can help me. Basically I want to store a number in a
> file on a web site, and display that number, or a variation of it, on
> a number of web pages. 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
<snip>
> Since there will actually be 15 or so parameters, A, B, C ..., is
> there a way I can put all 15 parameters in a file (perhaps one per
> line, or with white space between them), rather than have 15 different
> files?

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 [message #183758 is a reply to message #183750] Mon, 18 November 2013 15:08 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <l6d45m$67d$1(at)news(dot)albasani(dot)net>, The Natural Philosopher
<tnp(at)invalid(dot)invalid> wrote:

> On 18/11/13 09:23, drkirkby(at)gmail(dot)com wrote:
>> I'm tyring to simplify the design of a web site which uses PHP, and
>> hope someone can help me. Basically I want to store a number in a
>> file on a web site, and display that number, or a variation of it, on
>> a number of web pages. 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, and not go around to every page updating them individually, as
>> that will be error prone.
>>
>> Is there a way to put the value of A in a text file, and use the
>> value in different parts of the site? If so how?
>>
>
> yes.
>
>> Since there will actually be 15 or so parameters, A, B, C ..., is
>> there a way I can put all 15 parameters in a file (perhaps one per
>> line, or with white space between them), rather than have 15
>> different files?
>>
>
> yes. Or a database table. Rather than a file.
>
>> 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 their vector network analyzers, whereas in this page which is a
>> bit longer, under the section "Standard Definitions",
>>
>> http://www.vnacalibration.co.uk/support/85033/HP/
>>
>> one has basicaly the same parameters, but expressed in a different
>> way. Another make of vector network analyzer wants the parameters in
>> a different way.
>>
>> Any thoughts?
>>
> 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.

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 [message #183765 is a reply to message #183758] Mon, 18 November 2013 16:22 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 18/11/13 15:08, Tim Streater wrote:
> In article <l6d45m$67d$1(at)news(dot)albasani(dot)net>, The Natural Philosopher
> <tnp(at)invalid(dot)invalid> wrote:
>
>> On 18/11/13 09:23, drkirkby(at)gmail(dot)com wrote:
>>> I'm tyring to simplify the design of a web site which uses PHP, and
>>> hope someone can help me. Basically I want to store a number in a
>>> file on a web site, and display that number, or a variation of it, on
>>> a number of web pages. 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, and not go around to every page updating them individually, as
>>> that will be error prone.
>>>
>>> Is there a way to put the value of A in a text file, and use the
>>> value in different parts of the site? If so how?
>>>
>>
>> yes.
>>
>>> Since there will actually be 15 or so parameters, A, B, C ..., is
>>> there a way I can put all 15 parameters in a file (perhaps one per
>>> line, or with white space between them), rather than have 15
>>> different files?
>>>
>>
>> yes. Or a database table. Rather than a file.
>>
>>> 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 their vector network analyzers, whereas in this page which is a
>>> bit longer, under the section "Standard Definitions",
>>>
>>> http://www.vnacalibration.co.uk/support/85033/HP/
>>>
>>> one has basicaly the same parameters, but expressed in a different
>>> way. Another make of vector network analyzer wants the parameters in
>>> a different way.
>>>
>>> Any thoughts?
>>>
>> 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.
>
> Why even bother with mysql. Use SQLite, where administration is zero.
>

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 [message #183768 is a reply to message #183747] Mon, 18 November 2013 16:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/18/2013 4:23 AM, drkirkby(at)gmail(dot)com wrote:
> I'm tyring to simplify the design of a web site which uses PHP, and hope someone can help me. Basically I want to store a number in a file on a web site, and display that number, or a variation of it, on a number of web pages. 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, and not go around to every page updating them individually, as that will be error prone.
>
> Is there a way to put the value of A in a text file, and use the value in different parts of the site? If so how?
>
> Since there will actually be 15 or so parameters, A, B, C ..., is there a way I can put all 15 parameters in a file (perhaps one per line, or with white 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 their vector network analyzers, whereas in this page which is a bit longer, under the section "Standard Definitions",
>
> http://www.vnacalibration.co.uk/support/85033/HP/
>
> one has basicaly the same parameters, but expressed in a different way. Another make of vector network analyzer wants the parameters in a different way.
>
> Any thoughts?
>
> Dave
>

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(at)attglobal(dot)net
==================
Re: Perform maths based on a number in a text file [message #183769 is a reply to message #183765] Mon, 18 November 2013 16:50 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
The Natural Philosopher <tnp(at)invalid(dot)invalid> writes:

> On 18/11/13 15:08, Tim Streater wrote:
<snip>
>> Why even bother with mysql. Use SQLite, where administration is zero.
>>
>
> 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'

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 [message #183770 is a reply to message #183765] Mon, 18 November 2013 16:53 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <l6desn$s68$1(at)news(dot)albasani(dot)net>, The Natural Philosopher
<tnp(at)invalid(dot)invalid> wrote:

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

>> Why even bother with mysql. Use SQLite, where administration is zero.
>
> 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'

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 [message #183800 is a reply to message #183750] Wed, 20 November 2013 11:50 Go to previous messageGo to next message
drkirkby is currently offline  drkirkby
Messages: 4
Registered: November 2013
Karma: 0
Junior Member
On Monday, 18 November 2013 13:19:50 UTC, The Natural Philosopher wrote:

> 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 :-(

Hi,
thank you for your thoughs. To be honest, I'm not over keen on using a database. 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, but 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 attract me.

To answer the question asked by someone else about how the constants will get 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 [message #183801 is a reply to message #183755] Wed, 20 November 2013 11:53 Go to previous messageGo to next message
drkirkby is currently offline  drkirkby
Messages: 4
Registered: November 2013
Karma: 0
Junior Member
On Monday, 18 November 2013 14:46:04 UTC, Ben Bacarisse wrote:
> drkirkby(at)gmail(dot)com writes:
>
>
>
>> I'm tyring to simplify the design of a web site which uses PHP, and
>
>> hope someone can help me. Basically I want to store a number in a
>
>> file on a web site, and display that number, or a variation of it, on
>
>> a number of web pages. 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
>
> <snip>
>
>> Since there will actually be 15 or so parameters, A, B, C ..., is
>
>> there a way I can put all 15 parameters in a file (perhaps one per
>
>> line, or with white space between them), rather than have 15 different
>
>> files?
>
>
>
> 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.

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 [message #183802 is a reply to message #183768] Wed, 20 November 2013 11:58 Go to previous messageGo to next message
drkirkby is currently offline  drkirkby
Messages: 4
Registered: November 2013
Karma: 0
Junior Member
On Monday, 18 November 2013 16:38:29 UTC, Jerry Stuckle 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.

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 [message #183804 is a reply to message #183800] Wed, 20 November 2013 12:02 Go to previous messageGo to next message
David Robley is currently offline  David Robley
Messages: 23
Registered: March 2013
Karma: 0
Junior Member
drkirkby(at)gmail(dot)com wrote:

> On Monday, 18 November 2013 13:19:50 UTC, The Natural Philosopher wrote:
>
>> 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
>> :-(
>
> Hi,
> thank you for your thoughs. To be honest, I'm not over keen on using a
> database. 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, but 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 attract me.
>
> To answer the question asked by someone else about how the constants will
> get updated. It will be me doing it manually - no program will update
> them. Maybe file_get_contents will do what I want.
>
> Dave

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 [message #183805 is a reply to message #183800] Wed, 20 November 2013 13:03 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 20/11/13 11:50, drkirkby(at)gmail(dot)com wrote:
> On Monday, 18 November 2013 13:19:50 UTC, The Natural Philosopher wrote:
>
>> 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 :-(
>
> Hi,
> thank you for your thoughs. To be honest, I'm not over keen on using a database. 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, but 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 attract me.
>
> To answer the question asked by someone else about how the constants will get updated. It will be me doing it manually - no program will update them. Maybe file_get_contents will do what I want.
>

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.


> Dave
>


--
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 [message #183807 is a reply to message #183800] Wed, 20 November 2013 16:20 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/20/2013 6:50 AM, drkirkby(at)gmail(dot)com wrote:
> On Monday, 18 November 2013 13:19:50 UTC, The Natural Philosopher wrote:
>
>> 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 :-(
>
> Hi,
> thank you for your thoughs. To be honest, I'm not over keen on using a database. 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, but 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 attract me.
>
> To answer the question asked by someone else about how the constants will get updated. It will be me doing it manually - no program will update them. Maybe file_get_contents will do what I want.
>
> Dave
>

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(at)attglobal(dot)net
==================
Re: Perform maths based on a number in a text file [message #183808 is a reply to message #183747] Thu, 21 November 2013 03:10 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 18 Nov 2013 01:23:06 -0800, drkirkby wrote:

> I'm tyring to simplify the design of a web site which uses PHP, and hope
> someone can help me. Basically I want to store a number

> Any thoughts?

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(at)gmail(dot)com
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: array search part 2
Next Topic: What do you think about this book on API ?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Dec 18 04:46:55 GMT 2024

Total time taken to generate the page: 0.03016 seconds