adding (Showing current start to number of entries of Total entries) in zend Paginator - zend-framework

what i want to do is that i wana show user this
Showing 1 to 10 of 50 entries
Showing 11 to 20 of 50 entries
Showing 21 to 30 of 50 entries
Showing 31 to 40 of 50 entries
Showing 41 to 50 of 50 entries
i have used Zend Paginator in my app lets say
Showing A to B of C entries
I can easily find C which is equal to
$result = $DB->fetchAll($sql);
$total =count($result);
if we see here
$page=$this->_getParam('page',1);
//here we can get the requested page#.
//lets hard code this
$paginator->setItemCountPerPage(10);
$per_page =10;
in my view count($this->paginator) give me total number of pages that is if
if total = 101 = $total
than page = 9 = $page
and paginator = 11 = count($this->paginator)
how can i achieve this but generic mean working with next,previous and so on..

Showing A to B of C entries
Is roughly this:
$page = $paginator->getCurrentPageNumber();
$perPage = $paginator->getItemCountPerPage();
$total = $paginator->getTotalItemCount();
$A = ($page - 1) * $perPage + 1;
$B = min($A + $perPage - 1, $total);
$C = $total;

Related

python code worked with Proxy server, now not working without

'All,this is code that will put production counts off machines and load them into a lean manufacturing software. This tracks production states and Preventative maintenance for machines. The code used to work until network changes started with changing v-lans and now the latest no proxy server. since then they assigned the PC which this code will be running on with a static IP not requiring it to read from the company server, nor go through a proxy. When starting Python with command line Shift right click (l2l.py) the screen is black then goes away. If the code is tested with Notepad++, the code gives errors pertaining to xlrd, then requests. I changed all upgraded the xlrd and the files are in the script folder of the pc. Still striking out to no avail. I don't do much C code, but have been searching several websites for answers. Any help would be appreciated. Thank You- Bruno'
import xlrd # module to interface with excel
import requests # module to upload the data to L2L
from datetime import datetime #module for time stamps
import os
import time
outtime = 5
import requests
session = requests.Session()
session.trust_env = false
response = session.get('http://www.graham.leading2lean.com')
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
L1Startlog = 0
L1Startgood = 0
L1Flog = 0
L1Fgood = 0
L1EndLog = 0
L1EndGood = 0
L1Err = 0
file_location = "C:/Users/brfdc/Desktop/L2L.xlsm" #location of excel file on local computer
workbook = xlrd.open_workbook(file_location)#opening the excel file specified in the previous line
sheet = workbook.sheet_by_index(0) #opening the first sheet in the excel file.
L3Sgood = sheet.cell_value(3,1) #Line 3 starting report good bottle count
#--------------------------------------------------
#Line three error checking
if (L3Sgood != 42) and (L3Slog != 42):
L3Startgood = L3Sgood
L3Startlog = L3Slog
else:
pass
if (L3Bottle != 42):
L3BottleG = L3Bottle
else:
L3BottleG = 0
#end of line 3 error checking
#--------------------------------------------------
if (L5Sgood != 42) and (L5Slog != 42): #Line 5 Error checking
L5Startgood = L5Sgood #Line 5
L5Startlog = L5Slog #Line 5
else:
pass
if (L1Sgood !=42) and (L1Slog != 42): #Line 1 Error checking
L1Startgood = L1Sgood #Line 1
L1Startlog = L1Slog #Line 1
else:
pass
if (L2Sgood != 42) and (L2Slog != 42): #Line 2 Error checking
L2Startgood = L2Sgood #Line 2
L2Startlog = L2Slog #Line 2
else:
pass
if (L7Sgood != 42) and (L7SAlog != 42) and (L7SBlog != 42): #Line 7 Error checking
L7Startgood = L7Sgood #Line 7
L7StartlogA = L7SAlog #Line 7
L7StartlogB = L7SBlog #Line 7
else:
pass
if (L6Sgood != 42) and (L6Slog != 42): #Line 6 error checking
L6Startgood = L6Sgood #Line 6
L6Startlog = L6Slog #Line 6
else:
pass
if (L8Sgood != 42) and (L8SAlog != 42) and (L8SBlog != 42): #Line 8 Error checking
L8Startgood = L8Sgood #Line 8
L8StartlogA = L8SAlog #Line 8
L8StartlogB = L8SBlog #Line 8
else:
pass
Api_End_Point = 'https://graham.leading2lean.com/api/1.0/pitchdetails/record_details/'
API_key = "c74de26eee9d5617de3218b5301d00de"
url = Api_End_Point +"?auth=" + API_key
DATETIME_SECONDS_STRING_FORMAT = "%Y-%m-%d %H:%M:%S"
testsendfinal = 0
L7StartlogT = L7StartlogA + L7StartlogB #Add the two SIPA sides together Line 7
L8StartlogT = L8StartlogA + L8StartlogB #Add the two SIPA sides together Line 8
while True:
#while True:
try:
startingTime = datetime.now().strftime(DATETIME_SECONDS_STRING_FORMAT) #assigning the current time to this variable
print(startingTime)
secs = datetime.now().strftime("%S")
mins = datetime.now().strftime("%M")
#corrective math for on the dot 30 minute marks because program drifts in time over the course of an hour.
if (int(mins) >= 27 and int(mins) < 30):
minutes_to_30 = 29 - int(mins)
minutes_to_seconds = minutes_to_30 * 60
secs_to_go = 60 - int(secs)
seconds_to_30 = secs_to_go + minutes_to_seconds
pause_time = seconds_to_30
print(seconds_to_30)
else:
pause_time = 60 #180
#--------------------------------------------------------------------------------------------------------------------------
print('start time of log ' + startingTime) #this is just printing on the screen what the time is. used for troubleshooting
L3SLoss = L3Startlog - L3Startgood #Line 3 Difference in two counters to calculate scrap
L5SLoss = L5Startlog - L5Startgood #Line 5 Difference in two counters to calculate scrap
L1SLoss = L1Startlog - L1Startgood #Line 1 Difference in two counters to calculate scrap
L2SLoss = L2Startlog - L2Startgood #Line 2 Difference in two counters to calculate scrap
L6SLoss = L6Startlog - L6Startgood #Line 6 Difference in two counters to calculate scrap
L7SLoss = L7StartlogT - L7Startgood #Line 7 Difference in two counters to calculate scrap
L8SLoss = L8StartlogT - L8Startgood #Line 8 Difference in two counters to calculate scrap
#change 6:30 and 18:30 start of report numbers. force these numbers to zero when at these two times.
hour_time = datetime.now().strftime("%H")
minute_time = datetime.now().strftime("%M")
if int(hour_time) == 6 or int(hour_time) == 18:
if int(minute_time) == 30:
L1SLoss = 0
L2SLoss = 0
L3SLoss = 0
L5SLoss = 0
L6SLoss = 0
L7SLoss = 0
L8SLoss = 0
L1Startgood = 0
L2Startgood = 0
L3Startgood = 0
L5Startgood = 0
L6Startgood = 0
L7Startgood = 0
L8Startgood = 0
L1EndGood = 0#added this 10/29/2018
L2EndGood = 0
L3EndGood = 0
L5EndGood = 0
L6EndGood = 0
L7EndGood = 0
L8EndGood = 0
L1EndLog = 0
L2EndLog = 0
L3EndLog = 0
L5EndLog = 0
L6EndLog = 0
L7EndLogA = 0
L7EndLogB = 0
L8EndLogA = 0
L8EndLogB = 0
os.system("new.bat")
else:
print("Not right minute for number force to zero")
else:
print("Not Right time for number force to zero ")
#--------------------------------------------------------------------------------------
time.sleep(pause_time) # sleep for ~3 minutes
#--------------------------------------------------------------------------------------
currentTime = datetime.now().strftime(DATETIME_SECONDS_STRING_FORMAT) #assigning the current time to this variable.
file_location = "C:/Users/brfdc/Desktop/L2L.xlsm" #location of excel file on local computer
workbook = xlrd.open_workbook(file_location) #opening the excel file specified in the previous line
sheet = workbook.sheet_by_index(0) #opening the first sheet in the excel file
L3Fgood = sheet.cell_value(3,1) #Line 3 end report good bottle count
L3Flog = sheet.cell_value(2,1) #Line 3 end report log bottle count
L5Flog = sheet.cell_value(6,1) #Line 5 end report log bottle count
L5Fgood = sheet.cell_value(7,1) #Line 5 end report good bottle count
L1Flog = sheet.cell_value(9,2) #Line 1 end report log bottle count
L1Fgood = sheet.cell_value(10,1)#Line 1 end report good bottle count
L2Fgood = sheet.cell_value(14,1)#Line 2 end report good bottle count
L2Flog = sheet.cell_value(13,1) #Line 2 end report log bottle count
L7Fgood = sheet.cell_value(21,1)#Line 7 starting report good bottle count
L7FAlog = sheet.cell_value(19,1)#Line 7 starting report log bottle count side A
L7FBlog = sheet.cell_value(20,1)#Line 7 starting report log bottle count side B
L6Flog = sheet.cell_value(17,1) #Line 6 end report log bottle count
L6Fgood = sheet.cell_value(18,1)#Line 6 end report good bottle count
L3Bottle = sheet.cell_value(3,3)#Line 3 bottle being run. 1 means running arizona
L8Fgood = sheet.cell_value(25,1)#Line 8 starting report good bottle count
L8FAlog = sheet.cell_value(23,1)#Line 8 starting report log bottle count side A
L8FBlog = sheet.cell_value(24,1)#Line 8 starting report log bottle count side B
mins_time = datetime.now().strftime("%M")
m = datetime.now().strftime("%M")
if (L3Fgood != 42) and (L3Flog != 42): #Line 3
L3EndGood = L3Fgood #Line 3
L3EndLog = L3Flog #Line 3
L3Err = 0
else:
L3Err += 1
if (L3Bottle != 42):
L3BottleG = L3Bottle
if (L5Flog != 42) and (L5Fgood != 42): #Line 5
L5EndGood = L5Fgood #Line 5
L5EndLog = L5Flog #Line 5
L5Err = 0
else:
L5Err += 1
if (L1Flog != 42) and (L1Fgood != 42): #Line 1
L1EndGood = L1Fgood #Line 1
L1EndLog = L1Flog #Line 1
L1Err = 0
else:
L1Err += 1
if (L2Fgood != 42) and (L2Flog != 42): #Line 2
L2EndGood = L2Fgood #Line 2
L2EndLog = L2Flog #Line 2
L2Err = 0
else:
L2Err += 1
if (L7Fgood != 42) and (L7FAlog != 42) and (L7FBlog != 42):
L7EndGood = L7Fgood
L7EndLogA = L7FAlog
L7EndLogB = L7FBlog
L7Err = 0
else:
L7Err += 1
if (L6Fgood != 42) and (L6Flog != 42):
L6EndGood = L6Fgood
L6EndLog = L6Flog
L6Err = 0
else:
L6Err += 1
if (L8Fgood != 42) and (L8FAlog != 42) and (L8FBlog != 42):
L8EndGood = L8Fgood
L8EndLogA = L8FAlog
L8EndLogB = L8FBlog
L8Err = 0
else:
L8Err += 1
L3EndLoss = L3EndLog - L3EndGood #Line 3 Difference in two counters at end of report to calculate scrap
L5EndLoss = L5EndLog - L5EndGood #Line 5 Difference in two counters at end of report to calculate scrap
L1EndLoss = L1EndLog - L1EndGood #Line 1 Difference in two counters at end of report to calculate scrap
L2EndLoss = L2EndLog - L2EndGood #Line 2 Difference in two counters at end of report to calculate scrap
L6EndLoss = L6EndLog - L6EndGood #Line 6 Difference in two counters at end of report to calculate scrap
L7EndLogT = L7EndLogA + L7EndLogB #Line 7 add two SIPA log bottle sides together
L7EndLoss = L7EndLogT - L7EndGood #Line 7 Difference in two counters at end of report to calculate scrap
L8EndLogT = L8EndLogA + L8EndLogB #Line 8 add two SIPA log bottle sides together
L8EndLoss = L8EndLogT - L8EndGood #Line 8 Difference in two counters at end of report to calculate scrap
#--------------------------------------------------------------------------------------
L3ChangeGood = L3EndGood - L3Startgood #Line 3 Change in starting report good bottle count and ending report good bottle count
L3ChangeLoss = L3EndLoss - L3SLoss #Line 3 Change in two different scrap numbers calculated above
L5ChangeGood = L5EndGood - L5Startgood #Line 5 Change in starting report good bottle count and ending report good bottle count
L5ChangeLoss = L5EndLoss - L5SLoss #Line 5 Change in starting report loss calculation and ending report loss calculation
L1ChangeGood = L1EndGood - L1Startgood #Line 1 Change in starting report good bottle count and ending report good bottle count
L1ChangeLoss = L1EndLoss - L1SLoss #Line 1 Change in starting report loss calculation and ending report loss calculation
L2ChangeGood = L2EndGood - L2Startgood #Line 2 Change in starting report good bottle count and ending report good bottle count
L2ChangeLoss = L2EndLoss - L2SLoss #Line 2 Change in two different scrap numbers calculated above
L6ChangeGood = L6EndGood - L6Startgood #Line 6 change in starting report good bottle count and end report good bottle count
L6ChangeLoss = L6EndLoss - L6SLoss #Line 6 change in two different scrap numbers calculated above
L7ChangeGood = L7EndGood - L7Startgood #Line 7 Change in starting report good bottle count and ending report good bottle count
L7ChangeLoss = L7EndLoss - L7SLoss #Line 7 Change in two different scrap numbers calculated above
L8ChangeGood = L8EndGood - L8Startgood #Line 8 Change in starting report good bottle count and ending report good bottle count
L8ChangeLoss = L8EndLoss - L8SLoss #Line 8 Change in two different scrap numbers calculated above
#--------------------------------------------------------------------------------------
L7DiffLogA = L7EndLogA - L7StartlogA
L7DiffLogB = L7EndLogB - L7StartlogB
L8DiffLogA = L8EndLogA - L8StartlogA
L8DiffLogB = L8EndLogB - L8StartlogB
if L7DiffLogA >= 1 and L7DiffLogB == 0:
L7Bottle = "Side A"
elif L7DiffLogB >= 1 and L7DiffLogA == 0:
L7Bottle = "Side B"
else:
L7Bottle = "Side A & B"
if L8DiffLogA >= 1 and L8DiffLogB == 0:
L8Bottle = "Side A"
elif L8DiffLogB >= 1 and L8DiffLogA == 0:
L8Bottle = "Side B"
else:
L8Bottle = "Side A & B"
#--------------------------------------------------------------------------------------
#below is logic to never send negative numbers for actual bottles and not send a relatively large amount of lost bottles
m = datetime.now().strftime("%M")
if (L3ChangeLoss < -1000) or (L3ChangeLoss>300): #Line 3
L3SendLoss = 0 #Line 3
else: #Line 3
L3SendLoss = L3ChangeLoss #Line 3
if (L3ChangeGood <0): #Line 3
if int(m) < 33 and int(m) > 30: #Line 3
L3SendGood = L3EndGood #Line 3
else: #Line 3
L3SendGood = 0 #Line 3
else: #Line 3
L3SendGood = L3ChangeGood #Line 3
if (L5ChangeLoss < -1000) or (L5ChangeLoss>600): #Line 5
L5SendLoss = 0 #Line 5
else: #Line 5
L5SendLoss = L5ChangeLoss #Line 5
if (L5ChangeGood < 0): #Line 5
if int(m) < 33 and int(m) > 30: #Line 5
L5SendGood = L5EndGood #Line 5
else: #Line 5
L5SendGood = 0 #Line 5
else: #Line 5
L5SendGood = L5ChangeGood #Line 5
if (L1ChangeLoss < -1000): #Line 1
L1SendLoss = 0 #Line 1
else: #Line 1
L1SendLoss = L1ChangeLoss #Line 1
if (L1ChangeGood < 0): #Line 1
if int(m) < 33 and int(m) > 30: #Line 1
L1SendGood = L1EndGood #Line 1
else: #Line 1
L1SendGood = 0 #Line 1
else: #Line 1
L1SendGood = L1ChangeGood #Line 1
if (L2ChangeLoss < -1000) or (L2ChangeLoss>300): #Line 2
L2SendLoss = 0 #Line 2
else: #Line 2
L2SendLoss = L2ChangeLoss #Line 2
if (L2ChangeGood <0): #Line 2
if int(m) < 33 and int(m) > 30: #Line 2
L2SendGood = L2EndGood #Line 2
else: #Line 2
L2SendGood = 0 #Line 2
else: #Line 2
L2SendGood = L2ChangeGood #Line 2
if (L6ChangeLoss<-1000) or (L6ChangeLoss>3000): #Line 6
L6SendLoss = 0 #Line 6
else: #Line 6
L6SendLoss = L6ChangeLoss #Line 6
if (L6ChangeGood <0): #Line 6
if int(m) < 33 and int(m) > 30: #Line 6
L6SendGood = L6EndGood #Line 6
else: #Line 6
L6SendGood = 0 #Line 6
else: #Line 6
L6SendGood = L6ChangeGood #Line 6
if (L7ChangeGood <0): #Line 7
if int(m) < 33 and int(m) > 30: #Line 7
L7SendGood = L7EndGood #Line 7
else: #Line 7
L7SendGood = 0 #Line 7
else: #Line 7
L7SendGood = L7ChangeGood #Line 7
if (L8ChangeGood <0): #Line 8
if int(m) < 33 and int(m) > 30: #Line 8
L8SendGood = L8EndGood #Line 8
else: #Line 8
L8SendGood = 0 #Line 8
else: #Line 8
L8SendGood = L8ChangeGood #Line 8
if (L3BottleG == 0):
RunningBottle = "Heinz"
elif (L3BottleG == 2):
RunningBottle = "Hershey"
elif (L3BottleG == 3):
RunningBottle = "RedGold"
else:
RunningBottle = "128oz Arizona"
print(L3BottleG,RunningBottle)
#--------------------------------------------------------------------------------------
parameters = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0003', # line code for line 3
'start': startingTime, # start time of data being collected
'end': currentTime, # end time of data being collected
'actual': L3SendGood, # number of good bottles going to
'scrap': L3SendLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 2, # number of operators on the line. usually one but have to ask if this is needed
'productcode': RunningBottle, # this will be where we put the name of product being produced. if needed.128oz Arizona Heinz
}
parametersL5 = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0005', # line code for line 5
'start': startingTime, # start time of data being collected
'end': currentTime, # end time of data being collected
'actual': L5SendGood, # number of good bottles going to
'scrap': L5SendLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 1, # number of operators on the line. usually one but have to ask if this is needed
'productcode': 'Bottle', # this will be where we put the name of product being produced. if needed.
}
parametersL1 = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0001', # line code for line 1
'start': startingTime, # start time of data being collected
'end': currentTime, # end time of data being collected
'actual': L1SendGood, # number of good bottles going to
'scrap': L1SendLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 1, # number of operators on the line. usually one but have to ask if this is needed
'productcode': 'Old Country', # this will be where we put the name of product being produced. if needed.
}
parametersL2 = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0002', # line code for line 2
'start': startingTime, # start time of data being collected
'end': currentTime, # end time of data being collected
'actual': L2SendGood, # number of good bottles going to
'scrap': L2SendLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 1, # number of operators on the line. usually one but have to ask if this is needed
'productcode': '128oz Arizona', # this will be where we put the name of product being produced. if needed.
}
parametersL7 = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0007', # line code for line 7
'start': startingTime, # start time of data being collected
'end': currentTime, # end time of data being collected
'actual': L7SendGood, # number of good bottles going to
'scrap': L7ChangeLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 1, # number of operators on the line. usually one but have to ask if this is needed
'productcode': L7Bottle, # this will be where we put the name of product being produced. if needed.
}
parametersL8 = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0008', # line code for line 7
'start': startingTime, # start time of data being collected
'end': currentTime, # end time of data being collected
'actual': L8SendGood, # number of good bottles going to
'scrap': L8ChangeLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 1, # number of operators on the line. usually one but have to ask if this is needed
'productcode': L8Bottle, # this will be where we put the name of product being produced. if needed.
}
parametersL6 = {
'site': 1, # the site code for site 0123 (bradford) is 1
'linecode': '0123-BLWM-0006', # line code for line 7
'start': 'startingTime', # start time of data being collected
'end': 'currentTime', # end time of data being collected
'actual': L6SendGood, # number of good bottles going to
'scrap': L6ChangeLoss, # This will be determined as to what numbers i really need. I assume log count less final count.
'operation_count': 1, # number of operators on the line. usually one but have to ask if this is needed
'productcode': "L6Bottle", # this will be where we put the name of product being produced. if needed.
}
if (L3SendGood == 0) and (L3SendLoss == 0):
print("Zero numbers no Send Line 3") #This is just for my entertainment
else:
print("sending L3")
three = requests.post(url, proxies=proxies, data=parameters,verify=False,timeout=out-time) #line actually making the call to upload the log
#print(three)
L3list = three.text
if L3list.find("true") != -1:
L3Startgood = L3EndGood #RT is running total. this will be for bottle count correction
L3Startlog = L3EndLog
print("L3 good")
else:
print("Number didn-t go through. Will not update starting numbers.")
if (L5SendGood == 0) and (L5SendLoss == 0):
print("Zero numbers no Send Line 5")
else:
print("sending L5")
five = requests.post(url, data=parametersL5,verify=False,timeout=out-time)
#print(five)
L5list = five.text
if L5list.find("true") != -1:
L5Startgood = L5EndGood
L5Startlog = L5EndLog
print("L5 good")
else:
Print("L5 numbers didn't go through. Will not update starting numbers.")
if (L1SendGood == 0) and (L1SendLoss == 0):
print("Zero numbers no Send Line 1")
else:
print("sending L1")
one = requests.post(url, data=parametersL1,verify=False,timeout=out-time)
#print(one)
L1list = one.text
if L1list.find("true") != -1:
L1Startgood = L1EndGood
L1Startlog = L1EndLog
print("L1 good")
else:
print("L1 numbers didn't go through. Will not update starting numbers.")
if (L2SendGood == 0) and (L2SendLoss == 0):
print("Zero numbers no Send Line 2")
else:
print("sending L2")
two = requests.post(url, data=parametersL2,verify=False,timeout=out-time)
#print(two)
L2list = two.text
if L2list.find("true") != -1:
L2Startgood = L2EndGood
L2Startlog = L2EndLog
print("L2 good")
else:
print("L2 numbers didn't go through. Will not update starting numbers.")
if (L7SendGood == 0) and (L7ChangeLoss == 0):
print("Zero numbers no Send Line 7")
else:
print("sending L7")
seven = requests.post(url, data=parametersL7,verify=False,timeout=out-time)
#print(seven)
L7list = seven.text
if L7list.find("true") != -1:
L7Startgood = L7EndGood
L7StartlogT = L7EndLogT
print("L7 good")
else:
print("L7 numbers didn't go through. Will not update starting numbers.")
if (L8SendGood == 0) and (L8ChangeLoss == 0):
print("Zero numbers no Send Line 8")
else:
print("sending L8")
eight = requests.post(url, data=parametersL8,verify=False,timeout=out-time)
#print(eight)
L8list = eight.text
if L8list.find("true") != -1:
L8Startgood = L8EndGood
L8StartlogT = L8EndLogT
print("L8 good")
else:
print("L8 numbers didn't go through. Will not update starting numbers.")
if (L6SendGood == 0) and (L6SendLoss == 0):
print("Zero numbers no Send Line 6") #This is just for my entertainment
else:
print("sending L6")
six = requests.post(url, data=parametersL6,verify=False,timeout=out-time) #line actually making the call to upload the log
#print(six)
L6list = six.text
if L6list.find("true") != -1:
L6Startgood = L6EndGood
L6Startlog = L6EndLog
print("L6 good")
else:
print("Number didn't go through. Will not update starting numbers.")
except Exception as e:
print(e)

