sm0ke Posted February 7, 2008 Share Posted February 7, 2008 Hi I have a problem with a calculator script i am creating. The script relies on user input, and as such I will not now which field in the table i need to pull in order to display the correct results. EG: User chooses a surface 600mm wide with a front only downturn, they have the choice to select either front only or both as well as 13 different widths. I then want to take the user input and select the correct database field. my script is constructed as below: $colour = $_POST['colour']; //colour selected $band = $_POST['band']; //price band (choice of 4) $down = $_POST['down']; //front or both $wide = $_POST['wide1']; //width selected (choice of 13) db_login(); $sql = "SELECT $down_$wide FROM band_$band"; $result = mysql_query($sql) or die ("Error in query: $query " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_object($result)) { $cost = $row->$down.'_'.$wide; echo $cost; } } using the above i usually only get the value of $wide echoed to the screen and not the value of the field $down_$wide. However if I manually insert the values as below it works correctly: $colour = $_POST['colour']; //colour selected $band = $_POST['band']; //price band (choice of 4) $down = $_POST['down']; //front or both $wide = $_POST['wide1']; //width selected (choice of 13) db_login(); $sql = "SELECT front_600 FROM band_$band"; $result = mysql_query($sql) or die ("Error in query: $query " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_object($result)) { $cost = $row->front_600; echo $cost; } } I've also attempted to use if statements so if ($down == 'front') then write the query afterwards using: $row->front_$wide. But still the script only sees the value 600 (width) and not the value held in the database. I figure I must be making a mistake in my syntax or in the structure of the database but cant seem to find an answer. Cheers... Please let me knopw if you need further info. Quote Link to comment Share on other sites More sharing options...
sm0ke Posted February 7, 2008 Author Share Posted February 7, 2008 Solved it. I changed this: { $cost = $row->$down.'_'.$wide; echo $cost; } to this: { $cost = $down.'_'.$wide; $cost1 = $row->$cost; echo $cost1; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.