Understanding how sessionlist example works

Topics: Developer Forum, User Forum
Sep 21, 2012 at 1:05 PM

Hi,

I have taken one of the activeVFP examples, copied it and tried to use my own data but having a few problems.  I think it is more a problem of understanding how it works as anything. 

I copied the program sessionlist.htm to stksearch.htm and copied in a table called cname.dbf with fpt and cdx files.  Changed all references in stksearch to use my table and fields.  Got the main web page to point to stksearch and all seems to work as the table shows the correct data from the 1st record.  It goes wrong when you search for something. 

There are 77k records in the stock file and I put in a code, say MJK and got it to show it found say 7k records, but the table still shows the main stock file data starting at 1st record B..  If I then click on the next page etc the found records goes back up to 77k and it just shows the next set of data still starting with B.

I have looked at the page source for the "next" button, which is:

<a href="/activevfp/Default.aspx?action=stksearch&nav=Next&page=3">Next</a>

Looking in Default.aspx, this calls
Response.Write(x.Process())

I think I have changed all the references I need to in stksearch
but not sure if there is anything else that needs changing.
When you run the main sessionlist example it seems to work as expected.

Any help appreciated.

Cheers,

Peter
Coordinator
Sep 21, 2012 at 9:51 PM
Edited Sep 21, 2012 at 9:54 PM

For 5.51, everything is in sessionlist.htm.  So the code in that is as follows:

*sql
   lcOrder=""
   DO CASE
   CASE oRequest.querystring("order")=="company"
    lcOrder= "2" && company order
   CASE oRequest.querystring("order")=="country"
    lcOrder = "country" && country order
   OTHERWISE
     lcOrder=oSession.VALUE("thissort")
     IF (ISNULL(lcOrder) .OR. EMPTY(lcOrder))
      lcOrder = "2"  && company order default
     ENDIF
   ENDCASE
   * save order to session
   oSession.VALUE("thissort",lcOrder)
  
   lcCompany = UPPER(NVL(oRequest.Form("txtCompany"),[]))
   lcWhere = ""
   IF !EMPTY(lcCompany)
     lcWhere = "WHERE UPPER(company) = lcCompany"
   ENDIF

SELECT * FROM customer &lcWhere ;
   INTO Cursor tCursor ;
   ORDER BY &lcOrder

If you want to save something to the Session object to use in the search, do this:

lcOrder=oSession.VALUE("thissort")
     IF (ISNULL(lcOrder) .OR. EMPTY(lcOrder))
      lcOrder = "2"  && company order default
     ENDIF
* save order to session
   oSession.VALUE("thissort",lcOrder)

and then use that in the SQL select
Sep 24, 2012 at 5:19 PM

Thanks for the tip.   Been doing a lot more testing and found out the original sessionlist has the same behaviour.  I would call it a bug as don't think it should work like that.  There are 92 entries in the customer table, so I duplicated one of the companies that begin with F, just so it would find more than 10 results in the search.  If you then did a search for F and found 16 entries.  If you click next or the "2" page it then reverts back to 100 entries (I.e. the full list).  I would have expected to be able to click next/previous just on the search results.   Looks like I hadn't done it wrong after all.

Will see if I can figure out how to get it working properly.

Cheers,

Coordinator
Sep 24, 2012 at 9:12 PM

That's why you have to save whatever search or order you want to the Session object.  In the example, I'm only saving ORDER to the Session object.  You will need to save whatever the SEARCH is to the session object.  Then, whenever it hits the page, the SQL will have a 'where &lcSearch'  just like it has 'ORDER BY &lcOrder' right now. 

All of this is just regular FoxPro. You just need to understand the web objects: Session, Request, Response and the fact that each hit will start with a blank slate as far as variables unless you use the Session object (or something else) to retrieve them.  You might want to take a look at the FAQ if you haven't already.

Sep 25, 2012 at 11:09 AM

I did read the FAQ but didn't fully get what it meant by a clean slate.  So if I understand this correctly (from a fox POV), when you do a search and find say 15 records you create your first page of results (10 items) using a select and show "next" and "2" links to the next page and save the order and number of matches or similar to the session object.  If you click on page 2, it has to run the select again, but this time move to record 11 and show 11-15 with "Previous" and "1" links?

Coordinator
Sep 25, 2012 at 3:50 PM
Edited Sep 25, 2012 at 3:53 PM

Ok, I redid the example on the demo site (my local site).  Here's the new main code:

 

<% *Settings
   lnTotPerPage  =3       && total records displayed per page
   lnpagenumbers =5         && how many page numbers to show
   lnStart=VAL(oRequest.querystring("page"))  && what page number are we on
   lcButton=oRequest.querystring("nav")       && which way are we going: next, previous, first, last
*If RESET button was pressed, clear Session
   IF oRequest.querystring("action")=="reset"
         oSession.VALUE("thissort","")
         oSession.VALUE("thiscompany","")
   ENDIF
   lcOrder=""
   DO CASE
   CASE oRequest.querystring("order")=="company"
    lcOrder= "2" && company order
   CASE oRequest.querystring("order")=="country"
    lcOrder = "country" && country order
   OTHERWISE
     lcOrder=oSession.VALUE("thissort")
     IF (ISNULL(lcOrder) .OR. EMPTY(lcOrder))
      lcOrder = "2"  && company order default
     ENDIF
   ENDCASE
   * save order to session
   oSession.VALUE("thissort",lcOrder)

   *company
   lcCompany = UPPER(NVL(oRequest.Form("txtCompany"),[]))
   IF EMPTY(lcCompany)
      lcCompany=oSession.VALUE("thiscompany")
   ENDIF
   * save company to session
   oSession.VALUE("thiscompany",lcCompany)
  
   lcWhere = ""
   IF !(EMPTY(lcCompany) .OR. ISNULL(lcCompany))
     lcWhere = "WHERE UPPER(company) = lcCompany"
   ENDIF
*sql
SELECT * FROM customer &lcWhere ;
   INTO Cursor tCursor ;
   ORDER BY &lcOrder
  
*create page numbers
 START=0
 lnPageMax = 0
 lnPageBegin = 0
 lnRowCount = RECCOUNT()
 SET PROC to oProp.AppStartPath+'prg\pages' ADDITIVE   && Make sure you use ADDITIVE or bad things happen!
 lcPages= pages(lnTotPerPage,lnpagenumbers,lnStart,lcButton,lnRowCount)
       
*Write Form, Table Headers, Table Records
%>


To see it in action go here:
http://68.100.248.108:444/avfp5.61Demo/sessionlist.avfp

To see the complete source code, go here:
http://68.100.248.108:444/avfp5.61Demo/showhtmlsource.avfp?file=sessionlist

Now if you do a search on "B" it will give you 2 pages and only 2 pages even as you go to the next page. This is because I saved the
SEARCH to the Session object. Just hit the RESET button to clear the Session and use the defaults meaning show all records.

Sep 25, 2012 at 4:36 PM

Excellent.  Was just looking at some ASP tutorials and finding out how forms work etc.  Think I'm getting my head round it.

Thanks,