Eloquent sum of units within duration from date

I have a table (projects) which contains a start_date and duration (in weeks) and a number (progress_pw) as to how much progress units can be achieved per week
projects
+----+----------+--------+-----------+
| ID |start_date|duration|progress_pw|
+----+----------+--------+-----------+
| 1 |2018-06-15| 2 | 500 |
| 2 |2018-06-19| 4 | 120 |
I want to get a summary by week of the total number of progress units expected to be used in that given week.
Eg:
*In the week starting Monday 11 June project.id 1 is expected to consume 500 units
*In the week starting Monday 18 June project.id 1 is expected to consume 500 units & project.id 2 is expected to consume 120 units with a total consumption of 620 units.
*In the week starting 2018-12-01 (sometime in the future) there are no active projects so 0 units consumed.
$i=0;$weeks=12;
while ($i < $weeks) {
$thisweek = Carbon::now()->addWeeks($i)->startOfWeek()->format('Y-m-d');
$requiredcap = DB::table('projects')->select(DB::raw("sum(progress_pw) as progress"))
->where('install_date', ">=", $thisweek) //<<< This is where im getting stuck!
->get();
$capacity['label'][] = Carbon::now()->addWeeks($i)->startOfWeek()->format('d M');
$capacity['required'][] = $requiredcap;
$i++;
}
Any pointers of the logic behind this one?
Try this:
$i = 0; $weeks = 12;
while ($i < $weeks) {
$start = Carbon::now()->addWeeks($i)->startOfWeek();
$end = (clone $start)->addDays(6);
$requiredcap = DB::table('projects')
->select(DB::raw('sum(progress_pw) as progress'))
->whereBetween('start_date', [$start->toDateString(), $end->toDateString()])
->orWhere(function ($query) use ($start, $end) {
$query->where('start_date', '<', $start->toDateString())
->where('end_date', '>=', $start->toDateString());
})->first();
$capacity['label'][] = $start->format('d M');
$capacity['required'][] = $requiredcap->progress;
$i++;
}

Round a number down (not half down)

I'm looking for a round mode like PHP_ROUND_HALF_DOWN but always down :
$number = 76.5 // I want 76
$number = 76.6 // I want 76
$number = 77.0 // I want 77
$number = 0.0165 // I want 0.0160
$number = 0.0166 // I want 0.0160
$number = 0.0170 // I want 0.0170
There is a solution ?
You can use the floor function, e.g.:
floor(76.5) = 76
But for rounding decimals to a decimal place you will need to multiply/divide, e.g.:
floor(0.0165 * 1000) / 1000 = 0.016
http://php.net/manual/en/function.floor.php

How to create a script that would calculate the difference between samples in a list?

I am trying to create a script that would calculate the difference between samples in a list.
If we take this example:
- result1 = 33
- result2 = 45
- result3 = 66
- result4 = 47
- result"n" = 50
The calculus should start at the second result from the list and descend until the last result, and then sum up those results:
result2 - result1 = 12,
result3 - result2 = 21,
result4 - result3 = 19,
result"n" - result4= 3
sum = 12 + 21 + 19 + 3 = 55
I am new at scripting, and so far i only came up with this solution:
$numbers
$1=[math]::abs($numbers[0]-$numbers[1])
$2=[math]::abs($numbers[1]-$numbers[2])
$3=[math]::abs($numbers[2]-$numbers[3])
$4=[math]::abs($numbers[3]-$numbers[4])
write-host "the results = $1, $2, $3, $4"
$sum = $1 + $2 + $3 + $4
The problem is that the list is dynamic and changes in length, one time there are 10 results and one time 20 for example.
I found a similar question here, but i don't know how to implement the solution to my case, as that is too complicated for me.
What you need is a For loop. It is structured as such:
For(<initial declaration, usually a start point like $i = 0>; <Condition to stop when false>;<Action to perform on each iteration to progress loop>){
Code to perform on each loop
}
For you we would do something like:
For($i=1;$i -le $numbers.count;$i++)
That starts 1, and since arrays start at 0 this will get you going with the second record. Then in the scriptblock we do something like:
{
[array]$Results += [math]::abs($numbers[$i] - $numbers[($i-1)])
}
That will get the differences for you, then to display them you can do something like:
"the results = " + ($Results -join ", ")
$sum = $Results|Measure -sum|select -expand Sum
So you put that all together and get
For($i=1;$i -le $numbers.count;$i++){
[array]$Results += [math]::abs($numbers[$i] - $numbers[($i-1)])
}
"the results = " + ($Results -join ", ")
$sum = $Results|Measure -sum|select -expand Sum
Use a for loop, use the length of your $numbers array to know when to stop.
$numbers = #(33,45,66,47,50)
$sum = 0
for($cur=1;$cur -lt $numbers.Length; $cur += 1){
$sum += [math]::abs($numbers[$cur]-$numbers[$cur-1]);
}
$sum

