function not printing

Discussion in 'PHP' started by gilgalbiblewheel, Jul 23, 2008.

  1. #1
    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";
    PHP:

     
    gilgalbiblewheel, Jul 23, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    $totaltds, and $tdInnerHTML do not exist in the scope of recValues()... make them global. Or pass them as parameter.
     
    nico_swd, Jul 23, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    How do you do that? I'm not too familiar in writing functions.
     
    gilgalbiblewheel, Jul 23, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    nico_swd, Jul 23, 2008 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Ok so I moved some things in the function.
    But another problem appears.The last $tdValues .= "'".$tdInnerHTML[1][$j]; shouldn't have ,' that follows it. I'm avoiding the multiples of 10 if($j % 10 != 0){
    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];
    			$tdValues .= "', ";
    			if($j % 10 != 0 + 9){
    				$tdValues .= "'";
    				$tdValues .= "<br />";
    				}
    			}
    		}
    	return $tdValues;
    	}
    	echo "<span style='font-weight: bold;'>".recValues()."</span><br />\n";
    PHP:
    So it's this part of the code that has to be fixed. I want to insert at every (multiple of 10) + 9. In other words 19, 29, 39...
    			if($j % 10 != 0 + 9){
    				$tdValues .= "'";
    				$tdValues .= "<br />";
    				}
    			}
    PHP:
     
    gilgalbiblewheel, Jul 23, 2008 IP