...

View Full Version : Rotating in order, not random



curb
06-15-2006, 10:58 AM
I'm using a small script that randomly rotates words/html through a single line from a text file. I'm trying to rotate the list in order so the next refresh would go down the list.



<? $textfile ="qoutes.txt";
$items = file("$textfile");
$item = rand(0, sizeof($items)-1);
echo $items[$item];
?>

curb
06-16-2006, 01:24 AM
Is it possible to do this from using the code above to start with?

Kid Charming
06-16-2006, 01:28 AM
Sure -- instead of using rand() to select an element, just send your array through a foreach() (http://us3.php.net/manual/en/control-structures.foreach.php) loop.

curb
06-16-2006, 01:54 AM
Sorry for making it unclear =x Thanks for your input anyways. I don't want the characters to load all at the same page. I want a word to show up in order for every refresh.

Here's another example:

text file:
hey
how
are
you


page.php
Refresh Page load #1 Shows "hey"
Refresh Page load #2 Shows "how"
Refresh Page load #3 Shows "are"
Refresh Page load #4 Shows "you"

anarchy3200
06-16-2006, 03:08 AM
Try:



<?
$textfile ="qoutes.txt";
$items = file("$textfile");

//If Session not set, set it
if(!isset($_SESSION['vid']){
$_SESSION['vid'] == 1;
}else{
//If session is set add one so the next item is displayed
$_SESSION['vid']++;
}

//Subtract one so the item id's start at 0
$itemid = $_SESSION['vid']-1;

//Output
echo $items[$itemid];
?>

curb
06-16-2006, 03:19 AM
I'm getting a parse error on line 6.

anarchy3200
06-16-2006, 11:33 AM
Sorry, forgot a closing bracket.


<?
$textfile ="qoutes.txt";
$items = file("$textfile");

//If Session not set, set it
if(!isset($_SESSION['vid'])){
$_SESSION['vid'] == 1;
}else{
//If session is set add one so the next item is displayed
$_SESSION['vid']++;
}

//Subtract one so the item id's start at 0
$itemid = $_SESSION['vid']-1;

//Output
echo $items[$itemid];
?>

curb
06-16-2006, 04:21 PM
I seem to get this error:


Notice: Undefined variable: _SESSION in c:\program files\localhost\www\rotate\index.php on line 16

Notice: Undefined variable: _SESSION in c:\program files\localhost\www\rotate\index.php on line 23

Notice: Undefined offset: -1 in c:\program files\localhost\www\rotate\index.php on line 26

Nicklas
06-17-2006, 01:21 AM
Try something like this

<?php
session_start();
$item = file("qoutes.txt");

if ($_SESSION['sess_row'] === NULL) {
$_SESSION['sess_row'] = 0;
} else {
$_SESSION['sess_row']++;
}

echo $item[$_SESSION['sess_row']];
?>

curb
06-20-2006, 12:14 AM
It rotates randomly but not in sequence. Does it have to have a session for it to rotate it in a sequence for every refresh?

curb
06-21-2006, 01:41 AM
Try something like this

<?php
session_start();
$item = file("qoutes.txt");

if ($_SESSION['sess_row'] === NULL) {
$_SESSION['sess_row'] = 0;
} else {
$_SESSION['sess_row']++;
}

echo $item[$_SESSION['sess_row']];
?>


Ok, got it tested and its working sort of. After the text file runs out of qoutes, it gives me this error:


Notice: Undefined offset: 4

Example: I have 3 qoutes and on the fourth refresh it gives me an error with the error number going up one at every refresh.

Fumigator
06-21-2006, 12:30 PM
You can make your code safe from overflow by using a modulus operator.


<?php
session_start();
$item = file("qoutes.txt");
$itemCount = count($item);

if ($_SESSION['sess_row'] === NULL) {
$_SESSION['sess_row'] = 0;
} else {
$_SESSION['sess_row'] = ($_SESSION['sess_row'] + 1) % $itemCount;
}

echo $item[$_SESSION['sess_row']];
?>

What this does is instead of incrementing the index of the array by one, it divides the index+1 by the total itemCount and sets the index of the array to the remainder of that division.

Or... you can just put an "if" statement in to check to see if you've gone past the end of your array. :)

curb
06-21-2006, 05:26 PM
You can make your code safe from overflow by using a modulus operator.


<?php
session_start();
$item = file("qoutes.txt");
$itemCount = count($item);

if ($_SESSION['sess_row'] === NULL) {
$_SESSION['sess_row'] = 0;
} else {
$_SESSION['sess_row'] = ($_SESSION['sess_row'] + 1) % $itemCount;
}

echo $item[$_SESSION['sess_row']];
?>

What this does is instead of incrementing the index of the array by one, it divides the index+1 by the total itemCount and sets the index of the array to the remainder of that division.

Or... you can just put an "if" statement in to check to see if you've gone past the end of your array. :)

Thanks Fumigator, the script is working but when I try adding any character before the code like an html, it gives me


<br />
<b>Warning</b>: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\program files\easyphp1-8\www\qoutes\index.php:1) in <b>c:\program files\easyphp1-8\www\qoutes\qoutes.php</b> on line <b>2</b><br />


Here's what I put:
Inspired Qoutes: <?php include 'qoutes.php' ?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum