global var returns to 0 after function call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

global var returns to 0 after function call

Post by LuaMadman »

HI, I have problem with global.
I declared $fileCount = 0; at the begining of the file.
then in a if(isset($_POST)) statment I call global $fileCount; and then $fileCount = $fileC;
$fileC is the local var. after that I called my function CreateImportButtons($fileC) in that function I did a print on $fileCount after calling global...
It returns 2 and the function creates my 2 buttons.
Then I click my button 1 of thoose 2, under that if(isset($_POST)) statement, I did print of $fileCount, now it's 0 again.
Why is that? Why is it reset to 0?
I'll post the relevant code bellow

Code: Select all

<!-- Simple form for uploading the file -->
<form enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" method="POST">
	<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
	Choose a file to upload (max 100KB): <input name="uploadedfile" type="file" /><br />
	<input type="submit" name="uploadbutton" value="Upload file" />
</form>

<?php

$fileCount = 0;

$target_path = 'tradera.csv'; //Save as tradera.csv in same dir
if (isset ($_POST['uploadbutton'])) {
	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
			print('The file '. basename( $_FILES['uploadedfile']['name']). 
			' has been uploaded<br/>');
			$file = dirname(__file__) . '/tradera.csv';
			$lines = count(file($file));
			if ($lines <= 15) { // Less then 15, Do one file
				print('There is less then 15 lines<br/>');
				rename(dirname(__file__) . '/tradera.csv',dirname(__file__) . '/tradera_1.csv');
				$fileC = 1;
			} elseif ($lines >= 15 && $lines <= 20) { // More then 15 but less then 20, one file
				print('There is between 15 and 20 lines<br/>');
				rename(dirname(__file__) . '/tradera.csv',dirname(__file__) . '/tradera_1.csv');
				$fileC = 1;
			} elseif ($lines > 20) { // More then 20 lines, do more file
				print('There is more then 20 lines<br/>');
				$orgFile = fopen($file,'r'); // Open file
				$c = 0; // Counter
				$fileC = 0; // File counter
				$newFileLines = ''; // empty line
				while(!feof($orgFile)) { // While not end of file
					$line = fgets($orgFile); // Get line
					$newFileLines = $newFileLines . $line; // Add to lines
					$c = $c+1; // Count +1
					//print('count: ' . $c . '<br/>');
					if ($c == 15 || feof($orgFile)) { // If count is 15 or file ends
						$c = 0; // count is reset to 0
						$fileC = $fileC+1; // File count +1
						$newFile = fopen(dirname(__file__) . '/tradera_' . $fileC . '.csv','w'); // Open a new file
						fwrite($newFile,$newFileLines); // Write new line data
						fclose($newFile); // close it
						$newFileLines = ''; // reset new file lines
					}
				}
				fclose($orgFile); // close file
				print($fileC . ' files was created<br/><br/>'); // report nr of files created
			}
			global $fileCount;
			$fileCount = $fileC;
			print($fileCount . ' modded!<br/>');
			CreateImportButtons($fileC);
	} else {
			print('There was an error uploading the file, please try again!<br/>');
	}
}

function CreateImportButtons($fileC) {
	?>
	<form enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" method="POST">
	<?php
	global $fileCount;
	print($fileCount . ' read @ buttons<br/>');
	for ($i=1; $i <= $fileC; $i++) {
		?>
		<input type="submit" name="importbutton" value="Import file <?php echo $i?>" />
		<?php
		print("&#160;&#160;&#160;&#160;&#160;&#160;&#160;");
	}
	?>
	</form>
	<?php
}

if(isset($_POST['importbutton'])) {
	print(substr($_POST['importbutton'],-1) . '<br/>');
	global $fileCount;
	print($fileCount . '<br/>');
	$fileC = $fileCount;
	CreateImportButtons($fileC);
}
?>
]
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: global var returns to 0 after function call

Post by lettie_dude »

2 things I notice straight away is you haven't declared the first $fileCount variable as global at the top of your script. That should be the only place you need to declare it as global.

Secondly everytime you submit the form it is sending it back to the same page and the first thing that happens is $fileCount is set to zero again.

Hopefully this will help you figure it out.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: global var returns to 0 after function call

Post by LuaMadman »

Thanks for your answer

I think my best bet is to save it to a file.
I only want to use 1 php file.
Post Reply