ASP/SQL/Paging - Get current page number - mysql

I answered my own question, sorry!
I started with this:
If Request("currentPage") <> "" AND isNumeric(Request("currentPage")) Then
currentPage = Request("currentPage")
Else
currentPage = 1
End If
And then on my paging links, I added:
.asp?currentPage="&currentPage+1&"
///////////////////////////////////////////////////////////////////
I have upgraded my search script to use LIMIT paging.
I am simply trying to work out what the current page is, based on the 'offset' value but I'm having some amateur troubles.
This is what I have:
currentPage = pageRange / rpp
' page range is the offset value for the LIMIT clause, for example 0, 20, 40...
' rpp is results per page value, for example 20...
If cInt(currentPage) <= 0 Then
currentPage = 1
End If
But page 1 and 2 both show "page 1", page 3 shows "page 2", page 4 shows "page 3" and so on. Obviously there is something wrong with this calculation but I cannot see it.
Any ideas?

May be better to keep track on page num and evaluate offset from it, than tracking offset? Or at least evaluate currentPage as cInt(pageRange / rpp)+1

Related

Difference between two time values in SSRS report

I have the following select statement:
SELECT [TR_DATE]
,[TR_TIME]
,RIGHT('000000' + CONVERT(varchar,TR_TIME), 6)
,TIMEFROMPARTS(SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),1,2), SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),3,2), SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),5,2),0,0) as trtime
,[ET_TYPENO]
,[ET_NAME]
FROM [DB400].[dbo].[TRANSACTIONREPORT]
where DEPT_NAME = 'GIJIMA AST HOLDINGS (PTY) LTD'
/*and LOC_NAME = 'Front Door and Gate'*/
and TR_DATE between '20200120' and '20200130'
order by TR_DATE, MST_LASTNAME, MST_FIRSTNAME, TR_TIME
which returns the following data:
I would like to use this to calculate the time the was spent in the building.
Note that ET_TYPENO = 1 equates to "Allowed Normal In" and ET_TYPENO = 2 to "Allowed Normal Out"
I have the following expression inside an ssrs report
=iif(Fields!ET_TYPENO.Value=2,
DateDiff(DateInterval.Hour, previous(Fields!trtime.Value),Fields!trtime.Value),
"")
But it resolves to the following #Error.
UPDATE
Expected Result
Calculate the time difference between the "Allowed Nornmal In" event and the "Allowed Normal Out" event.
Take line 2 and 3 for example. "Allowed Nornmal In" occured at 07:23:19 and "Allowed Normal Out" occured at 08:55:48. I need it to return the time difference between these two times. I.E. 1:32:29.
The trtime needs to be converted to a DATE type with the CDATE() function to use the DATEDIFF function.
=IIF(Fields!ET_TYPENO.Value = "OUT",
DateDiff(DateInterval.Hour, CDATE(previous(Fields!trtime.Value)), CDATE(Fields!trtime.Value)),
NOTHING)
Also, shouldn't Fields!ET_TYPENO.Value = 1 ?
Taking the suggestion from Honnover Fist's answer that the parameter of the DateDiff need to be Dates instead of Times. (The Documentation does say that a Time will work). I have changed my query to a DATETIMEFROMPARTS() instead of a TIMEFROMPARTS().
Then I changed the condition to the below code so that the hours would be displayed more accurately.
=IIF(Fields!ET_TYPENO.Value = 2,
DateDiff(DateInterval.Minute, previous(Fields!TR_DATETIME.Value), Fields!TR_DATETIME.Value) / 60,
NOTHING)

Rails Active Record .paginate, Get First page(row), if page (row) specified doesn't exist in database table

In rails 4.2, I am using paginate method to get paginated records from db. I want to get first page, if specified page is not present in the db.
I am using following active record functionality to fetch paged records from database. So If the page number is mentioned 2 and there is only one row in db, the query returns null.
#records = #user.where("some where clause here").paginate(per_page: 1, page: params[:page].presence || 1).
I want to fetch first record if specified page doesn't exist in db. It fetches first record, if params[:page] is empty, but if database doesn't have specified page, it returns nill.
I think this is what you need:
#records = #user.where(title: 'bla').paginate(per_page: 1, page: params[:page].presence || 1).
#results = #records.empty? ? #user.where(title: 'bla').paginate(per_page: 1, page: 1) : #records
This will mean if your #records instance variable has any records it will be returned in #results, and if it doesn't your app will return the first page of results. To make it a bit more readable:
#first_page_records = #user.where(title: 'bla').paginate(per_page: 1, page: 1)
#custom_page_records = #user.where(title: 'bla').paginate(per_page: 1, page: params[:page].presence || 1)
#results = #custom_page_records.empty? ? #first_page_records : #custom_page_records
Although My answer is similar to one posted by Mark. But I still posted it, as it will run an extra query only if original query returns empty records.
#records = #users.valid_users_with_invalid_msg.paginate(per_page: 1, page: params[:page].presence || 1)
if #records.empty?
#records = #users.valid_users_with_invalid_msg.paginate(per_page: 1, page: 1)
end
Note: I have moved the where clause in scope

