...

View Full Version : function not printing



gilgalbiblewhee
07-23-2008, 06:18 PM
I don't know if I missed something. The function is not printing at all.

$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";

Fou-Lu
07-23-2008, 07:04 PM
$tdInnerHTML[1] and $totaltds do not exist within the scope of the function recValues. You need to either accept it as a parameter, or globalize it - weight going towards the argument passing.


function recValues($tdArr)
{
if (!is_array($tdArr))
{
// Exceptions can only be thrown in php 5+, remove line if on php < 5, and return false
throw new Exception(sprintf("Argument passed to %s must be an array!", __FUNCTION__));
}

$tdValues = "";
for($j = 0; $j < count($tdArr); $j++)
{
if($j % 10 != 0)
{
$tdValues .= "'".$tdArr[$j];
if($j < 9)
{
$tdValues .= "', ";
}
else
{
$tdValues .= "'";
}
}
}
return $tdValues;
}

echo "<span style='font-weight: bold;'>hi".recValues($tdInnerHTML[1])."hi</span><br />\n";

Untested, but me thinks it will work.

gilgalbiblewhee
07-23-2008, 07:41 PM
I did the following changes and it worked:

function recValues(){
$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
$tdValues = array();
$tdValues = "";
for($j = 0; $j < $totaltds; $j++){
if($j % 10 != 0){
$tdValues .= "'".$tdInnerHTML[1][$j];
if($j % 10 == 9){
$tdValues .= "'";
$tdValues .= "<br />";
}else{
$tdValues .= "', ";
}
}
}
return $tdValues;
}
echo "<span style='font-weight: bold;'>".recValues()."</span><br />\n";
I get the following result:

'1', '1', 'gn', 'Genesis', '1', '1', '1', '1', 'In the beginning God created the heaven and the earth.'
'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.'
'1', '1', 'gn', 'Genesis', '1', '1', '3', '3', 'And God said, Let there be light: and there was light.'
'1', '1', 'gn', 'Genesis', '1', '1', '4', '4', 'And God saw the light, that it was good: and God divided the light from the darkness.'
...
But what I want to add is $k being equal to each line so that:

echo "<span style='font-weight: bold;'>".recValues()."</span><br />\n"; will not give the whole chunk of results. I'm not sure how I should manage to do that.

Fou-Lu
07-23-2008, 08:32 PM
I'm confused by what you would like for you're desired output, and this: 'But what I want to add is $k being equal to each line so that'.

If you give me an example of one of you're outputs and how you want it formatted within html, I can show you how to do that.

gilgalbiblewhee
07-23-2008, 08:55 PM
I want to create an array of $tdValues:

$tdValues[0]
$tdValues[1]
$tdValues[2]
...
each $tdValues[$k] should have one line:

'1', '1', 'gn', 'Genesis', '1', '1', '1', '1', 'In the beginning God created the heaven and the earth.'

Because later on I have:

for($k=0; $k < $totaltds; $k++)
$sql = "INSERT INTO bible (".fields().") VALUES (".recValues().")";
echo $sql."<br />\n";
}There is recValues(). $tdValues[$k] needs to somehow replace that.

Fou-Lu
07-23-2008, 11:35 PM
function recValues($tdArr)
{
if (!is_array($tdArr))
{
// Exceptions can only be thrown in php 5+, remove line if on php < 5, and return false
throw new Exception(sprintf("Argument passed to %s must be an array!", __FUNCTION__));
}

$tdValues = array();
$tdTmp = array();
for($j = 0; $j < count($tdArr); $j++)
{
$tdTmp[] = $tdArr[$j];
if (($j !== 0) && ($j % 9 == 0))
{
$tdValues[] = implode(', ', $tdTmp);
$tdTmp = array();
}
}
return $tdValues;
}


Lesse, would that work? For each of the items in the array, if index is not zero and has a remainder of zero when divided by 9, dump the tmp array imploded together into the tdvalues and reconstruct the tdtmp array.

Something like that what you are looking for? I assumed that the input array is one long array and thats why you were using the % 9 for it. I don't really recommend the use of file_get_contents within a function like this - it destroy's the reusability for it. Instead, use the file_get_contents and preg_match_all as the argument for it, this way you can use it with different data.

gilgalbiblewhee
07-25-2008, 01:34 AM
function recValues($tdArr)
{
if (!is_array($tdArr))
{
// Exceptions can only be thrown in php 5+, remove line if on php < 5, and return false
throw new Exception(sprintf("Argument passed to &#37;s must be an array!", __FUNCTION__));
}

$tdValues = array();
$tdTmp = array();
for($j = 0; $j < count($tdArr); $j++)
{
$tdTmp[] = $tdArr[$j];
if (($j !== 0) && ($j % 9 == 0))
{
$tdValues[] = implode(', ', $tdTmp);
$tdTmp = array();
}
}
return $tdValues;
}


Lesse, would that work? For each of the items in the array, if index is not zero and has a remainder of zero when divided by 9, dump the tmp array imploded together into the tdvalues and reconstruct the tdtmp array.

Something like that what you are looking for? I assumed that the input array is one long array and thats why you were using the % 9 for it. I don't really recommend the use of file_get_contents within a function like this - it destroy's the reusability for it. Instead, use the file_get_contents and preg_match_all as the argument for it, this way you can use it with different data.
I find that the solution you gave is getting complicated. This means I have to do a lot more reading than I can bear.

I decided to rework the function from scratch. THis is what I came up with:

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:

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("', ");
}

gilgalbiblewhee
07-25-2008, 05:54 AM
I almost succeeded into building my sql statement. But here is the result:


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:

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.')


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++;
}

}

}

Fou-Lu
07-25-2008, 08:59 AM
Yeah, the code I posted didn't work. So I altered the algorithm to account for 'row' like identifiers. This is what I used:


<?php

$arr = array(
'1', '1', 'gn', 'Genesis', '1', '1', '1', '1', 'In the beginning God created the heaven and the earth.',
'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.',
'1', '1', 'gn', 'Genesis', '1', '1', '3', '3', 'And God said, Let there be light: and there was light.',
'1', '1', 'gn', 'Genesis', '1', '1', '4', '4', 'And God saw the light, that it was good: and God divided the light from the darkness.',
);

function recValues($tdArr)
{
if (!is_array($tdArr))
{
// Exceptions can only be thrown in php 5+, remove line if on php < 5, and return false
throw new Exception(sprintf("Argument passed to %s must be an array!", __FUNCTION__));
}

$tdValues = array();
$tdTmp = array();
$tdCnt = count($tdArr);
for($j = 0, $i = 0; $j < $tdCnt; $j++)
{
if (($j != 0) && (($j + $i) % 10 == 9))
{
$i++;
$tdValues[] = implode('\', \'', $tdTmp);
$tdTmp = array();
}
$tdTmp[] = $tdArr[$j];
}
return $tdValues;
}

$newArry = recValues($arr);

for ($i = 0; $i < count($newArry); $i++)
{
// Give it a tester
echo "INSERT INTO bible (book, book_spoke, recordType, book_title, chapter, chapter_spoke, verse, verse_spoke, text_data) VALUES ('" . $newArry[$i] . "')\n";
}


?>

And this is my result:


X-Powered-By: PHP/5.2.5

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.')
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.')
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.')


Is this not what you are looking for it to do?

I would then create a new function to take care of reading from the file, generating the recValue array, and running a foreach to execute the insertion commands.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum