Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    494

    Unanswered: adding pagination to foreach loop

    I have the pagination in the for loop but don't know how to paginate in the foreach loop:
    PHP Code:
        //looking for the ths
        
    $file="saveddbtable/bible_small.htm";
        
    $contents_of_page file_get_contents($file);
        echo 
    $contents_of_page;
        
    $dom = new DOMDocument();
        
    $dom->loadHTML($contents_of_page);
        
    // Initialize arrays
        
    $tdArray $array = array();

        
    // count the array for future comparison
        
    $count count($fields); //no array_slice because id is left out from the beginning

        // Get all the Table Cells, but if it matches the same count as the Table Header array, create a new array row
        
    $td $dom->getElementsByTagName('td');
        
    $numRecordsPerPage 20;
        
    $offset $numRecordsPerPage * ($page 1);    
        
        
    $i $offset;
        foreach( 
    $td as $value ){
            if(
    count($tdArray[$i]) != $count){
                
    $tdArray[$i][] = $value->nodeValue;
                echo 
    '<pre>tdArray'.$i.'=';
                
    print_r($tdArray[$i]);
                echo 
    '</pre>';
            }else{
                
    $i++;
                
    $tdArray[$i][] = $value->nodeValue;
            }
        }

        for(
    $j $offset$j $offset $numRecordsPerPage$j++){
            echo 
    "-->".$j."<--";
            
    // Add to insert statement
            
    $sql "INSERT INTO ".$dbTable3." (".implode(", "array_slice($fields1)).") VALUES ('".implode("', '"array_slice($tdArray[$j], 1))."')";
            echo 
    "<span style=\"color: green;\">".$sql."</span><br />\n";
            
    mysql_query($sql) or die(mysql_error());
        }
        echo 
    '<pre>';
        
    print_r($fields);
        
    var_dump($tdArray);
        echo 
    '</pre>'

  2. #2
    Join Date
    Jul 2004
    Posts
    494
    Actually the pagination may not be the problem but the way it collects the innerhtml of the td tags.
    So my question is how to collect the values of only the number assigned by the pagination. So if it's 20 records per page and the total records to collect is 24 then it would take 2 pages. The first page would have records 1-20 and the 2nd page 21-24.
    The table from which it collects:
    Code:
    <table><tr><th>id</th><th>book</th><th>book_spoke</th><th>recordType</th><th>book_title</th><th>chapter</th><th>chapter_spoke</th><th>verse</th><th>verse_spoke</th><th>text_data</th></tr><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr><tr><td>2</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>2</td><td>2</td><td>And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</td></tr><tr><td>3</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>3</td><td>3</td><td>And God said, Let there be light: and there was light.</td></tr><tr><td>4</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>4</td><td>4</td><td>And God saw the light, that it was good: and God divided the light from the darkness.</td></tr><tr><td>5</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>5</td><td>5</td><td>And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.</td></tr><tr><td>6</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>6</td><td>6</td><td>And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.</td></tr><tr><td>7</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>7</td><td>7</td><td>And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so.</td></tr><tr><td>8</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>8</td><td>8</td><td>And God called the firmament Heaven. And the evening and the morning were the second day.</td></tr><tr><td>9</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>9</td><td>9</td><td>And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so.</td></tr><tr><td>10</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>10</td><td>10</td><td>And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good.</td></tr><tr><td>11</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>11</td><td>11</td><td>And God said, Let the earth bring forth grass, the herb yielding seed, and the fruit tree yielding fruit after his kind, whose seed is in itself, upon the earth: and it was so.</td></tr><tr><td>12</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>12</td><td>12</td><td>And the earth brought forth grass, and herb yielding seed after his kind, and the tree yielding fruit, whose seed was in itself, after his kind: and God saw that it was good.</td></tr><tr><td>13</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>13</td><td>13</td><td>And the evening and the morning were the third day.</td></tr><tr><td>14</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>14</td><td>14</td><td>And God said, Let there be lights in the firmament of the heaven to divide the day from the night; and let them be for signs, and for seasons, and for days, and years:</td></tr><tr><td>15</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>15</td><td>15</td><td>And let them be for lights in the firmament of the heaven to give light upon the earth: and it was so.</td></tr><tr><td>16</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>16</td><td>16</td><td>And God made two great lights; the greater light to rule the day, and the lesser light to rule the night: he made the stars also.</td></tr><tr><td>17</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>17</td><td>17</td><td>And God set them in the firmament of the heaven to give light upon the earth,</td></tr><tr><td>18</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>18</td><td>18</td><td>And to rule over the day and over the night, and to divide the light from the darkness: and God saw that it was good.</td></tr><tr><td>19</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>19</td><td>19</td><td>And the evening and the morning were the fourth day.</td></tr><tr><td>20</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>20</td><td>20</td><td>And God said, Let the waters bring forth abundantly the moving creature that hath life, and fowl that may fly above the earth in the open firmament of heaven.</td></tr><tr><td>21</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>21</td><td>21</td><td>And God created great whales, and every living creature that moveth, which the waters brought forth abundantly, after their kind, and every winged fowl after his kind: and God saw that it was good.</td></tr><tr><td>22</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>22</td><td>22</td><td>And God blessed them, saying, Be fruitful, and multiply, and fill the waters in the seas, and let fowl multiply in the earth.</td></tr><tr><td>23</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>23</td><td>1</td><td>And the evening and the morning were the fifth day.</td></tr><tr><td>24</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>24</td><td>2</td><td>And God said, Let the earth bring forth the living creature after his kind, cattle, and creeping thing, and beast of the earth after his kind: and it was so.</td></tr></table>

  3. #3
    Join Date
    Jul 2004
    Posts
    494
    It seems that the problem comes from before that:
    PHP Code:
        $td $dom->getElementsByTagName('td');
        
    $numRecordsPerPage 20;
        
    $offset $numRecordsPerPage * ($page 1);    
        
        
    $i $offset;
        foreach( 
    $td as $value ){
            if(
    count($tdArray[$i]) != $count){
                
    $tdArray[$i][] = $value->nodeValue;
            }else{
                
    $i++;
                
    $tdArray[$i][] = $value->nodeValue;
            }
        } 
    where $td is collecting all 24 records. And the foreach loop is assigning the value of $tdArray[20] to the first record and not the 20th in page 2.
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #4
    Join Date
    Jul 2004
    Posts
    494

    pagination for $td = $dom->getElementsByTagName('td');

    Maybe a more accurate title is how to use pagination for selecting:
    PHP Code:
    $td $dom->getElementsByTagName('td'); 
    This would select all 31102 records. But I want each page to select maybe 500 records per page.
    Compare bible texts (and other tools):
    TheWheelofGod

  5. #5
    Join Date
    Sep 2010
    Posts
    27
    Well i am not an expert. but i guess your approach towards pagination is wrong.
    why don't you use pagination file as include? why do you want to collect data from td?
    feel free if you want me to share my pagination file with you. You can use it as include. You will only have to declare a couple of variables in the file where it's included. And every thing will be fine.
    Last edited by belask; 04-01-11 at 18:18.

Posting Permissions

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