Jump to content

Making a variable stay set???


gsaldutti

Recommended Posts

I have a page that loads and declares a certain variable ($isfile) as being equal to "" (just to declare it).  Then there is a script further down on the page that might be initiated by the user and if it is, that script sets the $isfile variable equal to "yes".

 

Then there is another script for another part of the page that loads the entire page again, so it always sets that $isfile variable back to "".

 

However, once it is set to "yes", I would like it to stay set to "yes". 

 

How can I make that happen?

Link to comment
https://forums.phpfreaks.com/topic/132955-making-a-variable-stay-set/
Share on other sites

In the script that resets the $isfile variable, check to see if the its already to set to yes before setting it to ""

 

if(isset($isfile) && $isfile != 'yes')
   $isfile = '';

 

Now the $isfile variable shouldn't overwritten if its set to 'yes'.

 

This will only apply to current page. If you want variables to span over multiple pages then use sessions as suggested.

 

 

I think those suggestions will work, thanks guys.  But to answer the question of why I keep reloading the same page...

 

It's basically a page where users can upload 10 different images.  I want them to be able to do it all on the same page rather than have to upload image #1 and then go to another page to do image #2 and then to another page to do image #3 and so on.  So I have a bunch of forms that upload images and they all call the same action (i.e. the current page).

 

I'm open to ideas to do it differently but it seems like if I want them to be able to upload them all from the same page, then I will have to keep calling the same page, right?

 

I'm new to this so I very easily could be missing something obvious.

if you name your file upload fields as an array in your form, eg

<input type="files[]" ... />

 

Then you should be able to use a foreach loop to loop through all the uploaded files

 

foreach($_FILE['files'] as $file)
{
     // process uploaded
     echo '<pre>'.print_r($file, true).'</pre>';
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.