Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,487 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,698 people online right now. Registration is fast and FREE... Join Now!




if statement as a variable

 
Reply to this topicStart new topic

if statement as a variable

skirkpatrick
6 Mar, 2008 - 02:12 PM
Post #1

New D.I.C Head
*

Joined: 18 Feb, 2008
Posts: 11





I am trying to create a variable using an if/else statement in order to be able to pass the variable to the next page.

The if/ else is a simple math function that take a beginning quantity from the database and subtract another number from the beginning quantity - if the result is less than 0 then display 0. If not display the resulting number.

I want the results to be a variable that I can then pass along to the database.

CODE
if($products['products_quantity'] - $_POST['subtractnumber'] <= 0) {
        echo "0";
        }
        else  {
        echo $products['products_quantity'] - $_POST['subtractnumber'];
        }


I am going somewhat brain dead on this

any help is greatly appreciated

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: If Statement As A Variable
6 Mar, 2008 - 02:26 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I am not completely sure what you are having trouble with. Your example is very much on the right track.

php

// Set a default quantity for our variable
$newqty = 0;

// If they pass a subtract number and that number is greater than zero
if (isset($_POST["subtractnumber"]) && ($_POST["subtractnumber"] > 0)) {

// Find the new quantity
$newqty = $products['products_quantity'] - $_POST['subtractnumber'] ;

// If the new quantity is less than zero, set it to zero
// (you might also want them to know there is not that much to subtract)
if ($newqty <= 0) { $newqty = 0; }
}

echo "Quantity is: $newqty";

// Now lets pass to a database...

$sql = "Update products set quantity = $newqty where productID = 2";

// Go one executing the query on your database...


I hope this is what you were asking. The $newqty variable would be your variable to do whatever you want with and update the database if so desired.

Enjoy!

"At DIC we be new quantity calculating code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

skirkpatrick
RE: If Statement As A Variable
7 Mar, 2008 - 09:08 AM
Post #3

New D.I.C Head
*

Joined: 18 Feb, 2008
Posts: 11

Thank you - That helped immensly. I knew I was close but just couldn't get it. Sometimes you are too close to the trees to see the forest.

Thanks Again.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:30PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month