sf_guy Posted November 9, 2012 Share Posted November 9, 2012 I searched for this but couldn't find a definitive answer: Are session variables available immediately? For example, if I have <head> <script type="text/javascript"> var stopvalue = <? PHP echo $SESSION['maxrows'] ?>; function validate() { for (x=0; x<stopvalue, x++) { // do something } } </script> </head> and then, in the <body> on the same page have $SESSION['maxrows']=db->Recordcount(); Will 'maxrows' be visible to the 'validate' function or not? Thanks, everyone on this forum has been very helpful to this PHP beginner. I know I could just try it and see if it works, but I don't want to go down the path of develping an entire page only to find it won't work (and if it won't work, what's a good alternative?) Link to comment https://forums.phpfreaks.com/topic/270504-availability-of-session-variables/ Share on other sites More sharing options...
JonnoTheDev Posted November 9, 2012 Share Posted November 9, 2012 Simple answer, no. A script runs from top to bottom. You could get the session value into your javascript by using ajax to make a server side call. You will need the call to be made only after you have set the session value. Im guessing that your javascript is event driven such as a form being submitted, so it can make the call at that point. Link to comment https://forums.phpfreaks.com/topic/270504-availability-of-session-variables/#findComment-1391328 Share on other sites More sharing options...
sf_guy Posted November 9, 2012 Author Share Posted November 9, 2012 Thanks, that's what I thought. Not quite ready for ajax yet. Is there a "best practice" way to tell the last id when writing multiple items using PHP? For example, one user may have startdate1, startdate2, and startdate 3 but another user could conceivably have all the way through startdate150. I'm thinking of using a test to see if doc.getelementbyID(startdate + variable) is null and then breaking out of the loop if so. Is that a good way to do it? Link to comment https://forums.phpfreaks.com/topic/270504-availability-of-session-variables/#findComment-1391371 Share on other sites More sharing options...
JonnoTheDev Posted November 12, 2012 Share Posted November 12, 2012 If you are talking about the last mysql database primary key then you can use mysql_insert_id() after you run your insert query. To obtain the last id for a specific result you could use the MAX statement in a query i.e mysql_query("SELECT MAX(fieldname) AS last_id FROM table WHERE user_id=1"); Link to comment https://forums.phpfreaks.com/topic/270504-availability-of-session-variables/#findComment-1391797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.