Jump to content

get field by column name?


Nothadoth

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267562-get-field-by-column-name/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.