#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Best Practice - Page built from multiple lookups


    Having recently started to work with PHP, I am having trouble with best practice for a page built up from multiple lookups. I think my history with other environments is getting in the way.

    The main page is easy to display, the underlying table is a list of indexes to related tables. The problem is adding a new row or making modifications to that table.

    What I would like to do is display a page with empty data fields but links to lookup pages. The user clicks on the link, selects the item on the lookup page, clicks the select button, and is returned to the main page with data filled in. This is fairly easy to do for the first lookup. The problem is that the user needs to make two other selections from different tables/pages. Since PHP is stateless I'm having trouble keeping the previous selections with each new selection. I have been playing with multiple post values but the code it getting very complicated (ugly) and I have just decided I have programmed myself into a box.

    How would you solve such a problem?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,538
    Rep Power
    595
    I'd probably use sessions.

    Comments on this post

    • swestfisher agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    That might work, but there are is a lot more going on in this system that just these pages. Would this really be best practice?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    not sure what your worries are. Storing three IDs or arrays/objects is laughable compared to what other applications do with sessions.

    This is a classical use case for sessions.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    not sure what your worries are. Storing three IDs or arrays/objects is laughable compared to what other applications do with sessions.

    This is a classical use case for sessions.
    Two votes is plenty good enough for me.
    My problem was that I had looked at sessions and had thought it wasn't appropriate for what I see as a global variable. I've been programming for a long time and had often run across code that the developer should have known better. I wanted to make sure that if you were working on this code in the future you wouldn't think the programmer a complete idiot.
    Tomorrow I'll rewrite using sessions.
    Thanks!
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by swestfisher
    My problem was that I had looked at sessions and had thought it wasn't appropriate for what I see as a global variable.
    Well, the $_SESSION variable, which holds the session values, is global (superglobal, actually).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    Well, the $_SESSION variable, which holds the session values, is global (superglobal, actually).
    My previous message wasn't real clear. In most programming environments global variables are to be avoided if at all possible. Since my problem is only with four pages in a much larger system, I did not feel it appropriate to use a global variable accessible from all pages. Since two of you believe it is an appropriate use in PHP, I'll forge ahead.
    Thanks!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,538
    Rep Power
    595
    Originally Posted by swestfisher
    In most programming environments global variables are to be avoided if at all possible.
    Where did you ever get that idea? I have been programming for more than 40 years in many different environments and have never heard of that practice. What is the purported reasoning and why then do globals even exist? Perhaps you are thinking in terms of creating your own globals.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by gw1500se
    Where did you ever get that idea? I have been programming for more than 40 years in many different environments and have never heard of that practice. What is the purported reasoning and why then do globals even exist? Perhaps you are thinking in terms of creating your own globals.
    Structured programming theory frowns on using globals, I'm an old geezer too.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Superglobals like $_SESSION, $_POST, $_GET etc. are the foundation of PHP. This language wasn't designed with a theory or best practices in mind. Actually, it wasn't really designed at all, it grew naturally.

    If you don't like this programming style, then PHP might not be the right language for you. I'm serious. For people caring strongly about clean, structured code, PHP is pretty much the last language I'd recommend. Why did you choose it, by the way? Have you looked into other languages like Ruby or Python?

    Comments on this post

    • gw1500se agrees : Amen
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    Superglobals like $_SESSION, $_POST, $_GET etc. are the foundation of PHP. This language wasn't designed with a theory or best practices in mind. Actually, it wasn't really designed at all, it grew naturally.

    If you don't like this programming style, then PHP might not be the right language for you. I'm serious. For people caring strongly about clean, structured code, PHP is pretty much the last language I'd recommend. Why did you choose it, by the way? Have you looked into other languages like Ruby or Python?
    Whoa there, I'm aware of what you say. And I am not complaining about PHP. I was explaining my initial reluctance to use session variables for my problem. As I stated above, since I have multiple responses stating it is the proper way to solve my problem I am good with it.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by swestfisher
    Whoa there, I'm aware of what you say. And I am not complaining about PHP. I was explaining my initial reluctance to use session variables for my problem. As I stated above, since I have multiple responses stating it is the proper way to solve my problem I am good with it.
    Just wanted to thank you all, it was amazing how easy it was to implement using session variables.

IMN logo majestic logo threadwatch logo seochat tools logo