8 Replies - 1529 Views - Last Post: 25 April 2008 - 03:36 PM Rate Topic: -----

#1 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

php getting a variable to display from an if statement

Post icon  Posted 24 April 2008 - 02:17 PM

Hello, I'm trying to get the value of a variable to display using an if statement in a function.

here's the code from the html page (it's from the drop down menu)
<select name="product_a">
<option value="producto" selected>------- </option>
<option value="Capuchino">Capuchino </option>
<option value="Tazon">Tazon </option>
<option value="Tacita">Tacita </option>
</select>

when the user selects one of these items and submits the form I want display a value depending on what the user has selected. here's my php code. i can't get the output to display the price variables using my function aPrice()

<?php
$price_capuchino = 8;
$price_tazon = 12;
$price_tacita = 7;

function aPrice(){
if($product_a == Capuchino){
echo $price_capuchino;
}
if($product_a == Tazon){
echo $price_tazon;
}
if($product_a == Tacita){
echo $price_tacita;
}
}

$order = "Abajo es la informaccion por tu pedido<br><br>";
$order .= "$quantity_a x $product_a @<br>";

echo $order;

aPrice();
?>

Is This A Good Question/Topic? 0
  • +

Replies To: php getting a variable to display from an if statement

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: php getting a variable to display from an if statement

Posted 24 April 2008 - 02:30 PM

First of all, you should be looking at the $_POST global array for the value. It will be stored in the array element like so... $product_a = $_POST["product_a"];

Secondly, when you define your variables outside of the function, then want to use them in your function, you have to use the "global" keyword in your function so that it knows to access the higher scope variables.

Third I would pass the value from the $_POST to the function to then use for $product_a because without passing it to the function, the function is looking for a local copy yet again. So try something along these lines...

$price_capuchino = 8;
$price_tazon = 12;
$price_tacita = 7;

function aPrice($product_a){
     // Notice they are defined as global so it will access the ones defined out of the variable.
     global $price_capuchino, $price_tazon, $price_tacita;
 
     if($product_a == "Capuchino"){
          echo $price_capuchino;
     }
     if($product_a == "Tazon"){
          echo $price_tazon;
     }
     if($product_a == "Tacita"){
          echo $price_tacita;
     }
}

// Receive the data from the form through the POST array (assuming your form method = "post")
// and then pass it to the function. The function will then use the global variables.
aPrice($_POST["product_a"]);



Lastly, if you are going to compare values, remember they are strings so you need the double quotes around the words you are comparing to. Then go ahead with the rest of the program, either returning a value back from aPrice or using variables straight from $_POST. Hopefully all these fixes will work for you.

"At DIC we be price echoing code ninjas..ninjas..ninjasssss!" :snap:

This post has been edited by Martyr2: 24 April 2008 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Re: php getting a variable to display from an if statement

Posted 24 April 2008 - 02:56 PM

thanks so much. now, is it possible to take the value i get and make that into a variable so that i can use it in an 'echo' statement?



View PostMartyr2, on 24 Apr, 2008 - 02:30 PM, said:

First of all, you should be looking at the $_POST global array for the value. It will be stored in the array element like so... $product_a = $_POST["product_a"];

Secondly, when you define your variables outside of the function, then want to use them in your function, you have to use the "global" keyword in your function so that it knows to access the higher scope variables.

Third I would pass the value from the $_POST to the function to then use for $product_a because without passing it to the function, the function is looking for a local copy yet again. So try something along these lines...

$price_capuchino = 8;
$price_tazon = 12;
$price_tacita = 7;

function aPrice($product_a){
     // Notice they are defined as global so it will access the ones defined out of the variable.
     global $price_capuchino, $price_tazon, $price_tacita;
 
     if($product_a == "Capuchino"){
          echo $price_capuchino;
     }
     if($product_a == "Tazon"){
          echo $price_tazon;
     }
     if($product_a == "Tacita"){
          echo $price_tacita;
     }
}

// Receive the data from the form through the POST array (assuming your form method = "post")
// and then pass it to the function. The function will then use the global variables.
aPrice($_POST["product_a"]);



Lastly, if you are going to compare values, remember they are strings so you need the double quotes around the words you are comparing to. Then go ahead with the rest of the program, either returning a value back from aPrice or using variables straight from $_POST. Hopefully all these fixes will work for you.

"At DIC we be price echoing code ninjas..ninjas..ninjasssss!" :snap:

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: php getting a variable to display from an if statement

Posted 24 April 2008 - 03:14 PM

As I had mentioned towards the end, if you use the return statement instead of echo in your function you can have it return the appropriate price and store that into a variable like so...


$itemprice = aPrice($_POST["product_a"]);

echo "The price of the item you chose is: $itemprice";



I hope that is what you were looking for. :)
Was This Post Helpful? 0
  • +
  • -

#5 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Re: php getting a variable to display from an if statement

Posted 24 April 2008 - 04:38 PM

thanks so much. i am just learning so i appreciate your patience.

View Postsfw, on 24 Apr, 2008 - 02:56 PM, said:

thanks so much. now, is it possible to take the value i get and make that into a variable so that i can use it in an 'echo' statement?



View PostMartyr2, on 24 Apr, 2008 - 02:30 PM, said:

First of all, you should be looking at the $_POST global array for the value. It will be stored in the array element like so... $product_a = $_POST["product_a"];

Secondly, when you define your variables outside of the function, then want to use them in your function, you have to use the "global" keyword in your function so that it knows to access the higher scope variables.

Third I would pass the value from the $_POST to the function to then use for $product_a because without passing it to the function, the function is looking for a local copy yet again. So try something along these lines...

$price_capuchino = 8;
$price_tazon = 12;
$price_tacita = 7;

function aPrice($product_a){
     // Notice they are defined as global so it will access the ones defined out of the variable.
     global $price_capuchino, $price_tazon, $price_tacita;
 
     if($product_a == "Capuchino"){
          echo $price_capuchino;
     }
     if($product_a == "Tazon"){
          echo $price_tazon;
     }
     if($product_a == "Tacita"){
          echo $price_tacita;
     }
}

// Receive the data from the form through the POST array (assuming your form method = "post")
// and then pass it to the function. The function will then use the global variables.
aPrice($_POST["product_a"]);



Lastly, if you are going to compare values, remember they are strings so you need the double quotes around the words you are comparing to. Then go ahead with the rest of the program, either returning a value back from aPrice or using variables straight from $_POST. Hopefully all these fixes will work for you.

"At DIC we be price echoing code ninjas..ninjas..ninjasssss!" :snap:

Was This Post Helpful? 0
  • +
  • -

#6 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

Re: php getting a variable to display from an if statement

Posted 25 April 2008 - 02:06 AM

I hope nobody minds me jumping in here, but I have to say that Martyr's explanation is really helpful. Very clear for a beginner like myself - so an extra thank you.
Was This Post Helpful? 0
  • +
  • -

#7 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Re: php getting a variable to display from an if statement

Posted 25 April 2008 - 09:25 AM

Hi Martyr, here's another questions. after following your advice i got it functioning, but i want the users to be able to choose not only 'product_a' but 'product_b'......up to 'product_f'. my question is, do i have to put this code in 6 times for each product selection? this would be more than 100 lines of code. i have been studying loops, but i don't understand how to assign a counter to something other than a number. attached is my php code. thank you in advance.




View Postsmallfatguy, on 25 Apr, 2008 - 02:06 AM, said:

I hope nobody minds me jumping in here, but I have to say that Martyr's explanation is really helpful. Very clear for a beginner like myself - so an extra thank you.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: php getting a variable to display from an if statement

Posted 25 April 2008 - 09:31 AM

No that is the beauty of functions, you define them once and can call them with several pieces of data. All you need to do is pass each item to the function. So you will have one function and like 6 function calls...


$item1 = aPrice($_POST["product_a"]);
$item2 = aPrice($_POST["product_b"]);
$item3 = aPrice($_POST["product_c"]);
...

echo "Item 1 is priced: $item1 and Item 2 is priced: $item2 and Item 3 is priced: $item3";



Now of course if the prices change for each product and each product list is different, then you will have to come up with a more specific function to read the type of product passed to it and either do a switch statement or call other functions you create for each product etc. It all depends on what you are designing.

But if each product is of the specific types defined in aPrice, then you can pass each product to the function. :)



Smallfatguy, I appreciate the compliment. I try my best to give accurate descriptions and examples as many of the other talented staff here do. Glad it is appreciated. :)
Was This Post Helpful? 0
  • +
  • -

#9 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Re: php getting a variable to display from an if statement

Posted 25 April 2008 - 03:36 PM

Hi again,
thanks to your help i get it working.
my question: if a user only selects one product (they have option to select from 3 different drop down menus), a "0" reads for the price because that is the result of the $itemtotal variable. but is there a way to make nothing display in the total column if they don't select a product? i have the same problem with adding the dollar (peso in my case), but the sign is the same ($). because if a user only chooses one product, for the other two product select options the $itemtotal variables display as '0' and shows '$'. I attached my php code as phphelp.rtf and below are the links to the two pages that i am working with:

(this is the page that has the form)
http://www.rocavivac.../ws/pedidos.php

(this is the result of the order/pedido page)
http://www.rocavivac...onfirmacion.php

-mil gracias


View PostMartyr2, on 25 Apr, 2008 - 09:31 AM, said:

No that is the beauty of functions, you define them once and can call them with several pieces of data. All you need to do is pass each item to the function. So you will have one function and like 6 function calls...


$item1 = aPrice($_POST["product_a"]);
$item2 = aPrice($_POST["product_b"]);
$item3 = aPrice($_POST["product_c"]);
...

echo "Item 1 is priced: $item1 and Item 2 is priced: $item2 and Item 3 is priced: $item3";



Now of course if the prices change for each product and each product list is different, then you will have to come up with a more specific function to read the type of product passed to it and either do a switch statement or call other functions you create for each product etc. It all depends on what you are designing.

But if each product is of the specific types defined in aPrice, then you can pass each product to the function. :)



Smallfatguy, I appreciate the compliment. I try my best to give accurate descriptions and examples as many of the other talented staff here do. Glad it is appreciated. :)

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1