
07-23-2008, 12:18 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
|
function not printing
I don't know if I missed something. The function is not printing at all.
PHP Code:
$contents_of_page = file_get_contents('../bible1.html');
preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML);
$totaltds = count($tdInnerHTML[1]);
print_r($tdInnerHTML[1]);//prints all TD contents
/********************************************************************************/
function recValues(){
$tdValues = array();
$tdValues = "";
for($j = 0; $j < $totaltds; $j++){
if($j % 10 != 0){
$tdValues .= "'".$tdInnerHTML[1][$j];
if($j < 9){
$tdValues .= "', ";
}else{
$tdValues .= "'";
}
}
}
return $tdValues;
}
echo "<span style='font-weight: bold;'>hi".recValues()."hi</span><br />\n";
|

07-23-2008, 09:05 PM
|
 |
Moderator
|
|
Join Date: May 2003
Posts: 1,438
|
|
|
PHP will exit a function as soon as it reaches a return statement. Therefore, any code you place after the "return" will never be executed. Put the echo statement before the return, and you'll be okay.
__________________
I hate Internet Explorer! Anyone with me?
|

07-24-2008, 07:40 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
I decided to rework the function from scratch. THis is what I came up with:
PHP Code:
function recValues(){
$contents_of_page = file_get_contents('../bible1.html');
preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML);
$totaltds = count($tdInnerHTML[1]);
$line = 0;
$strLine = array();
for($k=0; $k < $totaltds; $k++){
if($k % 10 == 0){
print("line ");
$strLine[$line] =
}
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
print("'");
print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
print("'<br />\n");
}else{
print("', ");
}
//return $tdValues[$j];
$j++;
$line++;
}
}
But I'm stuck. I need $strLine[$line] = to be equal to all this:
PHP Code:
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
print("'");
print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
print("'<br />\n");
}else{
print("', ");
}
|

07-24-2008, 08:22 PM
|
 |
Moderator
|
|
Join Date: May 2003
Posts: 1,438
|
|
I hope I'm understanding this correctly. I'm assuming that you are saying that you want to store the entire "if" statement you mentioned in your second code block into a string variable. Is that correct?
If so, here is all you need to do:
PHP Code:
function recValues(){ $contents_of_page = file_get_contents('../bible1.html'); preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML); $totaltds = count($tdInnerHTML[1]); $line = 0; $strLine = array(); for($k=0; $k < $totaltds; $k++){ if($k % 10 == 0){ print("line "); $strLine[$line] = ' if($k % 10 != 0){ $j = 0; $tdValues = array(); $tdValues[$j] = $tdInnerHTML[1][$k]; print("\'"); print("<span style=\'font-weight: bold; color: red\'>".$tdValues[$j]."</span>"); //print($tdInnerHTML[1][$k]); if($k % 10 == 9){ print("\'<br />\n"); }else{ print("\', "); }'; } //return $tdValues[$j]; $j++; $line++; } }
__________________
I hate Internet Explorer! Anyone with me?
Last edited by curtiss; 07-24-2008 at 08:26 PM.
|

07-24-2008, 08:37 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
Quote:
Originally Posted by curtiss
I hope I'm understanding this correctly. I'm assuming that you are saying that you want to store the entire "if" statement you mentioned in your second code block into a string variable. Is that correct?
If so, here is all you need to do:
PHP Code:
function recValues(){
$contents_of_page = file_get_contents('../bible1.html');
preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML);
$totaltds = count($tdInnerHTML[1]);
$line = 0;
$strLine = array();
for($k=0; $k < $totaltds; $k++){
if($k % 10 == 0){
print("line ");
$strLine[$line] = '
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
print("\'");
print("<span style=\'font-weight: bold; color: red\'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
print("\'<br />\n");
}else{
print("\', ");
}';
}
//return $tdValues[$j];
$j++;
$line++;
}
}
|
Why isn't it printing?
|

07-24-2008, 08:40 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
I see what's going on.
PHP Code:
function recValues(){
$contents_of_page = file_get_contents('../bible1.html');
preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML);
$totaltds = count($tdInnerHTML[1]);
$line = 0;
$strLine = array();
for($k=0; $k < $totaltds; $k++){
if($k % 10 == 0){
print("line ");
$strLine[$line] = '
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
print("\'");
print("<span style=\'font-weight: bold; color: red\'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
print("\'<br />\n");
}else{
print("\', ");
}';
}
print($strLine[$line]);
//return $tdValues[$j];
$j++;
$line++;
}
}
print($strLine[$line]); is printing the code on the page. But I need the result of the code inside the single quotes.
|

07-24-2008, 09:08 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
How about making $strLine[$line] equal to a function which would have:
PHP Code:
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
print("'");
print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
print("'<br />\n");
}else{
print("', ");
}
How does that work?
|

07-24-2008, 09:12 PM
|
 |
Moderator
|
|
Join Date: May 2003
Posts: 1,438
|
|
|
Gil - I don't think I can really help you much more unless I understand what it is you're ultimately trying to do.
The way your code is written, it's not really going to do anything at all. Unfortunately, I don't really understand what the goal is, so I can't begin to try to rewrite it to make it work for you.
Can you give me a short description of what it is you ultimately are trying to do?
__________________
I hate Internet Explorer! Anyone with me?
|

07-24-2008, 09:49 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
Quote:
Originally Posted by curtiss
Gil - I don't think I can really help you much more unless I understand what it is you're ultimately trying to do.
The way your code is written, it's not really going to do anything at all. Unfortunately, I don't really understand what the goal is, so I can't begin to try to rewrite it to make it work for you.
Can you give me a short description of what it is you ultimately are trying to do?
|
What happened was that I accidentally erased my db table. Fortunately I had a copy of the table in HTML. So I'm trying to reconvert it to a db table.
Another problem was that I don't know how to upload a db table to an online server and connect to it. But I know how to connect to a created db on the online server. So I need this technique in both cases. I'm almost done with this.
I need $strLine[$line] to equal to a function which would have:
PHP Code:
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
print("'");
print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
print("'<br />\n");
}else{
print("', ");
}
I came across something close to what I wanted:
PHP: create_function - Manual
The example:
PHP Code:
<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>
Is that all that you need to know? Or you want me to explain further?
|

07-24-2008, 11:50 PM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 247
|
|
I almost succeeded into building my sql statement. But here is the result:
Quote:
line
0 INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ()
'1',
'1', '1',
'1', '1', 'gn',
'1', '1', 'gn', 'Genesis',
'1', '1', 'gn', 'Genesis', '1',
'1', '1', 'gn', 'Genesis', '1', '1',
'1', '1', 'gn', 'Genesis', '1', '1', '1',
'1', '1', 'gn', 'Genesis', '1', '1', '1', '1',
'1', '1', 'gn', 'Genesis', '1', '1', '1', '1', 'In the beginning God created the heaven and the earth.'
9 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.')
line
'1',
'1', '1',
'1', '1', 'gn',
'1', '1', 'gn', 'Genesis',
'1', '1', 'gn', 'Genesis', '1',
'1', '1', 'gn', 'Genesis', '1', '1',
'1', '1', 'gn', 'Genesis', '1', '1', '2',
'1', '1', 'gn', 'Genesis', '1', '1', '2', '2',
18 INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ('1', '1', 'gn', 'Genesis', '1', '1', '2', '2', )
'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.'
line
'1',
'1', '1',
'1', '1', 'gn',
'1', '1', 'gn', 'Genesis',
'1', '1', 'gn', 'Genesis', '1',
'1', '1', 'gn', 'Genesis', '1', '1',
'1', '1', 'gn', 'Genesis', '1', '1', '3',
27 INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ('1', '1', 'gn', 'Genesis', '1', '1', '3', )
'3',
'3', 'And God said, Let there be light: and there was light.'
|
As you see there is a repetition. Instead it should be:
Quote:
line
9 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.')
line
27 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', '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.')
line
36 INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ('1', '1', 'gn', 'Genesis', '1', '1', '3', '3',
'And God said, Let there be light: and there was light.')
|
PHP Code:
function recValues(){
$contents_of_page = file_get_contents('../bible1.html');
preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML);
$totaltds = count($tdInnerHTML[1]);
$line = 0;
$strLine = array();
for($k=0; $k < $totaltds; $k++){
if($k % 10 == 0){
print("line ");
$strLine[$line] = "";
}
if($k % 10 != 0){
$j = 0;
$tdValues = array();
$tdValues[$j] = $tdInnerHTML[1][$k];
$strLine[$line] .= "'";
//print("'");
$strLine[$line] .= $tdValues[$j];
//print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>");
//print($tdInnerHTML[1][$k]);
if($k % 10 == 9){
$strLine[$line] .= "'";
//print("'<br />\n");
}else{
$strLine[$line] .= "', ";
//print("', ");
}
}
print($strLine[$line]."<br />\n");
//return $strLine[$line];
$j++;
if($k % 9 == 0){
$sql = "INSERT INTO bible (".fields().") VALUES (".$strLine[$line].")";
print("<span style='color: red;'>".$k." ".$sql."</span><br />\n");
$line++;
}
}
}
|

07-27-2008, 08:14 PM
|
 |
Moderator
|
|
Join Date: May 2003
Posts: 1,438
|
|
Gil - That's just because you're performing the following action every time the loop runs through a cycle:
PHP Code:
print($strLine[$line]."<br />\n");
The print statement you have after setting the $sql variable each time is plenty.
In order to avoid the first empty statement, I would recommend changing this statement:
PHP Code:
if($k % 9 == 0){
to
PHP Code:
if($k % 9 == 0 && !empty($k)){
__________________
I hate Internet Explorer! Anyone with me?
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
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
| | |