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
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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).
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.