Undefined offset: 0

Discussion in 'PHP' started by gilgalbiblewheel, Mar 27, 2011.

  1. #1
    //looking for the ths
    $file="saveddbtable/bible.htm";
    $contents_of_page = file_get_contents($file);
     
    $dom = new DOMDocument();
    $dom->loadHTML($contents_of_page);
    // Initialize arrays
    $thArray = $tdArray = $array = array();
    // Get all Table Headers and throw them in an array
    $th = $dom->getElementsByTagName('th');
    foreach ( $th as $th ){
        $thArray[] = $th->nodeValue;
    }
    // count the array for future comparison
    $count = count($thArray);
    /* Example:
    Array(
    [0] => Unit Type
    [1] => Availability
    [2] => Rates
    )
    */
    // 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');
    $i = 0;
    foreach( $td as $td ) {
        if(count($tdArray[$i]) != $count){//line 101
            $tdArray[$i][] = $td->nodeValue;
        }else{
            $i++;
            $tdArray[$i][] = $td->nodeValue;
        }
        if($i=100){
            exit;
        }
    }
    echo '<pre>';
    print_r(array_combine($thArray, $tdArray));
    echo '</pre>';
    PHP:
    Line 101 says:
    I want to add a pagination at $i. Is it a good start? I limited $i at 100 and then exit the loop to test it first.
     
    gilgalbiblewheel, Mar 27, 2011 IP
  2. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    I want to extract the innerhtml from this table and store into an array:
    $contents_of_page="<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></table>";
    $dom = new DOMDocument();
    $dom->loadHTML($contents_of_page);
    // Initialize arrays
    $thArray = $tdArray = $array = array();
    // Get all Table Headers and throw them in an array
    $th = $dom->getElementsByTagName('th');
    foreach ( $th as $th ){
    	$thArray[] = $th->nodeValue;
    }
    // count the array for future comparison
    $count = count($thArray);
    /* Example:
    Array(
    [0] => Unit Type
    [1] => Availability
    [2] => Rates
    )
    */
    // 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');
    $i = 0;
    foreach( $td as $td ) {
    	if(count($tdArray[$i]) != $count){
    		$tdArray[$i][] = $td->nodeValue;
    	}else{
    		$i++;
    		$tdArray[$i][] = $td->nodeValue;
    	}
    	if($i=100){
    		exit;
    	}
    }
    echo '<pre>';
    print_r(array_combine($thArray, $tdArray));
    echo '</pre>';
    PHP:
     
    gilgalbiblewheel, Mar 28, 2011 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    	$file="saveddbtable/bible.htm";
    	$contents_of_page = file_get_contents($file);
    	$dom = new DOMDocument();
    	$dom->loadHTML($contents_of_page);
    	// Initialize arrays
    	$thArray = $tdArray = $array = array();
    	// Get all Table Headers and throw them in an array
    	$th = $dom->getElementsByTagName('th');
    	foreach ( $th as $th ){
    		$thArray[] = $th->nodeValue;
    	}
    	// count the array for future comparison
    	$count = count($thArray);
    	/* Example:
    	Array(
    	[0] => Unit Type
    	[1] => Availability
    	[2] => Rates
    	)
    	*/
    	// 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');
    	$i = 0;
    	foreach( $td as $td_field ){
    		if(count($tdArray[$i]) != $count){
    			$tdArray[$i][] = $td_field->nodeValue;
    		}else{
    			$i++;
    			if($i=50){
    				break;
    			}
    			$tdArray[$i][] = $td_field->nodeValue;
    		}
    
    	}
    	$numRecordsPerPage = 50;
    	$offset = $numRecordsPerPage * ($page - 1);
    	//$tdarr = implode("', '", array_slice($tdArray[0], 1));
    	
    	for($j = $offset; $j < $offset + $numRecordsPerPage; $j++){
    		// Add to insert statement
    		$tdarr = implode("', '", array_slice($tdArray[$j], 1));
    		$sql = "INSERT INTO ".$dbTable3." (".$fieldarr.") VALUES ('".$tdarr."')";
    		echo "<span style=\"color: green;\">".$sql."</span><br />\n";
    		//mysql_query($sql) or die(mysql_error());
    	}
    /*echo '<pre>';
    //print_r(array_combine($thArray, $tdArray));
    print_r($thArray);
    print_r($tdArray);
    echo '</pre>';*/
    PHP:
     
    gilgalbiblewheel, Mar 30, 2011 IP
  4. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Maybe
    $file="saveddbtable/bible.htm";
    $contents_of_page = file_get_contents($file);
    PHP:
    doesn't work with
     
    $dom = new DOMDocument();
    $dom->loadHTML($contents_of_page); 
    PHP:
    Because when the source is in the same php file it works fine:
    <!DOCTYPE html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
    <?php
        $text = '
            <table>
                <tr>
                    <th>Unit Type</th>
                    <th>Availability</th>
                    <th>Rates</th>
                </tr>
                <tr>
                    <td>One Bedroom</td>
                    <td>Call for Availability</td>
                    <td>hello</td>
                </tr>
                <tr>
                    <td>One Living Room</td>
                    <td>Call not for Availability</td>
                    <td>hello</td>
                </tr>
                <tr>
                    <td>Two Bedrooms</td>
                    <td>Not Availabile</td>
                    <td>Good Bye</td>
                </tr>            
            </table>';
        echo $text;
        $dom = new DOMDocument();
        $dom->loadHTML($text);
        // Initialize arrays
        $thArray = $tdArray = $array = array();
        // Get all Table Headers and throw them in an array
        $th = $dom->getElementsByTagName('th');
        foreach ( $th as $th ) {
            $thArray[] = $th->nodeValue;
        }
        // count the array for future comparison
        $count = count($thArray);
        /* Example:
            Array
            (
                [0] => Unit Type
                [1] => Availability
                [2] => Rates
            )
        */
        // 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');
        $i = 0;
        foreach( $td as $td ) {
            if(count($tdArray[$i]) != $count) {
                $tdArray[$i][] = $td->nodeValue;
            } else {
                $i++;
                $tdArray[$i][] = $td->nodeValue;
            }
        }
        echo '<pre>';
            print_r(array_combine($thArray, $tdArray));
        echo '</pre>';
    ?>         
    </body>
    
    </html>
    PHP:
     
    gilgalbiblewheel, Mar 30, 2011 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    There is something wrong with the $offset formula:
    	$numRecordsPerPage = 20;
    	$offset = $numRecordsPerPage * ($page - 1);
    PHP:
    So if $page=1 then $offset would result to 0.
     
    gilgalbiblewheel, Mar 31, 2011 IP