GetelementsByTagName seems to not work properly

this question sounds stupid but how come when I use the function GetElementsByTagname("frame") , it only returns 3 as a length and not 5 as I expected ?
Here is the HTML of the webpage where I counted 5 times the apparition of the tagname "frame" but when I ask for the length in VBA I get 3...
My observations :
1) You can see that 3 is the number of main frames (top_navigation, contentframe, dummyframe)
2) If I try to access to one of the mainframes via getelementbyname, it works but if I try to access on the the subframes of contentframe ( leftnavigation or postfachcontent) it doesn't work ( 0 item detected)
Here is my code :
Dim Frame As IHTMLElementCollection
Set Frame = IEDoc.getElementsByName("contentframe") ' this works and returns 1 item
MsgBox Frame.Length
Set Frame = IEDoc.getElementsByName("postfachcontent")
MsgBox Frame.Length ' this returns 0 item
Dim Collection As IHTMLElementCollection
Set Collection = IEDoc.getElementsByTagName("frame")
MsgBox Collection.Length ' this returns 3 and I expected 5...
Only 3 frames are on that page, the rest are inside an embedded html frame which getElementsByTagName cannot access as it is a different DOM tree.

Display selected amount of records from database

I need a page that displays records from a database, sorted by their jobs. So the database holds different kind of persons with different jobs. For example, on the page "teacher" I just want to display all the teachers, not the other persons. I want to have 3 persons on a page and a button "previous" and "next" beneath it. If an user clicks the next-button I want the next 3 records to be shown. When the user reaches the last records, I need the next-button to disappear. Same goes for "previous".
What I have so far:
This piece of code creates a value named startrow.
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//give the value of the starting row 0 because nothing was found in URL
$startrow = 0;
//otherwise take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
The query I have:
$sql = mysql_query("SELECT * FROM $tbl_name WHERE jobs='teacher' LIMIT $startrow, 3")or
die(mysql_error());
$sql2 = "SELECT COUNT(*) AS TotalJobs FROM $tbl_name WHERE jobs='teacher'";
$result_count = mysql_query($sql2);
$count = mysql_fetch_array($result_count);
I created the $count for the if / else function in the next part (the part that doesn't seem to work the way I want it to):
if ($startrow < $count )
echo 'Next';
$prev = $startrow - 3;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
echo 'Previous';
So the previous-button seems to work the way it should. The next doesn't. I've created the
if ($startrow < $count)
so basicly, if the value of startrow is smaller than the total number of records, it puts a next-button. But if I test this, it displays the next-button anyhow, no matter the value of startrow.
What am I missing here?
mysql_fetch_array returns an array containing the count, not the value itself. Use $count['TotalJobs'] instead.
Also, you shouldn't use the mysql extension anymore, instead use PDO or MySQLi.
Edit
You can change:
$count = mysql_fetch_array($result_count);
to
$countArr = mysql_fetch_array($result_count);
$count = $countArr['TotalJobs'];

MySQL/ASP Paging Using Limit

Currently, I am using this recordset paging method:
(this example has been cut down for this post)
Function getnext10(num)
pageLen = len(num)
if pageLen = 1 Then
next10 = 10
else if pageLen > 1 Then
pageRem = 10
pageTen = right(num, 1)
next10 = num + pageRem - pageTen
end if
end if
getnext10 = next10
End Function
Function getPrev10(num)
pageLen = Len(num)
If pageLen = 1 Then
prev10 = 1
ElseIf pageLen > 1 Then
lastNumber = Right(num,1)
prev10 = num - lastNumber
End If
If prev10 = 0 Then
prev10 = 1
End If
getPrev10 = prev10
End Function
Which uses a 'Do Until Loop' to get the correct group of results for that page.
I have been advised to drop this method and to start using LIMIT paging instead. I understand how to apply this to my project, I think, so I'm not looking for anybody's code, I just need an explanation as to why I need to start using it.
Apart from using less code in the LIMIT version, which will reduce my page size (mega slightly), why is using the LIMIT method better? Will it improve my search query performance?
Any further clarification on this subject will be gratefully received.
Yes, using LIMIT would improve the perfomance of your webpage. Right now you're fetching unnessesary records just to move onto right record while LIMIT would land you onto right record right away. Lets say you have to page 100 records, 10 records per page, and user request page 10 (last page). With the "Do Until Loop" solution you're fetching all 100 records but you discard 90 of them right away, without actually using them. So 90% of the resultset you didn't use for anything constructive! Using LIMIT you fetch only the records you going to use and thus you aren't wasting any resources.