Jump to content

calling a function in an if statement


ryanlitwiller

Recommended Posts

I am trying to call a user defined function in an if statement when the condition is true.

if($number_of_rows_after>$number_of_rows_before) 
     {
      myFunction();
      echo "this my if";
     }
     else{
     echo "this is my else";
     }

myFunction() is getting called regardless of whether or not my condition is true or false

 

so i will have "this is my else" echoed and myFuction() runs

Link to comment
https://forums.phpfreaks.com/topic/287639-calling-a-function-in-an-if-statement/
Share on other sites

Well I will try to prove,

 

This is my main php file and the if statement is toward the bottom

 

indexhelp.php

// Select the database to work with
	$db = mysql_select_db('test',$link);
	if(!$db) {
		die('Selected database unavailable: ' . mysql_error());
	}
	// Include the Mail package
	include("sendMail.php");
	// import simple_html_dom.php to give me various methods for website selection and scraping
	include("simple_html_dom.php");
	// get DOM from BPL URL
	$html0 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?forum=19');
	$html1 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?offset=25&forum=19');
	$html2 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?offset=50&forum=19');
	$html3 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?offset=75&forum=19');
	
	 
	
function makePage($html)
{	
	$link_number = -1;//will use this as a new array key number in a foreach
    $meta_array = array();//making sure there is an array when functions call upon it to eliminate those errors
    foreach($html->find('td.forum_listing') as $e) {
    
        foreach($e->find('p.meta') as $f){//find all meta id's
        $meta = $f->plaintext;
        //echo $meta;
        $explode_meta = explode(" ",$meta);//explode by spaces and make an array        
        
        if(!ctype_digit($explode_meta[0])){//only if array value content does not have just positive whole numbers
        $link_number++;//adds 1 to array key number, same as $link_number = $link_number + 1;
 
        //build a new array by key number value and name associations
        $meta_array[$link_number] = array(
        "date" => $explode_meta['0'],
        "time" => $explode_meta['1'],
        "timezone" => str_replace("by","",$explode_meta['2']),
        "username_first" => $explode_meta['3'],
        "username_last" => $explode_meta['4']
        );
        }
        }
        
    }

// find all td tags with class=forum_listing
$link_number = -1;//reset number
    foreach($html->find('td.forum_listing') as $tdTagExt){
        foreach($tdTagExt->find('a')as $aTagExt){
        $link_number++;//add by one again
            $title = $aTagExt->plaintext;
            $href = "http://www.backpackinglight.com".$aTagExt->href;
            //associating the link and title in a new array
            $meta_array[$link_number]['title'] = $title;
            $meta_array[$link_number]['href'] = $href;
        }
    }
    
//print_r($meta_array)."<br />";//to see how the array looks    

    //access the new links array
    foreach($meta_array as $link){

    echo "<p>";
    echo "<a href='".$link['href']."' target='_blank'>".$link['title']."</a><br />";
    echo " Date: ".$link['date']." Time: ".$link['time']." ".$link['timezone']."<br />"; //Posted by: ".$link['username_first']." ".$link['username_last']."<br />";
    echo "</p>";
		    //convert array to string for db
		    $link_href = $link['href'];
		    $link_title = $link['title'];
		    $link_date = $link['date'] . " " . $link['time'];
		   
		    $result = mysql_query("select * from bp");
		    $number_of_rows_before = mysql_num_rows($result);
		    echo "Number of rows fetched are : ". $number_of_rows_before;
		    //populate db
		    mysql_query("insert into `bp` (`url`,`title`,`date`) values ('$link_href','$link_title','$link_date')");
		    $number_of_rows_after = mysql_num_rows($result);
		    echo "Number of rows fetched are : ". $number_of_rows_after;
		    
		    	// send myself email after every new db entry
			    if($number_of_rows_after>$number_of_rows_before) 
			    	{
			    	sendMail();
			    	echo "this my if";
			    	}
			    else{
					echo "this is my else";
					}	
		  
    }
}
makePage($html0);
makePage($html1);
makePage($html2);
makePage($html3);

// Close the connection
mysql_close($link);

?>

here is my sendMail.php which has my sendMail function

<?php
function sendMail()
{
	// Include the Mail package
	require "Mail.php";

	// Identify the sender, recipient, mail subject, and body
	$sender    = "[email protected]";
	$recipient = "[email protected]";
	$subject   = "Test mail";
	$body      = "New Post!";

	// Identify the mail server, username, password, and port
	$server   = "ssl://smtp.gmail.com";
	$username = "[email protected]";
	$password = "*********";
	$port     = "465";

	// Set up the mail headers
	$headers = array(
			"From"    => $sender,
			"To"      => $recipient,
			"Subject" => $subject
	);

	// Configure the mailer mechanism
	$smtp = Mail::factory("smtp",
			array(
					"host"     => $server,
					"username" => $username,
					"password" => $password,
					"auth"     => true,
					"port"     => 465
			)
	);

	// Send the message
	$mail = $smtp->send($recipient, $headers, $body);

	if (PEAR::isError($mail)) {
		echo ($mail->getMessage());
	}
}

sendMail()
?>

My live site is http://php-ryanlitwiller.rhcloud.com/indexhelp.php and even when my else echo is displayed on the screen I still get an email from my sendMail() function

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.