rossh Posted September 13, 2006 Share Posted September 13, 2006 Hi i have a function which loops through an array each time the user refreshes the screen. I have this working, but i wanted to have the text in a seperate file and it's not working. Can someone help.function newAdvert($filename){ $a_advert = array(); $file = fopen($filename, "r"); while(!feof($file)) { $a_advert[] = fgets($file, 4096); } $i_advert_num = $_COOKIE["advert"]; if($i_advert_num == "" || $i_advert_num >= sizeof($a_advert)-1) { $i_advert_num = 0; } else { $i_advert_num++; } setcookie ("advert"); setcookie("advert", $i_advert_num); $advert = $a_advert[$i_advert_num]; return $advert; fclose ($file); }echo newAdvert("advert.txt");//OLD FUNCTION BELOWfunction advert(){ $aQuotes[0] = "one"; $aQuotes[1] = "two"; $aQuotes[2] = "three"; $iQuoteNum = $_COOKIE["quotes"]; if($iQuoteNum == "" || $iQuoteNum >= sizeof($aQuotes)-1) { $iQuoteNum = 0; } else { $iQuoteNum++; } setcookie ("quotes"); setcookie("quotes", $iQuoteNum); $banner = $aQuotes[$iQuoteNum]; return $banner;}ThanksRoss Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 13, 2006 Share Posted September 13, 2006 what do you mean you want the text in a seperate file? You want to loop through text in a seperate file? you want to use an include for the function? you want to output text to a different file?I don't get it. Could you try to be more specific? Quote Link to comment Share on other sites More sharing options...
rossh Posted September 13, 2006 Author Share Posted September 13, 2006 I'm also getting a Headers already sent error, because of the cookies, how do i resolve this when i'm running the function inside html tags? Quote Link to comment Share on other sites More sharing options...
rossh Posted September 13, 2006 Author Share Posted September 13, 2006 Sorry, the file reads the lines in the textfile and puts it into an array. The function then displays one of those element values on the screen. As the user refreshes the screen the cookie value increments and this value is taken as the array element so the user is rotating the text each time they refresh, but the functions not working from reading the file. The old function work where the value of each element are in the function and i don't know where i'm going wrong.ThanksRoss Quote Link to comment Share on other sites More sharing options...
rossh Posted September 13, 2006 Author Share Posted September 13, 2006 Hi i've been playing around with this, i can get this working if it's not in a function. $filename = "advert.txt"; $a_advert = array(); $file = fopen($filename, "r"); while(!feof($file)) { $aQuotes[] = fgets($file, 4096); } $iQuoteNum = $_COOKIE["quotes"]; if($iQuoteNum == "" || $iQuoteNum >= sizeof($aQuotes)-1) { $iQuoteNum = 0; } else { $iQuoteNum++; } setcookie ("quotes"); setcookie("quotes", $iQuoteNum); $banner = $aQuotes[$iQuoteNum]; return $banner;How can i put this in a function so i can then pass different filenames to it?ThanksR Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 14, 2006 Share Posted September 14, 2006 if you're trying to use output in the function as part of the header info, then re-do the header info, you'll get errors, so I'd either take the output out of the function and use the function to do the calculation and call the function before outputting later, or keep it the way it is (working). Quote Link to comment Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 I'm still lost on your question...what are you trying to do with the array for every refresh...and WHY isn't it working....what is it not doing that you want it to doand a tipthe file() function will automatically put the contents of a file into an arrayso this whole thing here[code]$a_advert = array(); $file = fopen($filename, "r"); while(!feof($file)) { $aQuotes[] = fgets($file, 4096); }[/code]can be shortened to[code]$a_advert = file($filename);[/code] Quote Link to comment Share on other sites More sharing options...
rossh Posted September 14, 2006 Author Share Posted September 14, 2006 Sorry guys, can i start again. I want to be able to put the code which works into a function, so i can use it for various text files. function advert($filename) { $a_advert = array(); $file = fopen($filename, "r"); while(!feof($file)) { $aQuotes[] = fgets($file, 4096); } $iQuoteNum = $_COOKIE["quotes"]; if($iQuoteNum == "" || $iQuoteNum >= sizeof($aQuotes)-1) { $iQuoteNum = 0; } else { $iQuoteNum++; } $banner = $aQuotes[$iQuoteNum]; return $banner; return $iQuoteNum; }This is what i have so far, but i have a problem with the cookies. Headers already sent error. How can i create and update the cookie outside the function, and preferably only when the function is in use?If you want me to make a new question i can. SorryRoss Quote Link to comment Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 just make sure you're calling this function of yours before you echo anything to the browserthat includes nonPHP stuff like the DOCTYPE and HTML tagsyou gotta call it before any text would be normally displayed on the browser Quote Link to comment Share on other sites More sharing options...
rossh Posted September 14, 2006 Author Share Posted September 14, 2006 Thanks for getting back to me. I've tried the following but i can only display the first array element, the cookie value is not being updated?index.phprequire_once("include/functions.php");if ($page="home"){$advert = advert("advert.txt");}home.htm<body><?php echo($advert); ?>...Thanks R Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.