...

View Full Version : getting head around functions



graphixal
01-28-2012, 10:56 PM
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.


<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...


<?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! :thumbsup:

_Aerospace_Eng_
01-29-2012, 01:08 AM
So what are you trying to get done with the function? The price?

jmj001
01-29-2012, 10:43 AM
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


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


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


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


$hemm = get_my_size($_GET['width'], $_GET['length'],'2.99')+$size;

graphixal
01-29-2012, 04:32 PM
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 -



...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:



....if etc
$price = $hemm;
displayHemmQuote();

break;

} else {


Cant find an example anywhere..

jmj001
01-29-2012, 05:45 PM
yes, a function can either return a value or echo stuff to screen directly

so your function can be



my_function('123'); // calling the function
function my_function($thisValue){
echo $thisvalue;
}
will print 123 to the browser

or


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...

graphixal
01-29-2012, 08:28 PM
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

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? :confused:

weir-07
01-29-2012, 08:50 PM
Maybe something just like


<? if ($_POST['Calculate']) {
THING TO SHOW ON SUBMIT } ?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum