Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-28-2012, 10:56 PM   PM User | #1
graphixal
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
graphixal is an unknown quantity at this point
Smile getting head around functions

Hi, Im in the mix of learning PHP fully,

but I am struggling on this bit of code, it works fine, but I want to change it so it works with functions, as I will be using this bit of code on other pages.

Code:
<title>Size Quote</title>

</head> 
<body> 

<h1>Quote</h1> 

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "GET"> 

Width: <input type = "text" name = "width" size=4 > 
<br/> 
Length: <input type = "text" name = "length" size=4> 
<br> 

Hemming finish? <input type="checkbox" name ="hemming" value ="yes" /> 
<br/>
 
 <?php 

	if (isset($_GET["hemming"]) && ($_GET['calc'] == "yes")) { 
	$width = $_GET['width']; 
	$length = $_GET['length']; 
	$size = $width*$length*4.99;
	$hemm = $width*$length*2.99+$size;
	$price = $hemm;
	
	print "<h3>Quote</h3>"; 
	print "The price of the sheet: <br/><br/>
	$width x $length is $price <p>";
	
} else {
		$width = $_GET['width']; 
		$length = $_GET['length']; 
		$size = $width*$length*4.99;
		$price = $size;

		print "<h3>Quote</h3>"; 
		print "The price of the sheet: <br/><br/>
		$width x $length is $price <p>";
 } 
?>

<br />

<input type="hidden" name="calc" value ="yes"> <input type = "submit" name = "Calculate"/> </form>

</body>
</html>
I have tried this but still cant figure this simple function method out...

Code:
<?php 

function size($width, $length) {
	$width = $_GET['width'];
	$length = $_GET['length'];
	$size = $width*$length*4.99;
	return $size;
}

?>

<?php
	if (isset($_GET["hemming"]) && ($_GET['calc'] == "yes")) {   
	$hemm = $width*$length*2.99+$size;
	$price = $hemm;
	
	print "<h3>Quote</h3>"; 
	print "The price of the sheet: <br/><br/>
	$size $width x $length is $price <p>";
	
} else {
		$price = $size;

		print "<h3>Quote</h3>"; 
		print "The price of the sheet: <br/><br/>
		$width x $length is $price <p>";
 } 
?>

any help would be great!
graphixal is offline   Reply With Quote
Old 01-29-2012, 01:08 AM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
So what are you trying to get done with the function? The price?
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 01-29-2012, 10:43 AM   PM User | #3
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
think of a function as a process that takes in a specified set of information and returns you a value based on a commonly used set of calculations

you were very close with your example you just need to actually make use of the returned value

PHP Code:
    if (isset($_GET["hemming"]) && ($_GET['calc'] == "yes")) { 
    
$width $_GET['width']; 
    
$length $_GET['length']; 
    
$size $width*$length*4.99;
    
$hemm $width*$length*2.99+$size;
    
$price $hemm
needs to be

PHP Code:
if (isset($_GET["hemming"]) && ($_GET['calc'] == "yes")) { 
    
$size get_my_size($_GET['width'], $_GET['length'],'4.99');
    
$hemm $width*$length*2.99+$size;
    
$price $hemm
while including your function on the same page -- prob don;t use size as a function name it's too generic a word and may cause problems somewhere.. dunno where but i just wouldn't use it

PHP Code:
function get_my_size($width$length$calValue) {
    
$width $_GET['width'];
    
$length $_GET['length'];
    
$size $width*$length*$calValue;
    return 
$size;


if you want to make the function available for other scripts you need to put it in it's own page and then require_once(); that page at the top of the scripts that need access to it or other functions you have in your library

you can now use the same function to calculate the hemm size too

PHP Code:
$hemm get_my_size($_GET['width'], $_GET['length'],'2.99')+$size

Last edited by jmj001; 01-29-2012 at 10:46 AM..
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
graphixal (01-29-2012)
Old 01-29-2012, 04:32 PM   PM User | #4
graphixal
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
graphixal is an unknown quantity at this point
Thanks jmj001,

you have really helped me to understand functions...the other thing I am trying to figure out is how to output the print into another area of the page.

ie -

PHP Code:
       ...if etc
    
print "<h3>Quote</h3>"
    print 
"The price of the sheet: <br/><br/>" $_GET['width'] . " x " $_GET['length'] . " is $price <p>";

} else { 
could I tell it to display a function else where or show another div layer?

I was thinking something like:


PHP Code:
....if etc
$price 
$hemm;
displayHemmQuote();

break;

} else { 

Cant find an example anywhere..

Last edited by graphixal; 01-29-2012 at 04:35 PM..
graphixal is offline   Reply With Quote
Old 01-29-2012, 05:45 PM   PM User | #5
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
yes, a function can either return a value or echo stuff to screen directly

so your function can be


PHP Code:
my_function('123');  // calling the function
function my_function($thisValue){
     echo 
$thisvalue;

will print 123 to the browser

or

PHP Code:
echo my_function('123');  // calling the function
function my_function($thisValue){
     return 
$thisvalue;

will also print 123 to the browser

but in the second example the value is being returned for the echo to use where in the first, echo is in the function itself and no value is returned...
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
graphixal (01-29-2012)
Old 01-29-2012, 08:28 PM   PM User | #6
graphixal
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
graphixal is an unknown quantity at this point
Aha, great. Thanks alot.

What I have now is the form processing and showing the div layers, which I dont need it to. I have the layers showing on whichever function...but if there is no values it shows one of the divs still.

PHP Code:
<?php 

function get_my_size($width$length$calValue) { 
    
$width $_GET['width']; 
    
$length $_GET['length']; 
    
$size $width*$length*$calValue
    return 
$size
}    

 if (isset(
$_GET["hemming"]) && ($_GET['calc'] == "yes")) {  
    
$size get_my_size($_GET['width'], $_GET['length'],'4.99'); 
    
$hemm get_my_size($_GET['width'], $_GET['length'],'2.99')+$size;  
    
$pricehemm $hemm;
    echo 
"<div class=\"block\" style=\"display:block\">";
    
} else {
        
$size get_my_size($_GET['width'], $_GET['length'],'4.99');
        
$price $size;
        echo 
"<div class=\"price\" style=\"display:block\">";
 } 
?>

<input type="hidden" name="calc" value ="yes"> <input type = "submit" name = "Calculate"/> </form>
<br />
<div class="block" style="display:none;">

prnt Price etc
It works, and fades in, but at the load of the page the script executes and shows the latter div layer price class div.

I have tried to use another if isset and empty($width etc)..

is there anyway to stop the form executing until button is clicked? I thought this code was already using the button to execute the code, am I missing something?
graphixal is offline   Reply With Quote
Old 01-29-2012, 08:50 PM   PM User | #7
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
Maybe something just like

PHP Code:
<? if ($_POST['Calculate']) {
THING TO SHOW ON SUBMIT ?>
weir-07 is offline   Reply With Quote
Users who have thanked weir-07 for this post:
graphixal (01-29-2012)
Reply

Bookmarks

Tags
elseif, functions, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:39 AM.


Advertisement
Log in to turn off these ads.