简体   繁体   中英

I need help developing a form

I'm trying to create a form that will create a table of pricing for t-shirts.

There are 10 shirts to choose from and each one is a different price. The S through XL are the same price but the XXL is a different price.

Next there is a selection for the color of the garment which also varys the price and there are 3 options White, Athletic, and Color.

The 3rd selection is the number of colors in the print for the shirt. Which there are 5 options (1-2, 3-4, 5-6, 7-8, 9-10 which affects the price.

Then I also have a few checkboxes which are add-ons that add to the price also.

The final data I want to show is a 2 row table showing in each column the price for 48pc, 72pc, 96pc, 144pc, 288pc. The first row will be the price for S-XL and the 2nd row would be for the XXL price.

I've created a super huge file that does the calculations and comes up with a cost but I know there is a easier way and a correct way that what I did. How would you guys do a file like this and can anyone help me out? I'm about to go crazy I been working on it for the past week and half straight.

I didn't post it because it was to long. I uploaded it to gitHub though heres the link: https://gist.github.com/1708558 GitHub

You are right, there is an easier way - you can write a php script that will generate all this code automatically!

For example, you create in your MySQL DB a table that will hold the < key,val > of all the shirts:

< DT0001 , DT0001SHIRTCOLOR > < DT000161 , DT000161SHIRTCOLOR > ...

and then you read the data from your PHP script and print/echo, for example:

echo "$(document).ready(function(){ $(\"#garmentselect\").change(function(){ ";

$con = mysql_connect("db-machine","username","password")
               or die("Unable to connect to SQL server");
mysql_select_db("schema") or die("Unable to select database");
$query = "select key,value from my_shirts;";
$result =  mysql_query($query) or die("No Records Found!");
while($row = mysql_fetch_array( $result )) {

    echo "if ($(this).val() == '".$row[key]."' ) { $('#".$row[val]."').slideDown("fast"); } else { $('#".$row[val].").slideUp("fast");}";
}
mysql_close($con);
echo "});"; // end of $("#garmentselect").change(...";
//continue ...

You can do this with most of your code and avoid duplication. In general, whenever you copy & paste your code and change only the parameters - you should think about creating a generalized method that will get the parameters as input and will generate the code you wanted to copy (this principle is called "code re-use").

Hope I helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM