Jump to content

How to change sequentially in php


11Tami

Recommended Posts

Hello, I know how to change things randomly with php

 

<?php

$this = rand(1,31);

$includefile = "file" . $this . ".txt";

include ($includefile);

?>

 

Which would change a text file between 1 and 31 randomly. But how to do it so first it goes to file01.txt then to file02.txt ,3, 4, and so on and in order? Do I need an i++ in there somewhere, if so how? Thank you very much.

Link to comment
Share on other sites

In case you go above 99 or 999, it is more efficient to use str_pad to add the zeros to the front instead of several if statements.

 

(change the 2 in the str_pad command to the number of digits in the highest number.)

<?php

for ($i=1; $i<32;$i++) {
  $includefile = "file" . str_pad($i, 2, "0", STR_PAD_LEFT) . ".txt";
  include ($includefile);
}

?>

 

Link to comment
Share on other sites

Thanks a lot, I'll send you guys something. Only problem I'm having is its showing all of them and all at once and posting two of the same in a row. Any way to put a delay in before the next one appears? I would imagine sleep would help with that but not sure what to attach it to. Thank you very much.

Link to comment
Share on other sites

I just realized, sleep won't work. It will still show all of them on the page. Because I don't think it can replace the first item with the next one, since the first one has already executed.

 

We're close but not quite. I need something that will show the new item when the page loads, instead of all at once. Thank you very much.

Link to comment
Share on other sites

This is the strangest thing, I've looked at your code inside and out. I think I can do it without the 0 in front.

 

But for the life of me I can't figure out why its putting all of them on

the page, instead of just one at a time on page load. Anyone know?

Link to comment
Share on other sites

Any way to put a delay in before the next one appears?

 

How much of a delay? Do you want to display file 01 first, then on next request 02 etc etc? If so, its a little more complex than the solutions provided. You'll need to store the last accessed file number somewhere (either a file or a database), then update that on each request.

 

But for the life of me I can't figure out why its putting all of them on

the page, instead of just one at a time on page load. Anyone know?

 

Because your including them within a loop.

Link to comment
Share on other sites

I don't think a sleep will work unless someone can proove to me otherwise, because it can't replace the item that was already put on the page before. Page load or refresh is fine to go to the next item. I think whats causing it to put more than one on the page is this $i<32;$i++

I think if it said something like $i++ without the < then it might work.

I'm just not savy enough at this to know right where to put it all.

 

$i = 1;

$i++; 

if ($i == 55)   

{$i}

$getfile = $i;

$includefile = "file" . $getfile . ".txt";

include ($includefile);

 

I know I have this wrong I just don't know how to do the i++

Something that says to automatically go to the next file number on next page refresh. Then if the file number reaches 55 for it to loop back to number 1 again. Hope someone can fix my errors. Thanks.

 

Link to comment
Share on other sites

If you just want to maintain it for a small amount of time, you could also use a session. eg;

 

<?php

  session_start();
  if (!isset($_SESSION['file'] || $_SESSION['file'] == 55)) {
    $_SESSION['file'] = 0;
  }

  include "file{$_SESSION['file']}.txt";
  $_SESSION['file']++;

?>

  

Link to comment
Share on other sites

D'oh ... thorpe beat me to the punch on that one :).

 

The only thing wrong with the code though is that it's based on a per session basis.  Meaning that every user who first visits your webpage will always see 'file1.txt' and then 'file2.txt', etc.

 

If you want each user to see a subsequent file (i.e. User A visits then he/she will see 'file1.txt' and then if User B visits the site after A then he/she will see 'file2.txt'), then I would suggest using a database or reading/writing to a file to keep track of the number.

Link to comment
Share on other sites

This will allow each user to have their own counter. So if user A has viewed the page 5 times, user B will still see file01 when visiting for the first time. We are throwing out various solutions, because you are not being specific enough in your requests. If the solutiion you want is not here, please be very specific.

 

<?php

$max_files = 31;
if (isset($_COOKIE['current_file']) {
 $current_file = $_COOKIE['current_file'];
 $current_file = ($current_file==$max_files)?0:$current_file++;
} else {
 $current_file = 0;
}

setcookie("current_file", $current_file, time()+60*60*24*30);  /* expire in 30 days*/

$include_file = "file" . str_pad($current_file, 2, "0", STR_PAD_LEFT) . ".txt";
include ($include_file);

?>

Link to comment
Share on other sites

I say lets forget about the code and just have a ho down ha ha, you people have great concepts. Thanks for the help. Ok..... back to the code.

 

Yes Thorpe I saw that, thank you, it just seemed over my head but I'd like to ask you about it later. You guys are taking this to a whole new level I hadn't even thought of before. But I'll try to keep it simple and explain further. It doesn't matter to me if each person sees a different item. A person is looking at the page and they see one item in the rotation. Then the page refreshes and the same person sees item number 2, refreshes again they item number 3. Thats all I need. I was trying to show that in the terrible last code I posted but I don't think anyone could tell what I was attempting. Hope that helps.

Link to comment
Share on other sites

I did, I posted it twice, on my first question and after that but I don't know where to go from there. Here is the last one again, but I know its incorrect, I just don't know how to fix it.

 

$i = 1;

$i++; 

if ($i == 10)   

{$i}

$getfile = $i;

$includefile = "file" . $getfile . ".txt";

include ($includefile);

 

Its supposed to go from one .txt file to the next in order.

when it reaches 10 or whatever number is inserted, to start over again.

 

Link to comment
Share on other sites

I am. I just don't know how to keep this going. This equals 1. I need it to go to 2 then 3 all on its own on page load. Is there some sort of repeat function or something to keep it all going? Can't find it.

 

<?php
$i = 0; 
$i++;
if ($i == 10)    
$i;
$getfile = $i++; 
$includefile = "file" . $getfile . ".txt";
include ($includefile);
?>

Link to comment
Share on other sites

Really, your not reading. You need to store the values somewhere. All variables are cleared on each request, you need your variables to persist, hance, you need to store there values somewhere. ie, a file or a database.

Link to comment
Share on other sites

<?php

  session_start();
  if (!isset($_SESSION['file'] || $_SESSION['file'] == 55)) {
    $_SESSION['file'] = 0;
  }

  include "file{$_SESSION['file']}.txt";
  $_SESSION['file']++;

?>

I believe Thorpe has already shown you how to fix this.  Have you tried this? Are you not reading? It doesn't seem like you are even considering other's suggestions.

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.