Jump to content

Recommended Posts

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?)

Edited by sf_guy
Link to comment
https://forums.phpfreaks.com/topic/270504-availability-of-session-variables/
Share on other sites

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.

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?

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");

Edited by neil.johnson
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.