Is there a way to extract a php array value from an index retrieved from a mysql field?
I mean, I have these 2 tables:
vechicle_types table:
vt_id | vt_type
1 | car
2 | van
3 | truck
4 | bike
vechicles table:
vehicle_brand | vechicle_model | vehicle_type
brand 1 | model 1 | 1
brand 1 | model 2 | 3
brand 1 | model 3 | 2
brand 2 | model 1 | 4
brand 2 | model 2 | 1
brand 2 | model 3 | 4
Then, I have a taxes array ($arr_taxes
) dynamically generated upon some calculations through some functions, so it cannot be stored in the database table, as the values are calculated inline
Now I want to create a SQL variable (vehicle_tax
) with the value extracted from $arr_taxes
, based on the type of vehicle (vehicle_type
) stored in the database
This is my code, which off course its not working:
$arr_taxes=(1=>10, 2=>15, 3=>20, 4=>12); # this array is dinamically generated previously from some calculations
$var_query="SELECT vehicle_brand, vechicle_model, vehicle_type, {$arr_taxes[vechicle_type]} as vehicle_tax FROM vehicles";
Is there a way to accomplish what I need?
Thank you in advance
UPDATED INFO:
Actually, the table is more like this
vehicle_brand | vechicle_model | vehicle_type | vehicle_price
brand 1 | model 1 | 1 | 300
brand 1 | model 2 | 3 | 150
brand 1 | model 3 | 2 | 200
brand 2 | model 1 | 4 | 100
brand 2 | model 2 | 1 | 300
brand 2 | model 3 | 4 | 600
Then, the query should look like this:
$var_query="SELECT vehicle_brand, vechicle_model, vehicle_type, vehicle_price, {$arr_taxes[vechicle_type]} as vehicle_tax, (vehicle_price+(vehicle_price*({$arr_taxes[vechicle_type]}/100)) as vehicle_finalprice FROM vehicles ORDER BY vehicle_finalprice";