Jump to content

Need to use one variable to update another


dropbop

Recommended Posts

Hi, I have been on and off this forum loads of time and it has helped me out so much over the past few months with different projects. But today there is one thing i simply cant figure out or find any kind of answer anywhere and would be really grateful if someone could give me a pointer.

 

I have a form which which i use to input data into a mysql database. One of the variables

$budget=$_POST['budget'];

which I use to add data to the 'budget' field in my database.

 

In the form, budget is a dropdown with a few options:

<select name="budget" id="budget">
    <option selected="selected">Please Choose One</option>
    <option>up to 500 Euros</option>
    <option>500 to 1000 Euros</option>
    <option>1000 to 1500 Euros</option>
    <option>1500 to 3000 Euros</option>
    <option>3000 to 5000 Euros</option>
    <option>Over 5000</option>
    <option>No Budget Set Yet</option>
  </select>

 

I have another field in my database called 'price'. What I would like to do is, if someone chooses a budget of 3000 to 5000 I would like this to enter 5.00 in the price field in the database.

 

I have tried a number of things, but nothing is working for me. This is where I am at now, which doesn't work either but thought it might give a better explanation of what I am trying to achieve.

 

$budget=$_POST['budget'];
$price=$budget['up to 500 Euros']='3.50';
$price=$budget['500 to 1000 Euros']='5.00';
$price=$budget['1000 to 1500 Euros']='4.50';
$price=$budget['1500 to 3000 Euros']='5.00';
$price=$budget['3000 to 5000 Euros']='5.50';
$price=$budget['Over 5000']='6.00';
$price=$budget['No Budget Set Yet']='4.00';

 

Also keep in mind that there is no 'price' field in the form. So no price is being posted to the processing page. I need to price to be worked out by what is posted from the budget field.

 

This is the only thing I am stuck with without having to ask for any help, and these forums have been a saviour to me and an excellent place for learning. But sometimes I think you have to give in and just ask for help! lol

 

Many thanks,

 

DB

 

Link to comment
Share on other sites

First, change your form to this:

<select name="budget" id="budget">
    <option selected="selected">Please Choose One</option>
    <option value="3.50">up to 500 Euros</option>
    <option value="5.00">500 to 1000 Euros</option>
    <option value="4.50">1000 to 1500 Euros</option>
    <option value="5.00">1500 to 3000 Euros</option>
    <option value="5.50">3000 to 5000 Euros</option>
    <option value="6.00">Over 5000</option>
    <option value="4.00>No Budget Set Yet</option>
  </select>

We simply added the value="" so we have a nice clean value to give to the php script.

 

Now, we get to input it.

$budget = (float) $_POST['budget'];
$price = in_array($budget, array(3.50, 4.00, 4.50, 5.00, 5.50, 6.00)) ? $budget : 4.00;

Note that this code will sanitize your data and ensure you have a good value. It will default to 4.00 (which to my understanding means no budget set), but you can set it to anything you want, or even refuse to enter this in the database if they give a bad value.

Link to comment
Share on other sites

That has worked perfectly,

 

I haven't done anything in arrays really up to now, so this has been a great learning curve for me.

Just in case your wondering what I am doing, I am trying to use a use a form to import data as products to a zen cart site. And thanks to you I know how to set prices depending on what budget option is chosen and inserted into my products table, without having to go near the zencart admin.

 

Your help is very much appreciated indeed.

 

Hopefully I will be able to answer other peoples questions as I learn more and more.

 

Thanks again

 

DB

 

Link to comment
Share on other sites

Actually I think I may have been a little premature in checking the results properly.

 

I am finding that the price is being inserted as it should depending on the value selected from the 'budget' dropdown. But I also have a field in my database to insert the value from budget but now its inserting the price associated with the value. So for example, if someone selects 500 to 1000 Euros as a value, it is now inserted into the database as '5.00' instead of the string of text - '500 to 1000 Euros'.

 

It might be best explained if you could see it in action...

 

On this page www.timberdecks.ie there is a form that is filled in, which is then processed and the results inserted into the database.

Those results are actually products in a zencart site here: www.timberdecks.ie/trades/

 

I you were to put some sample data into the form and submit it, then to the zencart part of the site. You will notice the data you sent in the form for 'budget' is showing the price instead.

 

Here is a snippet from my form processing page:

 

<?php
$name=$_POST['name'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
$address=$_POST['address'];
$size=$_POST['size'];
$material=$_POST['material'];
$handrail=$_POST['handrail'];
$description=$_POST['description'];
$timeframe=$_POST['timeframe'];
$termsagree=$_POST['termsagree'];
$date= date("F j, Y");
$model = time();
$processed=$POST['processed'];
$county=$POST['Dublin']='1';
$county=$POST['Westmeath']='2';
$county=$POST['Galway']='3';
$county=$POST['Roscommon']='4';
$budget = (float) $_POST['budget'];
$price = in_array($budget, array(3.50, 4.00, 4.50, 5.00, 5.50, 6.00)) ? $budget : 4.00;

$query = "INSERT INTO zen_products (products_id, products_model, products_quantity, products_image, products_date_added, products_status, master_categories_id, products_price, manufacturers_id, products_size, products_material, products_handrail, products_timeframe, products_budget) VALUES ('$model', '$model','3','product.jpg', NOW(), '1','1', '$price', '0','$size', '$material', '$handrail', '$timeframe', '$budget')";
mysql_query($query) or die(mysql_error());
mysql_close();
?>

<?php
mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$query = "INSERT INTO zen_products_description (products_id, language_id, products_name, products_description) VALUES ('$model', '1', 'Quote Request $model', '$description')";
mysql_query($query) or die(mysql_error());
mysql_close();
?>

<?php
mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$query = "INSERT INTO zen_products_to_categories (products_id, categories_id) VALUES ('$model', '$county')";
mysql_query($query) or die(mysql_error());
mysql_close();
?>

 

 

Any ideas?

 

regards

 

DB

Link to comment
Share on other sites

Sorted it :)

 

$price = (float) $_POST['budget'];
$price = in_array($price, array(1, 2, 3, 4, 5, 6)) ? $price : 7;
$budget = $_POST['1']='Up to 500';
$budget = $_POST['2']='500 to 1000 Euros';
$budget = $_POST['3']='1000 to 1500 Euros';
$budget = $_POST['4']='1500 to 3000 Euros';
$budget = $_POST['5']='1000 to 2500 Euros';
$budget = $_POST['6']='1000 to 2500 Euros';
$budget = $_POST['7']='No Budget Set Yet';

 

Thanks to your code SamT.... much appreciated! :D:intoxicated: Drinks on me

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.