Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-15-2006, 10:58 AM   PM User | #1
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
Rotating in order, not random

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.


Quote:
<? $textfile ="qoutes.txt";
$items = file("$textfile");
$item = rand(0, sizeof($items)-1);
echo $items[$item];
?>
curb is offline   Reply With Quote
Old 06-16-2006, 01:24 AM   PM User | #2
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
Is it possible to do this from using the code above to start with?
curb is offline   Reply With Quote
Old 06-16-2006, 01:28 AM   PM User | #3
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
Sure -- instead of using rand() to select an element, just send your array through a foreach() loop.
Kid Charming is offline   Reply With Quote
Old 06-16-2006, 01:54 AM   PM User | #4
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
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"
curb is offline   Reply With Quote
Old 06-16-2006, 03:08 AM   PM User | #5
anarchy3200
Regular Coder

 
anarchy3200's Avatar
 
Join Date: Mar 2003
Location: England
Posts: 261
Thanks: 0
Thanked 1 Time in 1 Post
anarchy3200 is an unknown quantity at this point
Try:
PHP Code:

<? 
$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];
?>
__________________
Mike
anarchy3200 is offline   Reply With Quote
Old 06-16-2006, 03:19 AM   PM User | #6
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
I'm getting a parse error on line 6.
curb is offline   Reply With Quote
Old 06-16-2006, 11:33 AM   PM User | #7
anarchy3200
Regular Coder

 
anarchy3200's Avatar
 
Join Date: Mar 2003
Location: England
Posts: 261
Thanks: 0
Thanked 1 Time in 1 Post
anarchy3200 is an unknown quantity at this point
Sorry, forgot a closing bracket.
PHP Code:
<?  
$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]; 
?>
__________________
Mike
anarchy3200 is offline   Reply With Quote
Old 06-16-2006, 04:21 PM   PM User | #8
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
I seem to get this error:

Quote:
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
curb is offline   Reply With Quote
Old 06-17-2006, 01:21 AM   PM User | #9
Nicklas
New Coder

 
Join Date: Jun 2006
Location: Sweden
Posts: 49
Thanks: 0
Thanked 3 Times in 3 Posts
Nicklas is on a distinguished road
Try something like this
PHP Code:
<?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']];
?>
Nicklas is offline   Reply With Quote
Old 06-20-2006, 12:14 AM   PM User | #10
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
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 is offline   Reply With Quote
Old 06-21-2006, 01:41 AM   PM User | #11
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
Quote:
Originally Posted by Nicklas
Try something like this
PHP Code:
<?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.
curb is offline   Reply With Quote
Old 06-21-2006, 12:30 PM   PM User | #12
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You can make your code safe from overflow by using a modulus operator.

Code:
<?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.
Fumigator is offline   Reply With Quote
Old 06-21-2006, 05:26 PM   PM User | #13
curb
New Coder

 
Join Date: May 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
curb is an unknown quantity at this point
Quote:
Originally Posted by Fumigator
You can make your code safe from overflow by using a modulus operator.

Code:
<?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

Quote:
<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' ?>
curb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.