Solved

Passing a variable from a Class method

Posted on 2015-02-13
4
64 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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

912 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

16 Experts available now in Live!

Get 1:1 Help Now