• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 83
  • Last Modified:

Passing a variable from a Class method

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
rgranlund
Asked:
rgranlund
  • 2
1 Solution
 
dale_abramsCommented:
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
 
gr8gonzoConsultantCommented:
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
 
Marco GasiFreelancerCommented:
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
 
dale_abramsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now