Solved

Passing a variable from a Class method

Posted on 2015-02-13
4
62 Views
Last Modified: 2015-02-13
I have the following class and method.  At the bottom of the method I have created a variable $html.  When I call my function in the html I want the variable $html to be available to echo out in my html page.  I'm not sure how to do this.

<?php

class Quote {
		
	private $EE;
	

	
	/**
	 * Constructor
	 */
	public function __construct()
	{
		$this->EE =& get_instance();
		
	}

	
	
	 
	public function renderQuote( $html ) {

    $ip = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

	if(isset($_POST['get_quote'])) { // Handle the form.)
		$bv = $_POST['bike_value'];
		$bs = $_POST['bike_state'];
		$be = $_POST['email_value'];
		
						
		$email_query =  ee()->db->query("SELECT email_address FROM exp_bicycle_addresses WHERE email_address='$be'");
				
		$emthere = $email_query->result();
			
		
			if(!$emthere)	{

	//  START INSERT EMAIL ADDRESS INTO DB
				$email_data = array('email_address'=> $be, 'date'=> 'NOW()', 'ip'=> $ip);
				$sql_e = ee()->db->insert_string('exp_bicycle_addresses', $email_data);
				
				$DB->query($sql_e);
	//  END INSERT EMAIL INTO DB
			} 
			
		
//  START CREAT QUOTE
		$create_quote = ee()->db->query("SELECT id, state, pd_base_rate FROM exp_state_rates WHERE id = '$bs'");
		
		$state_data = $create_quote->result();

			
		foreach ($state_data AS $row):
			
				$div = 100;
    			$id = $row->id;
				$st = $row->state; 
		 		$br = $row->pd_base_rate;
		endforeach;
    			$rate = $bv/$div;
				$sub_price = $br * $rate;
			[b]	$html = $sub_price;[/b]
				$html = number_format($html, 2, '.', ',');
				$month = 12;
				$month = $html/$month;
				$month = number_format($month, 2, '.', ',');

				if ($html > 100) {
					echo '<input type="hidden" name="state_value" value="'.$html.'" />';
					echo '<br />Your Estimated Base Rate:  &nbsp;&nbsp;<span class="price-lrg">  $<span id="estimate_year" class="price">'.$html.'</span> per year</span>';
				}

				if ($html < 100)	{
					echo '<input type="hidden" name="state_value" value="100" />';
					echo'<br />Your Estimated Base Rate:  &nbsp;&nbsp; <span class="price-lrg">$100 per year</span>';
				}
?>

<div style="clear:both;padding:0px 0px 13px 0px;">
	Includes Theft, Damage, Worldwide Damage Coverage, Rental Reimbursement and Roadside Assistance
</div>
<div class="clear"></div>
<h1 itemprop="name" class="product_title entry-title">Custom Bicycle Insurance From Spoke</h1>

		<?php

		}  //  END Form isset
		
	}  //  END Function
}  //  END Class
?>

<html>
<?php
$q = new Quote;
echo $q->renderQuote();

[b]echo $html;[/b]
?>

</html>

Open in new window

0
Comment
Question by:rgranlund
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
dale_abrams earned 500 total points
ID: 40608429
Try this...
<?php

class Quote {
		
	private $EE;
	private $html;
	

	
	/**
	 * Constructor
	 */
	public function __construct()
	{
		$this->EE =& get_instance();
		$this->html = '';
		
	}

	
	
	 
	public function renderQuote( $html ) {

    $ip = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

	if(isset($_POST['get_quote'])) { // Handle the form.)
		$bv = $_POST['bike_value'];
		$bs = $_POST['bike_state'];
		$be = $_POST['email_value'];
		
						
		$email_query =  ee()->db->query("SELECT email_address FROM exp_bicycle_addresses WHERE email_address='$be'");
				
		$emthere = $email_query->result();
			
		
			if(!$emthere)	{

	//  START INSERT EMAIL ADDRESS INTO DB
				$email_data = array('email_address'=> $be, 'date'=> 'NOW()', 'ip'=> $ip);
				$sql_e = ee()->db->insert_string('exp_bicycle_addresses', $email_data);
				
				$DB->query($sql_e);
	//  END INSERT EMAIL INTO DB
			} 
			
		
//  START CREAT QUOTE
		$create_quote = ee()->db->query("SELECT id, state, pd_base_rate FROM exp_state_rates WHERE id = '$bs'");
		
		$state_data = $create_quote->result();

			
		foreach ($state_data AS $row):
			
				$div = 100;
    			$id = $row->id;
				$st = $row->state; 
		 		$br = $row->pd_base_rate;
		endforeach;
    			$rate = $bv/$div;
				$sub_price = $br * $rate;
			[b]	$html = $sub_price;[/b]
				$html = number_format($html, 2, '.', ',');
				$month = 12;
				$month = $html/$month;
				$month = number_format($month, 2, '.', ',');

				if ($html > 100) {
					$this->html .= '<input type="hidden" name="state_value" value="'.$html.'" />';
					$this->html .= '<br />Your Estimated Base Rate:  &nbsp;&nbsp;<span class="price-lrg">  $<span id="estimate_year" class="price">'.$html.'</span> per year</span>';
				}

				if ($html < 100)	{
					$this->html .= '<input type="hidden" name="state_value" value="100" />';
					$this->html .= '<br />Your Estimated Base Rate:  &nbsp;&nbsp; <span class="price-lrg">$100 per year</span>';
				}
				
				$this->html .= '<div style="clear:both;padding:0px 0px 13px 0px;">
	Includes Theft, Damage, Worldwide Damage Coverage, Rental Reimbursement and Roadside Assistance
</div>
<div class="clear"></div>
<h1 itemprop="name" class="product_title entry-title">Custom Bicycle Insurance From Spoke</h1>';
		}  //  END Form isset
	}  //  END Function
	
	public function getHTML(){
		return $this->html;
	}
}  //  END Class
?>

<html>
<?php
	$q = new Quote;
	$q->renderQuote();
	echo $q->getHTML();
?>
</html>

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40608432
Should be easy. Instead of echoing out the content inside renderQuote, you can just use an output buffer to capture it all into a variable, then return the variable.

You can do this in 3 simple changes:

Change:
public function renderQuote( $html ) {

Open in new window

to:
public function renderQuote() {
ob_start();

Open in new window


Change:
	}  //  END Function
}  //  END Class

Open in new window

to:
          return ob_get_clean();
	}  //  END Function
}  //  END Class

Open in new window


Change:
$q = new Quote;
echo $q->renderQuote();

[b]echo $html;[/b]

Open in new window

to:
$q = new Quote;
$html = $q->renderQuote();

[b]echo $html;[/b]

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40608438
I'm sorry, but your function accept $html variable as parameter, but when you call the function you're not pass anything.
In addition, class' methods should return a value and not print that value directly on screen: this would avoid also problems like the one on line 67 where html tag are merged into php code (... tag are not allowed in php code)
Anyway, to echo a variable, first you need to declare that variable as a class property:
class Quote {
		
	private $EE;
	public $html;

Open in new window

then, when the property $html has been processed you can do this:
$q = new Quote;
$q->renderQuote();
echo $q->html

Open in new window

Within the function renderQuote() you should refer to that variable as $this->html;

I didn't get through your code to undertana exactly what it does, but you should rewrite the function to return a string which then you can print. And you can write another function which processes $html property: you could call this function from within the first function to get the value of $html but you could also call this function from your code to echo $html only.
0
 
LVL 1

Expert Comment

by:dale_abrams
ID: 40608457
Here is some updated code that reflects the lack of $html variable passed in. Simply replace the '[HTML GOES HERE]' with your required HTML code in the form of a string.
<?php

class Quote {
		
	private $EE;
	private $html;
	

	
	/**
	 * Constructor
	 */
	public function __construct()
	{
		$this->EE =& get_instance();
		$this->html = '';
		
	}

	
	
	 
	public function renderQuote( $html ) {

    $ip = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

	if(isset($_POST['get_quote'])) { // Handle the form.)
		$bv = $_POST['bike_value'];
		$bs = $_POST['bike_state'];
		$be = $_POST['email_value'];
		
						
		$email_query =  ee()->db->query("SELECT email_address FROM exp_bicycle_addresses WHERE email_address='$be'");
				
		$emthere = $email_query->result();
			
		
			if(!$emthere)	{

	//  START INSERT EMAIL ADDRESS INTO DB
				$email_data = array('email_address'=> $be, 'date'=> 'NOW()', 'ip'=> $ip);
				$sql_e = ee()->db->insert_string('exp_bicycle_addresses', $email_data);
				
				$DB->query($sql_e);
	//  END INSERT EMAIL INTO DB
			} 
			
		
//  START CREAT QUOTE
		$create_quote = ee()->db->query("SELECT id, state, pd_base_rate FROM exp_state_rates WHERE id = '$bs'");
		
		$state_data = $create_quote->result();

			
		foreach ($state_data AS $row):
			
				$div = 100;
    			$id = $row->id;
				$st = $row->state; 
		 		$br = $row->pd_base_rate;
		endforeach;
    			$rate = $bv/$div;
				$sub_price = $br * $rate;
			[b]	$html = $sub_price;[/b]
				$html = number_format($html, 2, '.', ',');
				$month = 12;
				$month = $html/$month;
				$month = number_format($month, 2, '.', ',');

				if ($html > 100) {
					$this->html .= '<input type="hidden" name="state_value" value="'.$html.'" />';
					$this->html .= '<br />Your Estimated Base Rate:  &nbsp;&nbsp;<span class="price-lrg">  $<span id="estimate_year" class="price">'.$html.'</span> per year</span>';
				}

				if ($html < 100)	{
					$this->html .= '<input type="hidden" name="state_value" value="100" />';
					$this->html .= '<br />Your Estimated Base Rate:  &nbsp;&nbsp; <span class="price-lrg">$100 per year</span>';
				}
				
				$this->html .= '<div style="clear:both;padding:0px 0px 13px 0px;">
	Includes Theft, Damage, Worldwide Damage Coverage, Rental Reimbursement and Roadside Assistance
</div>
<div class="clear"></div>
<h1 itemprop="name" class="product_title entry-title">Custom Bicycle Insurance From Spoke</h1>';
		}  //  END Form isset
	}  //  END Function
	
	public function getHTML(){
		return $this->html;
	}
}  //  END Class
?>

<html>
<?php
	$q = new Quote;
	$q->renderQuote('[HTML GOES HERE]');
	echo $q->getHTML();
?>
</html>

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now