Syntax Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Syntax Help

Post by gkwhitworth »

Ok here's the gist. I am new at progamming so any help would be appreciated. I am creating a form for a user so they can have their clients get an estimate before they order their product. I wanted to use my newly, and I mean newly accquire "skills" in PHP to create this. What happens: they select what package they want, they then click submit and it produces a total on screen.

I did a search and found some php scripts for calculators and this is what I came up with. The main problem the php parsing is having is my if then statements. Here is the code:

Code: Select all

<?
						$package = $_REQUEST['package'];
						$basic = 1500;
						$professionalLite = 2300;
						$premiumProfessional = 3500;
						
						if ($package == basic) {
							$answer = $basic;
							}
							
						if ($package == professionalLite) {
							$answer = $professionalLite;
							}
							
						if ($package == premiumProfessional) {
							$asnwer = $premiumProfessional;
							}
						?>
There errors that are produced are:

Code: Select all

Notice: Use of undefined constant basic - assumed 'basic' in D:\users\something.com\contact\web_orders.php on line 83

Notice: Use of undefined constant professionalLite - assumed 'professionalLite' in D:\users\something.com\contact\web_orders.php on line 87

Notice: Use of undefined constant premiumProfessional - assumed 'premiumProfessional' in D:\users\something.com\contact\web_orders.php on line 91
I got the code idea from a forum earlier: here is the link:
http://www.lonford.co.uk/random/thispage.php

Tell me what I am doing wrong....

Also he has the following:
[qoute]
<?php
$foo = $_POST['foo'];
$bar = $_POST['bar'];
if ($foo AND $bar)
{
$answer = $foo + $bar;
}

?>
[/qoute]

the if ($foo AND $bar) part - does that have to be in there for this to work. Also my client is going to have multpile options that don't have to be chosen...so it is a bit more complex. Any help would be greatly appreciated.
Last edited by gkwhitworth on Mon Sep 11, 2006 3:32 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

string literals have to be marked for php (or they are considered constants as the warning messages states).

Code: Select all

if ($package == 'basic') {
I'd use some kind of mapping here, Simple example: an array

Code: Select all

<?php
$prices = array(
		'basic' => 1500,
		'professionalLite' => 2300,
		'premiumProfessional' => 3500
	);

if ( isset($_REQUEST['package']) ) {
	$package = $_REQUEST['package'];
	if ( isset($price[$package]) ) {
		$answer = $prices[$package];
	}
}
?>
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Can someone please explain this...

Post by gkwhitworth »

voka said:
if ( isset($_REQUEST['package']) ) {
$package = $_REQUEST['package'];
if ( isset($price[$package]) ) {
$answer = $prices[$package];
}
}
?>
I am new to php so I would greatly appreciate an explanation for what this part of the script is doing:

I know that the if(isset($_REQUEST['package']) is checking to see if the variable package is set. other than that I am lost.
Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Maybe this one helps understanding the "real" script

Code: Select all

<?php
$prices = array(
		'basic' => 1500,
		'professionalLite' => 2300,
		'premiumProfessional' => 3500
	);


echo $prices['basic'], "<br />\n";

$package = 'basic';
echo $prices[$package], "<br />\n";

$package = 'professionalLite';
echo $prices[$package], "<br />\n";

$package = 'premiumProfessional';
echo $prices[$package], "<br />\n";
?>
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

unfortionatley....no

Post by gkwhitworth »

I am sorry....like I said I am really new to this. I understand that you are echoing the price based on the selected package but I really dont understand the syntax I guess....why do

For instance:
echo $prices['basic'], "<br />\n";

$package = 'basic';
echo $prices[$package], "<br />\n";

$package = 'professionalLite';
echo $prices[$package], "<br />\n";
Why do you put $prices['basic'] and not say.... $prices['professionalLite']
then you are saying $package = 'basic';

when the package is only determined by the user thus shouldn't it be $package = $_REQUEST['package']
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

gkwhitworth, no matter what you may think, "PLEASE HELP" is an extremely thread title. Oddly enough, there's a forum rule governing this.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
I suggest you come up with a new title and edit your original post to reflect it.
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

Code: Select all

<?	
		
	$package = $_REQUEST['package'];
	$prices = array (
					'basic' => 1500,
					'professionalLite' => 2300,
					'premiumProfessional' => 3500
					);
					
	if (isset($_REQUEST['package'])) {
		$answer = $prices[$package];
		}					
	
	?>
Ok I am getting the following errors:

Code: Select all

Notice: Undefined index: Professional Lite in D:\users\gkwinspired.com\contact\web_orders.php on line 91
So what is the problem?????!!!!!!????? I also am wanting this to calculate the total and it isn't. Just in case here is the form code:

Code: Select all

<form method="POST" action="web_orders.php" method="post">
					
						<table border="0" width="100%" cellspacing="0" cellpadding="4" id="table1">
							<tr class="normal">
								<td width="99%" colspan="4"><h2>Personal Information</h2>
								</td>
							</tr>
							<tr class="normal">
								<td width="15%" align="right" class="commonBack">
								<b>Package:</b></p></td>
								<td width="27%" class="commonBack">
								<select size="1" name="package">
								<option value="Basic">Basic</option>
								<option value="Professional Lite">Professional Lite
								</option>
								<option value="Premium Professional">Premium Professional
								</option>
								</select></td>
								<td width="19%" class="commonBack">
								<p align="right"><b>Total:</b></td>
								<td width="38%" class="commonBack">
								<input type="text"  size="20" value="<?php print $answer; ?>" name="total"></td>
							</tr>
							</table>
						<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
					</form>
[/img]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You left out the line

Code: Select all

if ( isset($price[$package]) ) {
it's testing wether there is such an element in the array $prices or not. Professional Lite is not. There's no $prices['Professional Lite'] and that's why php is moarning.
There's an element $prices['ProfessionalLite'] without spaces in the name.
Post Reply