11Tami Posted May 21, 2007 Share Posted May 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/ Share on other sites More sharing options...
per1os Posted May 21, 2007 Share Posted May 21, 2007 <?php for ($i=1; $i<32;$i++) { $getfile = $i; if ($i < 10) { $getfile = "0" . $i; } $includefile = "file" . $getfile . ".txt"; include ($includefile); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258516 Share on other sites More sharing options...
Psycho Posted May 21, 2007 Share Posted May 21, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258538 Share on other sites More sharing options...
11Tami Posted May 21, 2007 Author Share Posted May 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258542 Share on other sites More sharing options...
11Tami Posted May 21, 2007 Author Share Posted May 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258559 Share on other sites More sharing options...
kathas Posted May 21, 2007 Share Posted May 21, 2007 just use this...: <?php $this = rand(1,31); $includefile = "file" . str_pad($this, 2, "0", STR_PAD_LEFT) . ".txt"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258562 Share on other sites More sharing options...
11Tami Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks, isn't that random? I still need sequential. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258567 Share on other sites More sharing options...
11Tami Posted May 21, 2007 Author Share Posted May 21, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258576 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258578 Share on other sites More sharing options...
Psycho Posted May 21, 2007 Share Posted May 21, 2007 OK, let's say you display the 1st text fiel on a page, how do you want the user to be able to see the 2nd text item? Select a link, refresh automatically, what??? Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258579 Share on other sites More sharing options...
Corona4456 Posted May 21, 2007 Share Posted May 21, 2007 By the looks of it. It seems like you want to maintain state (between refreshes, or clicking on a link or whatever) which means you'll have to use the $_SESSION variable. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258591 Share on other sites More sharing options...
11Tami Posted May 21, 2007 Author Share Posted May 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258602 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 Did you even read my reply? Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258612 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 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']++; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258616 Share on other sites More sharing options...
Corona4456 Posted May 21, 2007 Share Posted May 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258630 Share on other sites More sharing options...
trq Posted May 21, 2007 Share Posted May 21, 2007 then I would suggest using a database or reading/writing to a file to keep track of the number. As I also suggested some 6 posts ago :-) Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258634 Share on other sites More sharing options...
Psycho Posted May 21, 2007 Share Posted May 21, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258636 Share on other sites More sharing options...
11Tami Posted May 22, 2007 Author Share Posted May 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258679 Share on other sites More sharing options...
11Tami Posted May 22, 2007 Author Share Posted May 22, 2007 Forgot to say when that same person comes back to the page again the same rotation starts over. Nothing needs to be remembered. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258693 Share on other sites More sharing options...
trq Posted May 22, 2007 Share Posted May 22, 2007 Well, we've already told you how. Now how about you try some code? Post it here if you have problems. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258703 Share on other sites More sharing options...
11Tami Posted May 22, 2007 Author Share Posted May 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258957 Share on other sites More sharing options...
trq Posted May 22, 2007 Share Posted May 22, 2007 Sorry... go back through this thread and actually read the replies, then, try to cretae some code following those ideas. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258995 Share on other sites More sharing options...
11Tami Posted May 22, 2007 Author Share Posted May 22, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-258999 Share on other sites More sharing options...
trq Posted May 22, 2007 Share Posted May 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-259006 Share on other sites More sharing options...
Corona4456 Posted May 22, 2007 Share Posted May 22, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/52389-how-to-change-sequentially-in-php/#findComment-259041 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.