Jump to content

Output array element on refresh


rossh

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/20615-output-array-element-on-refresh/
Share on other sites

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?
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
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
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).
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
[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]
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
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
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

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.