Jump to content

How to change sequentially in php


11Tami

Recommended Posts

Thank you all very much for all your help, yes I was reading, I was thinking in javascript. That it would just go onto the next item with the right code. Didn't know I needed sessions for php for it to be able to continue. After Thorpes last post making me understand how it works with php the lightbulb finally came on and it makes sense to me now. I tried Thorpes but am getting this.

Parse error: parse error, unexpected T_BOOLEAN_OR, expecting ',' or ')'  on line 4.

Link to comment
Share on other sites

That session code won't really persist either, and parse errors are simple typos.

 

Put simply, on each request you need to increment the last accessed file number (from storage), then display said file. On the next request you need to do the same again.

 

Javascript works no differently, there is no persistence between requests.

 

An example using a database.

 

<?php

  if ($result = mysql_query("SELECT fileno FROM files")) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      $file = $row['fileno'];
      if ($file == 55) {
        $next = 0;
      } else {
        $next = $file+1;
      }
      if (!mysql_query("UPDATE files SET fileno = $next")) {
        echo "Update failed";
      }
    } else {
      echo "Failed to retrieve fileno";
    }
  }
  include "file$file.txt";

?>

Link to comment
Share on other sites

OK thanks very much, I'm game I'll try anything.

 

Also, If anyone knows how mjdamato's could help with this. I just switched a few things around because I think I need to set the cookie first and I didn't understand a few other things when it wasn't working on page reload. As usual with this I don't know how to make it go to the next item on the next page load when the cookie visits again. I also don't know how to set his time() function. In the meanwhile, I'll get started on the database code.

 

<?php
$max_files = 15;
$current_file = 0;
if ($current_file = 0)
{setcookie("thiscookie", $current_file, time()+60*60*24*30);  /* expire in 30 days*/}
if (isset($_COOKIE['thiscookie']) {
setcookie("thiscookie", $current_file++, time()+60*60*24*30);}
if ($max_files = 15)
$current_file;
$include_file = "file" . $current_file . ".txt";             
include ($include_file);} 
?>

Link to comment
Share on other sites

That session code won't really persist either, and parse errors are simple typos.

 

Put simply, on each request you need to increment the last accessed file number (from storage), then display said file. On the next request you need to do the same again.

 

Javascript works no differently, there is no persistence between requests.

 

An example using a database.

 

<?php

  if ($result = mysql_query("SELECT fileno FROM files")) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      $file = $row['fileno'];
      if ($file == 55) {
        $next = 0;
      } else {
        $next = $file+1;
      }
      if (!mysql_query("UPDATE files SET fileno = $next")) {
        echo "Update failed";
      }
    } else {
      echo "Failed to retrieve fileno";
    }
  }
  include "file$file.txt";

?>

It does persist across refreshes just not when you close and reopen your browser.

Link to comment
Share on other sites

Ohmgosh, I can't believe it, this is doing something.

 

<?php
$max_files = 15;
$current_file = 0;
if ($current_file = 0)
{setcookie("thiscookie", $current_file, time()+60*60*24*30);  /* expire in 30 days*/}
if (isset($_COOKIE['thiscookie']) {
setcookie("thiscookie", $current_file++, time()+60*60*24*30);}
if ($current_file == $max_files)
$current_file;
$include_file = "file" . $current_file . ".txt";             
include ($include_file);} 

?>

 

Its going from file0.txt to file1.txt! Is there a way I can keep it going with out adding another line like this?

if (isset($_COOKIE['thiscookie']) {

setcookie("thiscookie", $current_file++, time()+60*60*24*30);}

I don't know why I would need to add any other lines anyway. It says if the cookie is the same to go on to the next text file already. Hoping you guys can tell me why it storing the first one to the second but not going on from there. Once I figure that out, I should be good to go.

Link to comment
Share on other sites

Ok, tami, you really need to just slow down, read the information and try to digest it. Also, I asked at least a couple of times for you to be VERY SPECIFIC as to what you are trying to accomplish. From your last post I am assuming now that you want the page to go from text0 to text1 to text2, etc. automatically. In other words, the user goes to the page and after x seconds it refreshes on it's own. Is this correct? (PLEASE BE SPECIFIC).

 

Now, you had a previous question regarding "my" time function. That is a built in PHP function. I was using it within the setcookie function because you have to provide expiration date when setting a cookie or else it expires once the browser window is closed (hence the "expires in 30 days comment"). A quick read of the setcookie documentation would have explained that.

 

You have some problems in the code you posted. The first IF statement is assigning 0 to the variable (single equal sign) and not testing to see if the variable is equal to 0 (double equal signs). So the script follows this order:

 

1) Assigns 0 to $current_file

2) Again assigns 0 to $current_file and sets a cookie

3) Again sets the cookie

4) If $current_file == $max_files, does nothing, since $current_file; just returns the value and nothing else

5) Includes $current_file;

 

I see no way that the above code will ever give you anything other than file0. You should have stuck withthe code I or someone else had provided. My code had some typos, but the logic is sound. I make no appologies as I clearly state in my signature, that I don't always test the code I provide.

 

Anyway, based upon what *I think* you want, this should work:

 

Will cycle through pages 0 to 15. Page will refresh every 3 seconds with the next file's content.

<?php

$max_files = 15;
if (isset($_COOKIE['current_file'])) {
  $current_file = $_COOKIE['current_file'] + 1;
  if ($current_file > $max_files) { $current_file = 0; }
} else {
  $current_file = 0;
}

setcookie("current_file", $current_file);  /* expire in 30 days*/

echo "<html>";
echo "<head>";
echo "<meta http-equiv=\"refresh\" content=\"3\">";
echo "</head>";
echo "<body>";

$include_file = "file" . $current_file . ".txt";
include ($include_file);
//echo $include_file;

echo "</body>";
echo "</html>";

?>

Link to comment
Share on other sites

Thank you very much! Thats mostly what I need, I just need to adjust just a few things but you did it, I appreciate it very, very, much. I can't keep defending myself, from the start I said this is what I needed except but a few things. But like I said I really appreciate the help from everyone and I already sent you all I had, have nothing else. Thanks again for all the help.

Link to comment
Share on other sites

I'm sorry if you feel that we are ganging up on you. I reread your first post and I still would not think that what you asked for there was what we ended up with. We are all willing to help, as evidenced by us sticking with this for three pages, but the person posting a question must take the responsibility of being specific intheir request. Correct terminology should be used and if the person does not know the correct terminolgy they should state that and try to explain the best they can.

 

...how to do it so first it goes to file01.txt then to file02.txt ,3, 4, and so on and in order?

 

It never stated "when" it would change files. Subsequent posts made it appear you wanted to change ont he same page load, which would be a javascript solution. In the end, the solution was more about an HTML feature ("refresh") as much as it was PHP.

Link to comment
Share on other sites

I just think really good coders forget why we are here in the first place. Because we don't know what we are doing. We aren't perfect, if we were we wouldn't be here. We are learning.

Just because I had no idea I would need cookies because of lack of experience, I still stated what I was looking for.

Link to comment
Share on other sites

No, it is the poster's responsibility to make sure the message is clear. You didn't need to state anything about cookies or anything else of a technical nature.

 

Here is one way the problem could ahve been stated such that the problem could have been solved on page 1.

 

=====================================

 

I have this script which shows random content.

 

<?php
$this = rand(1,31);
$includefile = "file" . $this . ".txt";
include ($includefile);
?>

 

However I want it so that when a user accesses the page it will first show file1 content, then after a few seconds the page will refresh and show the content for file2, and then file3, etc.

 

=====================================

 

Saying "..it goes to file01.txt then to file02.txt ,3, 4, and so on.." is confusing, beacuse "goes" is not specific enough. We did not know if you meant on subsequent visits of the page, all on one page, or by automatically refreshing the page.

 

Do, or say, what you wish. I am done with this post.

Link to comment
Share on other sites

Heck I do not know every function about php there is to know. I love the php.net serach feature and google.com for that. But I also make it a point to research projects that I am working on to figure out what can help me through the process.

 

I also read up on each function and would read what everyone is saying in detail. Reading is key, and all too many times people come here posting for help expecting the "experts" to do their reading and coding for them. Not to be bashing you or anyone else on this forum in any way, just an observation.

 

How can you expect to know how to code if you are not willing to put forth the effort and read and try and understand what is happening? That is how 99% of the experts came to be, very rarely you know people who got to where they are in the programming world without reading.

 

Just my 2 cents on the issue, it is always good to ask for help, just do not depend on it. =)

Link to comment
Share on other sites

We can only make clear what we understand. There is no way for us to be exactly clear if we don't know every function there is to know about php. Which a lot of us don't.

 

You obviously didn't READ my last post. The example statement had nothing to do with knowing anything about PHP functions. It had everything to do with adequately specifying the problem at hand. You could have saved everyone a lot of time by merely being specific as to when you wanted the functionality to take place instead of using the word "goes".

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.