phpmedo

From: ANT_THOMAS21 Feb 2016 23:35
To: ALL1 of 23
I have this code
Code: PHP
$marking0 = strtotime('sunday') * 1000;
$marking0a = $marking0 - 96400000;
$marking0 = $marking0 + 10000000;

$marking1 = strtotime('-1 week sunday') * 1000;
$marking1a = $marking1 - 96400000;
$marking1 = $marking1 + 10000000;

$markings = "
{ xaxis: { from: $marking0, to: $marking0a }, color: \"#F3F3ED\" },
{ xaxis: { from: $marking1, to: $marking1a }, color: \"#F3F3ED\" }
" ;
I need the same thing 52 times eg
Code: 
$marking2 = strtotime('-2 week sunday') * 1000;
$marking2a = $marking2 - 96400000;
$marking2 = $marking2 + 10000000;

$marking3 = strtotime('-3 week sunday') * 1000;
$marking3a = $marking3 - 96400000;
$marking3 = $marking3 + 10000000;

......

$marking52 = strtotime('-52 week sunday') * 1000;
$marking52a = $marking52 - 96400000;
$marking52 = $marking52 + 10000000;


$markings = "
{ xaxis: { from: $marking0, to: $marking0a }, color: \"#F3F3ED\" },
{ xaxis: { from: $marking1, to: $marking1a }, color: \"#F3F3ED\" },
{ xaxis: { from: $marking2, to: $marking2a }, color: \"#F3F3ED\" },
{ xaxis: { from: $marking3, to: $marking3a }, color: \"#F3F3ED\" },
.......
{ xaxis: { from: $marking52, to: $marking52a }, color: \"#F3F3ED\" }
" ;
There must be a pretty simple way to generate 52 of those without doing it all manually?

 
From: Peter (BOUGHTONP)21 Feb 2016 23:56
To: ANT_THOMAS 2 of 23

Seems like a convoluted way to do whatever this is, but yeah, here's a simple way to generate that - all untested and may contain stupid bugs because I've got a headache, but here you go...

First build the array of weeks:

    $marking = [ strtotime('sunday') * 1000 ];
    for ( var $i = 1 ; $i <= 52 ; $i++ )
        $marking[i] = strtotime((-1*$i).' week sunday') * 1000;

Then re-map each one into the desired format (use a better function name):

    function convert_to_my_format( $CurMarking )
    {
        return "{ xaxis: { from: ".($CurMarking+10000000).", to: ".($CurMarking-96400000)." }, color: \"#F3F3ED\" }";
    }

    $markings = array_map( "convert_to_my_format" , $marking );

Finally convert that array into a comma-delimited string:

    $markings = implode( "," , $markings );
From: ANT_THOMAS22 Feb 2016 00:40
To: Peter (BOUGHTONP) 3 of 23
Thanks BP. I'll give that a go tomorrow.
From: ANT_THOMAS22 Feb 2016 12:09
To: Peter (BOUGHTONP) 4 of 23
Right, we're close!

If I include the var in the 2nd line I get a 500 server error.

With it removed it outputs the 52nd and final part of the array but not the full array, I've checked by changing the 52 to another number.
 
Code: 
   for ( $i = 1 ; $i <= 52 ; $i++ )
        $marking[i] = strtotime((-1*$i).' week sunday') * 1000;

   function convert_to_markings( $CurMarking )
    {
        return "{ xaxis: { from: ".($CurMarking).", to: ".($CurMarking-96400000)." }, color: \"#F3F3ED\" }";
    }

    $markings = array_map( "convert_to_markings" , $marking );

    $markings = implode( "," , $markings );
Output from that is
 
Code: 
{ xaxis: { from: 1425168000000, to: 1425071600000 }, color: "#F3F3ED" }

Please help Peter!
From: Chris (CHRISSS)22 Feb 2016 20:30
To: ANT_THOMAS 5 of 23
There's a missing $ in the for loop code. $marking[i] should be $marking[$i]

Dunno if that will fix it though.
From: ANT_THOMAS22 Feb 2016 20:34
To: Chris (CHRISSS) 6 of 23
I have literally just spotted that, working now :D
From: Chris (CHRISSS)22 Feb 2016 21:07
To: ANT_THOMAS 7 of 23
Good timing :D Glad it's working.
From: ANT_THOMAS22 Feb 2016 23:05
To: Chris (CHRISSS) 8 of 23
It nicely highlights the weekends on my electricity usage chart.
From: Peter (BOUGHTONP)22 Feb 2016 23:16
To: Chris (CHRISSS) 9 of 23
Bah! Damn PHP and its stupid dollars.
From: Chris (CHRISSS)23 Feb 2016 00:58
To: ANT_THOMAS 10 of 23
Is that from the camera you have pointing at your electric meter?
From: Chris (CHRISSS)23 Feb 2016 01:00
To: Peter (BOUGHTONP) 11 of 23
Who puts dollar symbols in front of variable names? :(
From: CHYRON (DSMITHHFX)23 Feb 2016 02:07
To: Chris (CHRISSS) 12 of 23
jquery. Well, in front of selectors.
EDITED: 23 Feb 2016 02:07 by DSMITHHFX
From: ANT_THOMAS23 Feb 2016 10:40
To: Chris (CHRISSS) 13 of 23
That's the one.

I was originally just putting it in a spreadsheet and trawling through a load of readings every week or so (never got OCR working).

But now I've got it all in a MySQL database where I've got a little form that checks the last row in the database, then checks the date and displays the image for the next reading I need to enter, once entered it calculates the usage that day and the cost that day and adds them to the database. Data stored is - date, total meter reading, daily usage, cost. When it's up to date it tells me there's nothing more to add.

 
 
EDITED: 23 Feb 2016 10:54 by ANT_THOMAS
Attachments:
From: ANT_THOMAS23 Feb 2016 10:51
To: ANT_THOMAS 14 of 23
The last 12 Weeks

Attachments:
From: Peter (BOUGHTONP)23 Feb 2016 23:40
To: Chris (CHRISSS) 15 of 23
Idiots who blindly copy bits of shell script into a toy language without really knowing what they're doing, and then come up with lame justifications of why they shouldn't fix the nonsense when everyone uses the damned thing. Probably.
From: Peter (BOUGHTONP)23 Feb 2016 23:42
To: CHYRON (DSMITHHFX) 16 of 23
A perfect example!

...of an idiot who doesn't know what things are. :S

Hmm, bit harsh? Maybe just a dunce. (hug)

jQuery neither prefixes variable names not puts dollars in front of selectors. It aliases itself as $, and as a function it can have arguments, the first of which can be a selector, though it also can be other things, and also has various methods attached to the function's object itself.

Which is not to say it isn't a bit dumb, but it is at least more convenient than typing case sensitive jQuery, though even that isn't as $durbrained $as $bloody $_PHP.

From: Peter (BOUGHTONP)23 Feb 2016 23:50
To: ANT_THOMAS 17 of 23
It seems cost is just half of usage - or does that change?
From: ANT_THOMAS24 Feb 2016 10:36
To: Peter (BOUGHTONP) 18 of 23
Cost is calculated based on a standing daily rate + (units used * unit rate)
From: koswix24 Feb 2016 10:45
To: Peter (BOUGHTONP) 19 of 23
I've been wondering about that lately. Linux is, in theory, a much more secure OS than windows. But how many Linux users consist of people like me that spend days at a time running commands and scripts they find down the back of the internet trying to make bits of software work without any real clue of wtf they are actually doing? 
From: ANT_THOMAS24 Feb 2016 11:09
To: koswix 20 of 23
Depending on your level of knowledge, running a command is less of an issue because you can see what you're inputting (and hopefully have some idea what it does, but often not), running scripts though is a different issue since I doubt most people check every line.

Could have a format c: in there somewhere.