Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    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"

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $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.
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    I did the following changes and it worked:
    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]);
        
    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:
    PHP Code:
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    I want to create an array of $tdValues:
    PHP Code:
    $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:
    PHP Code:
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    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 == 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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    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 == 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:
    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(
    "', ");
                } 
    Last edited by gilgalbiblewhee; 07-25-2008 at 01:39 AM.

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    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.')
    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 == 0){
                
                
    $sql "INSERT INTO bible (".fields().") VALUES (".$strLine[$line].")";
                print(
    "<span style='color: red;'>".$k." ".$sql."</span><br />\n");
                
    $line++;
            }
            
        }
        


  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?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:
    Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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