Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Want add a promo code box to form?
Old 10-06-2010, 07:05 PM Want add a promo code box to form?
virtualgeorge's Avatar
Average Talker

Posts: 29
Trades: 0
I created a form here: http://nonstopshuttleservice.com/point_point_res.htm

It calculates the cost of the ride and adds for things like number of passengers & if they want to add a gratuity.

Would it be possible to check for the value entered into the text box and see if matches my promo code so it would equal a number like 5 or 10 for a 5 or 10 dollar discount. Once I get
$promo=5;
I know how to use that to deduct from my total but I don't know how to use an if statement to see if what they typed in the box equals my promo code.

Could someone possibly give me an example if this is possible?

thanks!
virtualgeorge is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-06-2010, 07:39 PM Re: Want add a promo code box to form?
portkey's Avatar
Skilled Talker

Posts: 55
Name: Erica Dion
Trades: 0
Are you using PHP to check for this after they click submit? If so, you be able to check for it using a simple if statement.

PHP Code:
if($promo == 5) {
   
//statements go here if true
} else {
   
//statement go here if false

Also, if you have more than one promo code you could use an array.

PHP Code:
$promo_codes = array(5,10); //separate promo codes with a comma
if(in_array($promo,$promo_codes)) {
   
//statements go here if true
} else {
   
//statement go here if false

__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
Old 10-06-2010, 09:52 PM Re: Want add a promo code box to form?
virtualgeorge's Avatar
Average Talker

Posts: 29
Trades: 0
So if they entered a code in the text box "promocode" and they need to enter the word "sale" for the promo code to equal a 5 dollar discount. Then if they enter the correct promo code "sale" into the text box, I need a variable like $discount=5, so:
Code:
if($promocode == sale) {
   $discount=5
} else {
   $discount=0
}
Would that work?

Then in my php form I could have something like:
Code:
$total=$subtotal-$discount
to display the total minus the discount.

thanks!

Got it figured out, thanks for the help!!

Last edited by virtualgeorge; 10-06-2010 at 10:56 PM..
virtualgeorge is offline
Reply With Quote
View Public Profile
 
Old 10-07-2010, 12:16 AM Re: Want add a promo code box to form?
virtualgeorge's Avatar
Average Talker

Posts: 29
Trades: 0
Sorry but didn't figure out the array.
If I use this for one code it works:
Code:
if($promocode == 'sale') {;
   $discount=5; 
} else {
   $discount=0;
};
If I try this I get an error on this line:
$promo1=5,$promo2=5;

Code:
$promo_codes = array(sale,bucks); 
if(in_array($promo1,$promo2)) {;
   $promo1=5,$promo2=5;
} else {
   $promo1=0,$promo2=0;
};
Any help appreciated

Last edited by virtualgeorge; 10-07-2010 at 12:17 AM..
virtualgeorge is offline
Reply With Quote
View Public Profile
 
Old 10-07-2010, 02:51 PM Re: Want add a promo code box to form?
portkey's Avatar
Skilled Talker

Posts: 55
Name: Erica Dion
Trades: 0
Well firstly, you can't declare two varies on a single line like that.

Secondly, you're not using the in_array() function properly because you need to actually give the array. The declaration for the function is:
PHP Code:
in_array(needle,haystack
The needle being the promo code you're looking for, and the haystack being the array of good promo codes you're looking for. If you have a word for every discount, then you can use a 2D array to store them.

PHP Code:
$promo_codes = array("sale5"=>5,"sale10"=>10); //separate promo codes with a comma
if(in_array($promo,$promo_codes)) {
   
$discount=$promo_codes[$promo];
} else {
   
$discount=0;
}
$total=$subtotal-$discount
So every value in the array will look like this, each one separated by commas:
PHP Code:
"PROMO_CODE"=>DISCOUNT_VALUE 
Hope that makes sense :-) It's a last faster and shorter than using a series of IF statements for every code you have.
__________________
Hysterical Web Design

Please login or register to view this content. Registration is FREE

Fair priced and reliable web design services that complement your website needs
portkey is offline
Reply With Quote
View Public Profile Visit portkey's homepage!
 
Old 10-07-2010, 03:27 PM Re: Want add a promo code box to form?
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
What if you plan to give away more than a hundred promo codes?
Better way to do this is to use database instead of array.
You need only two fields.
One would be if a promo code has already been used (0 or 1) and the second is generated by you:
PHP Code:
$promocode rand(9999999999); //generates a random number from 0..9999999999 
miki86 is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to Want add a promo code box to form?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.65528 seconds with 12 queries