SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 31
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    page redirect incrementally every 5 seconds

    I have pages:
    page00001.php
    page00002.php
    page00003.php
    page00004.php
    page00005.php
    ...
    page00009.php
    page00010.php
    ...
    page00099.php
    page00100.php
    ...


    I want to make the page redirect incrementally every, let's say 5 seconds.
    How would I be able to achieve this?
    Once the number reaches 10 and 100 I have to drop a "0" from "page0000". $i represents the number of page to be changed incrementally.
    PHP Code:
    require_once('class_http.php');
    $h = new http();
    $i=2;
    $link "http://.../new/page0000".$i.".htm";
    if (!
    $h->fetch($link)) {
      echo 
    "<h2>There is a problem with the http request!</h2>";
      echo 
    $h->log;
      exit();
    }
    $result $h->body
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is something you need to program in JavaScript. You can't redirect after the page is displayed with PHP as whatever code you write already executed on the server and terminated before any output was sent.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. That's what I'm thinking too. But I wonder how php ad Javascript are going to communicate with each other?

    The link to the page has to be in php:
    PHP Code:
    require_once('class_http.php');
    $h = new http();
    $i=2;
    [
    B]$link "http://.../new/page0000".$i.".htm";[/B]
    if (!
    $h->fetch($link)) {
      echo 
    "<h2>There is a problem with the http request!</h2>";
      echo 
    $h->log;
      exit();
    }
    $result $h->body
    And the only thing that needs to be changed is "0000".$i.".htm.
    So I'm wondering if there is a need to create a hidden field and write the number within it's value and then use $_Request to add a + 1 to make it change pages incrementally.
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    JavaScript knows what page is currently displayed in the browser (window.location), and PHP knows what page was requested ($_SERVER['REQUEST_URI']), therefore either is capable of getting the URL and adding 1 to it. The link doesn't need to be in PHP, it can be in either. The delayed redirect is JS.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My concern is that I'm going to lose the include file. Not only that, I'm going to lose the page.

    I'm looking for some way to refresh the page and at the same time to add the inside .htm file incrementally.


    page00001.htm
    page00002.htm
    page00003.htm
    page00004.htm
    page00005.htm
    ...
    page00009.htm
    page00010.htm
    ...
    page00099.htm
    page00100.htm
    ...


    To give you some info, it's a 700 page book converted into htm. So instead of going through them one by one I thought there might be some easier method to have it scraped off the screen one-by-one to insert them into a database later on.

    That's why I don't want to entirely redirect to a new page but rather refresh and add:
    PHP Code:
    $i=2;
    $link "http://.../new/page0000".$i.".htm"
    incrementally.
    Compare bible texts (and other tools):
    TheWheelofGod

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not save the contents of these files to your database directly, and skip the whole refresh-and-screen-scrape thing?

    PHP Code:
    $num_pages 700;

    for (
    $i 1$i <= $num_pages$i++) {

      
    $num $i;
      while (
    strlen($num) < 5) {
        
    $num '0' $num;
      }

      
    $contents_of_page file_get_contents('http://.../new/page' $num '.htm');

      
    $sql "INSERT INTO pages (page_num, content) VALUES (" $i 
           
    ", '" mysql_real_escape_string($contents_of_page) . "')";
      
    mysql_query($sql$connection);


    It'd probably take a few seconds to run... if you were planning to do something manually that takes 5 seconds to do, you'll save at least an hour.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would that be possible? Am I going to manually do it for 700 pages?
    Compare bible texts (and other tools):
    TheWheelofGod

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I updated my post with an example.

  9. #9
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Why not save the contents of these files to your database directly, and skip the whole refresh-and-screen-scrape thing?

    PHP Code:
    $num_pages 700;

    for (
    $i 1$i <= $num_pages$i++) {

      
    $num $i;
      while (
    strlen($num) < 5) {
        
    $num '0' $num;
      }

      
    $contents_of_page file_get_contents('http://.../new/page' $num '.htm');

      
    $sql "INSERT INTO pages (page_num, content) VALUES (" $i 
           
    ", '" mysql_real_escape_string($contents_of_page) . "')";
      
    mysql_query($sql$connection);


    It'd probably take a few seconds to run... if you were planning to do something manually that takes 5 seconds to do, you'll save at least an hour.
    Ok. So the get_file_contents() is going to grab the info from that file right? But isn't that similar to screen scraping? But your code seems pretty much what I'm looking for.

    Is there a recommendable way of inserting? do you know? because I'm thinking I should insert one <DIV> per record since the DIV tags have their own style attributes.
    Compare bible texts (and other tools):
    TheWheelofGod

  10. #10
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header("Refresh: 5; url=http://example.com/"); 
    I must admit, I did not read this thread as thoroughly as I should have done, so I cannot be certain this will meet your requirements.

    P.S. The refresh header is non-standard (but widely supported).

  11. #11
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I may be missing the point

    PHP Code:
    <?
    if (isset($_GET['i'])){
    $i $_GET['i'];
    $i $i 1;
    }
    else {
    $i 1;
    }
    echo 
    '<META HTTP-EQUIV="refresh" content="5;URL=page'.$i.'.php?i='.$i.'">';
    ?>
    It looks like it should work, just include this into each page (they have to be php enabled pages though)
    What I lack in acuracy I make up for in misteaks

  12. #12
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorenw View Post
    I may be missing the point

    PHP Code:
    <?
    if (isset($_GET['i'])){
    $i $_GET['i'];
    $i $i 1;
    }
    else {
    $i 1;
    }
    echo 
    '<META HTTP-EQUIV="refresh" content="5;URL=page'.$i.'.php?i='.$i.'">';
    ?>
    It looks like it should work, just include this into each page (they have to be php enabled pages though)
    OK I know what I want.

    The code above won't help.

    I need two things:
    1. Refresh the page every 5 seconds;
    2. by refreshing, the link (show below) has to change;
    PHP Code:
    require_once('class_http.php');
    $h = new http();
    $i = (isset($_GET['page']) ? intval($_GET['page']) : 1);
    $page 'http://.../new/page' str_pad ("$i",5,'0',STR_PAD_LEFT) . '.htm'
    if (!
    $h->fetch($page)) {
      echo 
    "<h2>There is a problem with the http request!</h2>";
      echo 
    $h->log;
      exit();
    }
    $result $h->body
    Compare bible texts (and other tools):
    TheWheelofGod

  13. #13
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Why not save the contents of these files to your database directly, and skip the whole refresh-and-screen-scrape thing?

    PHP Code:
    $num_pages 700;

    for (
    $i 1$i <= $num_pages$i++) {

      
    $num $i;
      while (
    strlen($num) < 5) {
        
    $num '0' $num;
      }

      
    $contents_of_page file_get_contents('http://.../new/page' $num '.htm');

      
    $sql "INSERT INTO pages (page_num, content) VALUES (" $i 
           
    ", '" mysql_real_escape_string($contents_of_page) . "')";
      
    mysql_query($sql$connection);


    It'd probably take a few seconds to run... if you were planning to do something manually that takes 5 seconds to do, you'll save at least an hour.
    I'm getting an error:
    Database created
    Fatal error: Maximum execution time of 60 seconds exceeded in ...\insertindb.php on line 20
    PHP Code:
    <?php
    $con 
    mysql_connect("","root","");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    if (
    mysql_query("CREATE DATABASE boti_book",$con))
      {
      echo 
    "Database created";
          
    $num_pages 292;
        
        for (
    $i 1$i <= $num_pages$i++) {
        
          
    $num $i;
          while (
    strlen($num) < 5) {
            
    $num '0' $num;
          }
        
          
    $contents_of_page file_get_contents('http://.../new/page' $num '.htm');//line 20
        // Create table in my_db database
        
    mysql_select_db("boti_book"$con);
        
    $sql "CREATE TABLE boti_pages(id int(6), page_num int(6), content longtext)";
          
    $sql .= "INSERT INTO boti_pages (page_num, content) VALUES (" $i ", '" mysql_real_escape_string($contents_of_page) . "')";
          
    mysql_query($sql$con);
        
        }
      }
    else
      {
      echo 
    "Error creating database: " mysql_error();
      }

    mysql_close($con);
    ?>
    Compare bible texts (and other tools):
    TheWheelofGod

  14. #14
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The error's pretty clear. Your script is taking longer than 60 seconds to run, the maximum execution time set for your server. Google that and you'd find how to change it (hint: it's in php.ini if you have root access, or you can override it with ini_set() if you don't).

    Why are you creating the database and table in your script? Wouldn't just just create those once, by hand, before running your code?

  15. #15
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    The error's pretty clear. Your script is taking longer than 60 seconds to run, the maximum execution time set for your server. Google that and you'd find how to change it (hint: it's in php.ini if you have root access, or you can override it with ini_set() if you don't).

    Why are you creating the database and table in your script? Wouldn't just just create those once, by hand, before running your code?
    Yeah I've done that way before. I thought I would be able to achieve without opening MySQL.

    There was another reason why I posted the code. I wanted to make sure that the code was right.

    I think I need to make some more research on how to grab the attributes of the DIV tags that vary. The DIV tags all have position: absolute; and maybe a few other things but they vary in left, top, width, height... so I wonder if PHP has the ability to grab those and place them in the database table?

    Another thing I'm not sure about as I look at the code you gave was how to make each DIV-innerHTML be placed in a separate record?
    Compare bible texts (and other tools):
    TheWheelofGod

  16. #16
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You're going to want to parse the contents of $contents_of_page with regular expressions (preg_match(), preg_match_all()) to extract the sections you want. PHP is perfectly capable of doing everything you need.

  17. #17
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Get the raw content in the database first, then you can manipulate it further with php after copying the table or something. Saves scraping it all again if you do it wrong.

  18. #18
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You're going to want to parse the contents of $contents_of_page with regular expressions (preg_match(), preg_match_all()) to extract the sections you want. PHP is perfectly capable of doing everything you need.
    Would this work?
    http://ca.php.net/manual/en/function...sbytagname.php
    Compare bible texts (and other tools):
    TheWheelofGod

  19. #19
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perhaps, but you'd have to create a DOMDocument to use that method on first... you don't need to do that, the regular expressions won't be complicated. If you hire someone, they can probably do this project in a few hours at most.

  20. #20
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Perhaps, but you'd have to create a DOMDocument to use that method on first... you don't need to do that, the regular expressions won't be complicated. If you hire someone, they can probably do this project in a few hours at most.
    Isn't there a simple way as in JavaScript where you can narrow it down to what you're looking for:
    Code:
    Document.GetElementByTagName("DIV").innerHTML
    But in PHP coding?

    This, first you have to find the file:
    PHP Code:
    $contents_of_page file_get_contents('http://.../new/page' $num '.htm'); 
    Then you have to look for the TagName.
    PHP Code:
    string file_get_contents  string $filename  [, int $flags  [, resource $context  [, int $offset  [, int $maxlen  ]]]] ) 
    Compare bible texts (and other tools):
    TheWheelofGod

  21. #21
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, you'd write a regular expression which matched the contents of div tags.

  22. #22
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gilgalbiblewheel View Post
    Isn't there a simple way as in JavaScript where you can narrow it down to what you're looking for:
    Code:
    Document.GetElementByTagName("DIV").innerHTML
    But in PHP coding?

    This, first you have to find the file:
    PHP Code:
    $contents_of_page file_get_contents('http://.../new/page' $num '.htm'); 
    Then you have to look for the TagName.
    PHP Code:
    string file_get_contents  string $filename  [, int $flags  [, resource $context  [, int $offset  [, int $maxlen  ]]]] ) 
    I'm trying to understand though:
    PHP Code:
    string file_get_contents  string $filename  [, int $flags  [, resource $context  [, int $offset  [, int $maxlen  ]]]] ) 
    $filename is the URL right?
    What are:
    $flags
    $context
    $offset: where to start from;
    $maxlen: maximum length.
    Compare bible texts (and other tools):
    TheWheelofGod

  23. #23
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Go to the function documentation at php.net if you want to know, but you don't need to use any of that. You need to download the entire webpage before you know where to start splitting it up.

  24. #24
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Go to the function documentation at php.net if you want to know, but you don't need to use any of that. You need to download the entire webpage before you know where to start splitting it up.
    http://ca.php.net/manual/en/function...t-contents.php
    What do you mean by downloading?

    I don't understand the difference between all these:
    dom_domelement_getelementsbytagnamens
    dom_domelement_getelementsbytagname
    dom_domdocument_getelementsbytagname
    dom_domdocument_getelementsbytagnamens
    Compare bible texts (and other tools):
    TheWheelofGod

  25. #25
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    file_get_contents() returns the contents of a file or URL as a string. If these book pages are on a drive somewhere, then they're files, if they're on the web somewhere, then they get downloaded before the contents can be returned.

    I'm still telling you that you don't need, or want, to use any of those DOM functions. This isn't JavaScript, they aren't easy nor do they save you time here.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •