Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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!||||

  • #3
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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 11:46 AM.

  • Users who have thanked jmj001 for this post:

    graphixal (01-29-2012)

  • #4
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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 05:35 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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...

  • Users who have thanked jmj001 for this post:

    graphixal (01-29-2012)

  • #6
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #7
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Maybe something just like

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

  • Users who have thanked weir-07 for this post:

    graphixal (01-30-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •