Nothadoth Posted August 25, 2012 Share Posted August 25, 2012 Hi, I have set up my database so that there are 3 sizes (A5, A4, A3). There is a price and quantity for each - so the column names are: a5_price, a5_qty, a4_price, a4_qty, a3_price, a3_qty In a form the user submits the size they want and it returns $size and $qty to add it to the cart. I want to query the database and get the quantity field which contains $size (so a3_qty for example) and then retrieves the current stock. How can I do this? At the moment I tried $sql = "SELECT pd.pd_id, pr.a3_qty, pr.a4_qty, pr.a5_qty, pr.postcard_qty FROM tbl_product pd, tbl_print pr WHERE pd.pd_id = $productId AND pr.pd_id = $productId"; $result = dbQuery($sql); extract($row); if (strstr($a3_qty, $ct_pd_size)) { $currentStock = $a3_qty; } elseif (strstr('$a4_qty', '$ct_pd_size')) { $currentStock = $a4_qty; } elseif (strstr('$a5_qty', '$ct_pd_size')) { $currentStock = $a5_qty; } Obviously that doesnt work as it is searching the contents of the field. How can I make it search the field name and then return the contents? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/267562-get-field-by-column-name/ Share on other sites More sharing options...
Christian F. Posted August 25, 2012 Share Posted August 25, 2012 I'd move the sizes into their own table, and then use a many-to-many relation in order to retrieve the data you want. Will not only make solving the issue you have now a lot easier, but any change in sizes and/or prices will also be a breeze to maintain. Quote Link to comment https://forums.phpfreaks.com/topic/267562-get-field-by-column-name/#findComment-1372334 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.