| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

03-27-11, 18:48
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
Undefined offset: 0
|
|
PHP Code:
//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>';
Line 101 says:
Quote:
|
Notice: Undefined offset: 0 in ...\create_bible.php on line 101
|
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.
|
|

03-27-11, 19:12
|
|
Jaded Developer
|
|
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
|
|
so what tdArray, where is it set
undefined offset usually means you are trying to access an unspecified index in an array.
is tdarray defines, if so where, and if so does it have a value
just as a start I doubt you can do
Code:
foreach( $td as $td ) {
try....
Code:
foreach( $td as $tdrow) {
..and then go on to use $tdrow in the rest of the block
why use an arbotray fiexed number of iterations
you could use the count($myArray) to find the number of items in the array, rememebr if using an integer index which uises a base zero so the first item in th earray is [0] the last item is count($myArray)-1
that way rouind you dont' have to worry about errors as you are always specifyiung a known array item.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
|
Last edited by healdem; 03-27-11 at 19:16.
|

03-28-11, 04:34
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
|
I want to extract the innerhtml from this table and store into an array:
PHP Code:
$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>';
|
|

03-30-11, 03:17
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
Quote:
Notice: Undefined offset: 0 in ...create_bible.php on line 102
INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ('1', '1', 'gn', 'Genesis', '1', '1', '1', '1', 'In the beginning God created the heaven and the earth.')
Notice: Undefined offset: 1 in ...create_bible.php on line 119
Warning: array_slice() expects parameter 1 to be array, null given in ...create_bible.php on line 119
Warning: implode() [function.implode]: Invalid arguments passed in ...create_bible.php on line 119
INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ('')
|
PHP Code:
$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>';*/
|
|

03-30-11, 04:28
|
|
Jaded Developer
|
|
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
|
|
I'd suggest you take it swtep by step and prove each block is working
Im not convinced (again) that
Code:
foreach ( $th as $th ){
is legit
I'd suggest you use the die statement to prove the code logic is right
... ferinstance
I'd want to make cvertain the number of th elements agreed with my test data, then I'd want to make certain the number TH elements was as expected and so onm
I'd ant to make certain the SQL is w2aht I think it is, so I'd want to see the actual SQL sent to the server not just the variable names
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
|
|

03-30-11, 12:11
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
I'm going to double check. The th is not even necessary to my coding but it was taken from a tested sample code which worked at the time. But I think I should erase that since the values of the th innerhtml are the names of the fields mentioned earlier in my code. And I'm not using the ths.
But here's the sample htm from which it's taking the info:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Bible Small Sample</title>
</head>
<body><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></body>
</html>
|
|

03-30-11, 12:25
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
I made some changes again in taking the th tag out because I'm not using it.
I'll post the entire php page but here's the section I'm working on:
PHP Code:
//looking for the ths
$file="saveddbtable/bible_small.htm";
$contents_of_page = file_get_contents($file);
$dom = new DOMDocument();
$dom->loadHTML($contents_of_page);
// Initialize arrays
$tdArray = $array = array();
// count the array for future comparison
$count = count($fields);
/* 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=20){
break;
}
$tdArray[$i][] = $td_field->nodeValue;
}
}
$numRecordsPerPage = 20;
$offset = $numRecordsPerPage * ($page - 1);
//$tdarr = implode("', '", array_slice($tdArray[0], 1));
for($j = $offset; $j < $offset + $numRecordsPerPage; $j++){
// Add to insert statement
$tdarr = implode("', '", $tdArray[$j]);
$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($fields, $tdArray));
print_r($fields);
print_r($tdArray);
echo '</pre>';
Entire code:
PHP Code:
<?php
/*originally ins_bible_indb.php now create_bible.php March 23, 2011*/
ob_start();
header("Content-Type: text/html; charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors', true);
set_time_limit(480);
include("includefiles/colors.php");
include("dbconnection.php");
/***************************/
$type=isset($_GET["type"]) ? $_GET["type"] : "";
$table=isset($_GET["table"]) ? $_GET["table"] : "";
//$page=isset($_GET["page"]) ? $_GET["page"] : "";
$page = isset($_GET["page"]) ? (int) $_GET["page"] : 1;
// Create database table
if (mysql_query("CREATE DATABASE IF NOT EXISTS ".$dbname, $con)){
echo "Database ".$dbname." created<br />\n";
}else{
echo "Error creating database ".$dbname.": " . mysql_error();
}
if($type=="delete"){
// Drop database table
$delete_sql = "DROP TABLE IF EXISTS ".$dbTable3;
mysql_select_db($dbTable3);
$retval = mysql_query($delete_sql, $con);
if(! $retval ){
die('Could not delete table: ' . mysql_error());
}
}
if($type=="create"){
//if($page==NULL){
// Create table leave this out book_title_gr mediumtext,
$sql = "CREATE TABLE IF NOT EXISTS ".$dbTable3."(
id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
book int(3),
book_spoke int(3),
recordType tinytext,
book_title mediumtext,
chapter int(3),
chapter_spoke int(3),
verse int(3),
verse_spoke int(3),
text_data longtext)";
//collect the newly created fields to insert values within
mysql_query($sql,$con) or die(mysql_error());
//}
echo "Table ".$dbTable3." created";
$sql = "SHOW FIELDS FROM ".$dbTable3;
$result = mysql_query($sql) or die(mysql_error());
$fields = array();
while ($show_column_names = mysql_fetch_array($result)){
if($show_column_names['Field']!=="id"){
$fields[]= $show_column_names['Field'];
}
}
$fieldarr = implode(", ", $fields);
//echo $fieldarr;
$addPage = $page+1;
$goto = $_SERVER['PHP_SELF']."?type=create&table=bible&page=".$addPage;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Page <?php echo $page;?> Inserting in the Bible Database Table</title>
</head>
<body>
<?php
/***********************************************************************************************************/
if($type=="create"){
//looking for the ths
$file="saveddbtable/bible_small.htm";
$contents_of_page = file_get_contents($file);
$dom = new DOMDocument();
$dom->loadHTML($contents_of_page);
// Initialize arrays
$tdArray = $array = array();
// count the array for future comparison
$count = count($fields);
/* 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=20){
break;
}
$tdArray[$i][] = $td_field->nodeValue;
}
}
$numRecordsPerPage = 20;
$offset = $numRecordsPerPage * ($page - 1);
//$tdarr = implode("', '", array_slice($tdArray[0], 1));
for($j = $offset; $j < $offset + $numRecordsPerPage; $j++){
// Add to insert statement
$tdarr = implode("', '", $tdArray[$j]);
$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($fields, $tdArray));
print_r($fields);
print_r($tdArray);
echo '</pre>';
/***********************************************************************************************************/
?>
<h3>Taken From: <a href="<?php echo $file; ?>" target="_blank"><?php echo $file; ?></a></h3>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?type=delete&table=bible">Delete Table</a>
<a href="<?php echo $goto; ?>">next</a>
<?php
}
if($type=="delete"){?>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?type=create&table=bible&page=1">Create Table</a>
<?php
}
?>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?show=all<?php
if($type=="create"){
echo "&type=create";
}
?>">Show all</a>
<?php
?>
<a href="?page=<?php echo $page + 1; ?>">Next page</a><?php
/*if($type=="create"){
//count($trInnerHTML[1])
for($td=1; $td<5; $td++){
preg_match_all($extract_td, $trInnerHTML[1][$td], $tdInnerHTML);
$implode_record = implode(", ", $tdInnerHTML[1]);
echo $implode_record;
}
}
if($type=="create"){
$num=round(count($trInnerHTML[1])/32);
echo "<span style=\"color: orange;\">".$num."</span><br />\n";
$start=$page*$num;
$limit=$page*$num;
$tds = array();
echo "page = ".$page;
for($getTds=$start; $getTds<$limit; $getTds++){//counting the trs
if($getTds!==0){
preg_match_all("#<td.*>(.+)</td#Ui", $trInnerHTML[1][$getTds], $tdInnerHTML);
//print_r($tdInnerHTML[1]);
echo "<span style=\"color: red;\">".count($tdInnerHTML[1])."</span>";
//echo "<br />\n";
for($splitTds=0; $splitTds<count($tdInnerHTML[1]); $splitTds++){
if($splitTds!==0){
$tds[] = addslashes($tdInnerHTML[1][$splitTds]);
echo "<span style=\"color: blue;\">".$tdInnerHTML[1][$splitTds]."</span><br />\n";
}
if($splitTds==count($tdInnerHTML[1])-1){
$tdarr = implode("', '", $tds);
$sql = "INSERT INTO ".$dbTable3." (".$fieldarr.") VALUES ('".$tdarr."')";
echo "<span style=\"color: green;\">".$sql."</span><br />\n";
mysql_query($sql) or die(mysql_error());
$tds = array();
}
}
}
}
}
*/
mysql_close($con);
if($type=="create"){
//then redirect them to the index
$add=$page+1;
echo "Location: create_bible.php?type=create&table=bible&page=".$add;
//header("Location: create_bible.php?type=create&table=bible&page=".$add);
}
?>
</body>
</html>
|
|

03-30-11, 13:14
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
Maybe
PHP Code:
$file="saveddbtable/bible.htm";
$contents_of_page = file_get_contents($file);
doesn't work with
PHP Code:
$dom = new DOMDocument();
$dom->loadHTML($contents_of_page);
Because when the source is in the same php file it works fine:
PHP Code:
<!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>
|
|

03-31-11, 09:52
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
There is something wrong with the $offset formula:
PHP Code:
$numRecordsPerPage = 20;
$offset = $numRecordsPerPage * ($page - 1);
So if $page=1 then $offset would result to 0.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|