Perl recursion help

I'm trying to write a program that will calculate the dividends of stocks. I did this without a subroutine. Right now, I'm trying to modify it so it can run using a recursive routine. Any help with this? Because I'm not so good at this.
Here's the original script + a pathetic attempt.
print "A stock xyz's price is now $100. It has 3.78% dividend. You have 1000 of it and reinvest the dividend into the stock.\n";
my %hash;
#stocknum = 1000;
#dividend = 6780;
while ($#dividend != 20) {
$a = $dividend[-1];
$stock = $stocknum[-1];
$div_total= $stock*100*0.0678;
$stock_total = $stock + int($a/100);
push (#stocknum, $stock_total);
push (#dividend, $div_total);
if ($#dividend == 20) {
last;
}
}
shift (#dividend);
$stock_num = $stocknum[-1];
$div = $stock_num*100*0.0678;
push (#dividend, $div);
#hash{#stocknum} = #dividend;
foreach $key(sort keys %hash) {
print "Stock number: $key\t"."Dividend: $hash{$key}\n";
}
$dividend=0.0378;
I don't think you want recursion. I think you just want to loop over the number of cycles of payouts that you're after. It looks like you're getting all mixed up with arrays for some reason.
print <<'HERE';
A stock xyz's price is now $100. It has 6.78% dividend.
You have 1000 of it and reinvest the dividend into the stock.
HERE
my $shares = 1000;
my $price = 100;
my $dividend = 6.78 / 100;
my $cycles = $ARGV[0] || 20;
foreach ( 1 .. $cycles ) {
local $cycle = $_;
local $payout = $shares * $dividend * $price;
local $new_shares = $payout / $price;
write();
$shares += $new_shares;
}
format STDOUT =
#### #####.###### ######.####### ###.###### #####.######
$cycle, $shares, $payout, $new_shares, $shares+$new_shares,
.
format STDOUT_TOP =
###.####%
$dividend
Cycle Shares Payout New Shares Total Shares
----------------------------------------------------------------
.
This gives me the output:
A stock xyz's price is now $100. It has 6.78% dividend.
You have 1000 of it and reinvest the dividend into the stock.
0.0678%
Cycle Shares Payout New Shares Total Shares
----------------------------------------------------------------
1 1000.000000 6780.0000000 67.800000 1067.800000
2 1067.800000 7239.6840000 72.396840 1140.196840
3 1140.196840 7730.5345752 77.305346 1217.502186
4 1217.502186 8254.6648194 82.546648 1300.048834
5 1300.048834 8814.3310942 88.143311 1388.192145
6 1388.192145 9411.9427423 94.119427 1482.311572
7 1482.311572 10050.0724603 100.500725 1582.812297
8 1582.812297 10731.4673731 107.314674 1690.126971
9 1690.126971 11459.0608610 114.590609 1804.717579
10 1804.717579 12235.9851873 122.359852 1927.077431
11 1927.077431 13065.5849830 130.655850 2057.733281
12 2057.733281 13951.4316449 139.514316 2197.247597
13 2197.247597 14897.3387104 148.973387 2346.220985
14 2346.220985 15907.3782750 159.073783 2505.294767
15 2505.294767 16985.8985220 169.858985 2675.153752
16 2675.153752 18137.5424418 181.375424 2856.529177
17 2856.529177 19367.2678194 193.672678 3050.201855
18 3050.201855 20680.3685775 206.803686 3257.005541
19 3257.005541 22082.4975671 220.824976 3477.830517
20 3477.830517 23579.6909021 235.796909 3713.627426
Don't worry about my use of format; I've had that on the brain this weekend since I rewrote some perlfaq stuff about it then also turned it into Use formats to create paginated, plaintext reports. You could just as easily created the output with printf:
print <<'HERE';
A stock xyz's price is now $100. It has 6.78% dividend.
You have 1000 of it and reinvest the dividend into the stock.
Cycle Shares Payout New Shares Total Shares
----------------------------------------------------------------
HERE
my $shares = 1000;
my $price = 100;
my $dividend = 6.78 / 100;
my $cycles = $ARGV[0] || 20;
foreach ( 1 .. $cycles ) {
my $payout = $shares * $dividend * $price;
my $new_shares = $payout / $price;
printf "%4d %12.6f %12.6f %10.6f %12.6f\n",
$_, $shares, $payout, $new_shares, $shares + $new_shares;
$shares += $new_shares;
}
As a side note, you really don't ever want recursion, and especially not in Perl if you can help it. Other languages get away with it because they know how to unroll your recursion to turn it into an iterative process. Perl, being a dynamic language, can't really do that because it doesn't know if the subroutine will have the same definition on the next go around. It's nice as a computer science topic because it makes the programming marginally easier and they know it all works out in the end. I think I talk about this in Mastering Perl somewhere, but Mark Jason Dominus covers it extensively in Higher-Order Perl. Basically, instead of recursion you use a queue, which is a better skill to practice anyway.