Jump to content

fwrite()


Recommended Posts

Hey all,

 

I am having some trouble here, look over my code and let me know what you think.

 

The $nextSeller isn't getting written.

 

<?php

$expire=time()+60*60*24*30;

function getSetSellerId() {
if (isset($_COOKIE['sellerId'])) { // If this is a returning cutsomer
	return $sellerId = 'preset ' . $_COOKIE['sellerId'];
} else {

	$file = 'inc/sellerId.txt';

	// Open file for reading
	$ff = fopen($file, 'r');
	$sellerId = fgets($ff);
	fclose($ff);

	setcookie('sellerId', $sellerId, $expire);
	$nextSeller=nextSeller($sellerId);
	return $sellerId . ' and the next one is ' . $nextSeller;

	// Open file to write
	$ff = fopen($file, 'w');
	fwrite($ff, $nextSeller);
	fclose($ff);
}
}

function nextSeller($lastSeller) {
$ids = array(0 => '641362', // Jason
			 1 => '671425', // Lorene
			 2 => '123456', // Daren <- need the real seller ID
			 3 => '789101'); // Unknown <- need the real seller ID

for($i=0; $i<= 3; $i++) {
	echo $i .' ';
	echo 'Last seller is: ' . $lastSeller .' <= | ';
	if ($ids[$lastSeller] == $ids[$i]) {
		if($i == 3) {
			$nextSeller = $i-3;
			} else {
				$nextSeller = i+1;
			}
		return $nextSeller;
	}
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/156580-fwrite/
Share on other sites

return $sellerId = 'preset ' . $_COOKIE['sellerId'];

$sellerId is useless there.

 

setcookie('sellerId', $sellerId, $expire);

PHP functions do not read variables outside of its scope so $expire won't have any value.

 

return $sellerId . ' and the next one is ' . $nextSeller;

When returning something inside a function, the function stops processing at that point so any code below it will not be executed. That's why it's not writing $nextSeller.

Link to comment
https://forums.phpfreaks.com/topic/156580-fwrite/#findComment-824434
Share on other sites

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.