View Full Version : Get price from DB echo different price
PatrikIden
11-22-2011, 07:14 PM
I i have in my DB a field named "price" and on my webpage i have a mailform in witch a user can enter a price e.g 1500 or 3000, now depending on what the user posts i want to echo some other price (that is not in DB) on the webpage.
e.g, a user posts price 1500, i want to echo 100
or posts 3000 echo 200
Thank's
/Patrik.
keyboard
11-22-2011, 10:02 PM
Would this work for you
<?php
$price = $_POST["price"];
if($price == "3000"){
echo "200";
}
?>
change $_POST["price"]; to the name of the input of your html form.
From your question, I couldn't work out are you actually using a DB in this problem at all???
PatrikIden
11-23-2011, 12:42 AM
Would this work for you
<?php
$price = $_POST["price"];
if($price == "3000"){
echo "200";
}
?>
change $_POST["price"]; to the name of the input of your html form.
From your question, I couldn't work out are you actually using a DB in this problem at all???
Sorry i wasetn clear, yes i'm using DB
And i need a range 0-1500 = 100
1501-3000 = 200
Thanks for your reply.
PatrikIden
11-23-2011, 01:25 AM
So your code isent working for me. This is the code i use to connect to DB:
<?php
$databasename='****'; // Name of the database
$tablename='jobadd'; // Name of the table
$mysqladd='****'; // Address to the MySQL Server
$mysqluser='****'; // Your MySQL UserName
$mysqlpass='****'; // Your MySQL Password
//CONNECT TO MYSQL
$link=mysql_connect($mysqladd, $mysqluser, $mysqlpass) or die('Could not connect to database: ' . mysql_error());
//CONNECT TO DATABASE
mysql_select_db($databasename, $link) or die('Could not connect to table: ' . mysql_error());
$query="SELECT * FROM jobadd";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) :
?>
keyboard
11-23-2011, 02:06 AM
Could you please try to explain your problem a little more...
What are you using the price field in your database for?
You could use nested if statements...
<?php
$price = $_POST["price"];
if($price < "0"){
if($price > "1500"){
echo "200";
}
}
?>
In my example, $price is the value that the user entered.
Would the value of $price be retrieved from a database or your html form?
PatrikIden
11-23-2011, 10:49 AM
Thanks for yor reply. Ok so this i hove my site should work.
I have a menu whare a user can pick a kategori (ex, painter) then this user enters a job discription and a price and pers,info. And when this user submitts this info it is stored in DB, and the "price" field is named "Pris" in DB.
And now i have a page whare i show the info that the prev, user just entered, so that a company could ansver on that (i gues it's i cind of Ad) request. So depending on what price is put in by the user, a Company would have to pay an answering fee. I will use the following price range:
0-15000 = 51
15000-50000 = 81
50000-200000 = 189
200000-500000 = 289
500000-1000000 = 389
1000000-3000000 = 589
3000000-1000000 = 989
1000000-> = 1389
So in the page whare i show the DB data that user puts in i'm sending some data over to the anvering page and use the "GET" code to get info, so i actualy got your code to work by sending "Pris" over to the ansvering page. But i ame however calling DB connection eaven in the ansvering page, so "post" should work?
This is the "GET" code i use:
<?php
$price = $_GET["Pris"];
if($price <= "15000"){
echo "51";
}
?>
That worked!
Thank you.
PatrikIden
11-23-2011, 01:59 PM
Hi finaly i got this working is used this code: I dont know how to set the last piece doe. 10000000-? so i set one hundred one million, i dont think a user will post a project that costs more than that. But if you have any anwer pleas tell. Anyway Thank you for all your help. Maybe you could help me whit some other problems to if you have time (See other posts in PHP section).
<?php
$price = $_GET["Pris"];
if($price > "0"){
if($price < "15001"){
echo "51";
}
}
if($price > "15002"){
if($price < "50001"){
echo "81";
}
}
if($price > "50002"){
if($price < "200001"){
echo "189";
}
}
if($price > "200002"){
if($price < "500001"){
echo "289";
}
}
if($price > "500002"){
if($price < "1000001"){
echo "389";
}
}
if($price > "1000002"){
if($price < "3000001"){
echo "589";
}
}
if($price > "3000002"){
if($price < "10000001"){
echo "989";
}
}
if($price > "10000002"){
if($price < "100000001"){
echo "1389";
}
}
?>
PatrikIden
11-23-2011, 02:54 PM
Ohh no. This whas not a good idea, when i use "GET" it is wisable in the address field and then a Company can just change the price in the address field and then the answering price will be wrong?
Can you have a look and se if i can use "POST" and not "GET" some howe?
Thank you.
/Patrik.
keyboard
11-24-2011, 05:53 AM
On the page that you want to have this code-
<?php
$price = $_GET["Pris"];
if($price > "0"){
if($price < "15001"){
echo "51";
}
}
if($price > "15002"){
if($price < "50001"){
echo "81";
}
}
if($price > "50002"){
if($price < "200001"){
echo "189";
}
}
if($price > "200002"){
if($price < "500001"){
echo "289";
}
}
if($price > "500002"){
if($price < "1000001"){
echo "389";
}
}
if($price > "1000002"){
if($price < "3000001"){
echo "589";
}
}
if($price > "3000002"){
if($price < "10000001"){
echo "989";
}
}
if($price > "10000002"){
if($price < "100000001"){
echo "1389";
}
}
?>
couldn't you just connect to the database on there and save the value you want to compare into $price instead of doing it on another page and then sending it by form?
Am I understanding what your saying correctly?
If not please try to explain...
PatrikIden
11-24-2011, 06:07 AM
I'v got the DB values (price) in one page and on that page i also have a answer this ad link, so when clicking that link some values (inc, price) have to go throw to the page whare the answering form is.
keyboard
11-24-2011, 06:53 AM
Okay, makes more sense to me now. If you want the information sent to be secure, you can't use GET method.
You could use something like this
<script language="javascript">
function bob()
{
document.forms["myform"].submit();
}
</script>
<form action="" method="post" id="myform" name="myform">
<input type="hidden" name="" value=" <?php echo $variable; ?> ">
<input type="hidden" name="" value=" <?php echo $variable; ?> ">
?> ">
</form>
but it still wouldn't be secure because anyone can browse your pages source code. The most secure way (correct me if I'm wrong!) I can think to do this (easily) is using sessions (http://www.tizag.com/phpT/phpsessions.php).
PatrikIden
11-24-2011, 05:57 PM
Okay, makes more sense to me now. If you want the information sent to be secure, you can't use GET method.
You could use something like this
<script language="javascript">
function bob()
{
document.forms["myform"].submit();
}
</script>
<form action="" method="post" id="myform" name="myform">
<input type="hidden" name="" value=" <?php echo $variable; ?> ">
<input type="hidden" name="" value=" <?php echo $variable; ?> ">
?> ">
</form>
but it still wouldn't be secure because anyone can browse your pages source code. The most secure way (correct me if I'm wrong!) I can think to do this (easily) is using sessions (http://www.tizag.com/phpT/phpsessions.php).
Yes maybe, i dont know how to use that thow.
keyboard
11-24-2011, 07:26 PM
<?php
session_start();
$variable = "hello";
$_SESSION["price"] = $variable;
?>
$variable is the value you want to pass over.
on your second page
<?php
session_start();
echo $_SESSION["price"];
?>
PatrikIden
11-24-2011, 08:51 PM
<?php
session_start();
$variable = "hello";
$_SESSION["price"] = $variable;
?>
$variable is the value you want to pass over.
on your second page
<?php
session_start();
echo $_SESSION["price"];
?>
Thank you i'l try this, and if i whant to pass over more than one variable ex, ID, Name?
and i still need to have this in a link to the answering form page?
keyboard
11-24-2011, 11:13 PM
This bit goes on the page which you are accessing the database
<?php
session_start();
$variable = "hello";
$_SESSION["price"] = $variable;
?>
If you want to have more than one piece of information, you do this
<?php
session_start();
$variable = "hello";
$variable2 = "hello2";
$_SESSION["price"] = $variable;
$_SESSION["price2"] = $variable2;
?>
This should go at the top of the page.
session_start();
This has to go after you retrieve the value from the database that your trying to use. $variable (or what ever you want to call it) has to be the value you are trying to transfer over to another page
$variable = "hello";
$variable2 = "hello2";
$_SESSION["price"] = $variable;
$_SESSION["price2"] = $variable2;
Note:You can call the variables anything you want.
PatrikIden
11-25-2011, 12:19 PM
This bit goes on the page which you are accessing the database
<?php
session_start();
$variable = "hello";
$_SESSION["price"] = $variable;
?>
If you want to have more than one piece of information, you do this
<?php
session_start();
$variable = "hello";
$variable2 = "hello2";
$_SESSION["price"] = $variable;
$_SESSION["price2"] = $variable2;
?>
This should go at the top of the page.
session_start();
This has to go after you retrieve the value from the database that your trying to use. $variable (or what ever you want to call it) has to be the value you are trying to transfer over to another page
$variable = "hello";
$variable2 = "hello2";
$_SESSION["price"] = $variable;
$_SESSION["price2"] = $variable2;
Note:You can call the variables anything you want.
OK i'm not realy getting this, how can this get the price from DB what ever the price is? (i mean it's the user that puts in the price when posting an add).
Can i put somthing like this in $Variable <?php echo mysql_result($result,$i,"price"); ?>, i use this echo in my code now.
djr33
11-25-2011, 08:24 PM
Instead of using echo, just store that value to one of the session variables:
echo 'something;
$var = 'something';
However, honestly using sessions will get complicated if a user happens to be browsing two things at one time because you'll need to track everything and not get the variables confused. Sessions are available on all pages and there is only one session per site.
PatrikIden
11-25-2011, 08:53 PM
Hi, keyboard1333 did you send me an email? i recived a email dhat have some add in it. So i just have to ask if it is you that sent me taht mail just so i dont send the code to someone else.
If it you thaen Thank you for getting in tuch.
Regars
/Patrik.
keyboard
11-25-2011, 09:32 PM
Yep, I sent you that email...
Sorry about the ad, it's one of incredimails dumb 'features'.
I really should get a proper email program...
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.