Jump to content


Photo

Output array element on refresh


  • Please log in to reply
9 replies to this topic

#1 rossh

rossh
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 13 September 2006 - 01:50 PM

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 BELOW

function 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;
}

Thanks

Ross

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 13 September 2006 - 02:17 PM

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?
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 rossh

rossh
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 13 September 2006 - 02:34 PM

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?

#4 rossh

rossh
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 13 September 2006 - 02:42 PM

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.

Thanks

Ross

#5 rossh

rossh
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 13 September 2006 - 03:39 PM

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?

Thanks
R

#6 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 06:21 AM

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).
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#7 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 06:56 AM

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 do


and a tip
the file() function will automatically put the contents of a file into an array

so this whole thing here
$a_advert = array(); 
   $file = fopen($filename, "r"); 
   
   while(!feof($file)) 
   { 
      $aQuotes[] = fgets($file, 4096);
   }
can be shortened to
$a_advert = file($filename);

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#8 rossh

rossh
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 14 September 2006 - 08:04 AM

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.  Sorry

Ross

#9 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 08:07 AM

just make sure you're calling this function of yours before you echo anything to the browser
that includes nonPHP stuff like the DOCTYPE and HTML tags
you gotta call it before any text would be normally displayed on the browser

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#10 rossh

rossh
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 14 September 2006 - 02:37 PM

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.php

require_once("include/functions.php");

if ($page="home")
{
$advert = advert("advert.txt");
}

home.htm

<body>

<?php echo($advert); ?>

...

Thanks
R




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users