Output text in a drop-down when a quantity is zero

I was able to get to the point of joining two tables and then hiding options from the list with a quantity of zero by filtering the results to only items with a quantity >0. What I actually need to do is always show all options, but add “Out of stock” next to any option that has a zero quantity. If anyone can help with this, I would appreciate it.

[php]
$sql2=mysql_query(‘SELECT * FROM shop_products_attributes LEFT JOIN shop_products_with_attributes_stock ON (shop_products_attributes.products_attributes_id=shop_products_with_attributes_stock.stock_attributes) WHERE shop_products_attributes.options_id=’.$rs1->options_id.’ AND shop_products_attributes.products_id=’.$rs->products_id.’ AND quantity>0 ORDER BY products_attributes_id desc’);
while($rs2=mysql_fetch_object($sql2)){
$output_attrib .= ‘<option value="’.getname(“products_options_values_id”,“shop_products_options_values”,“products_options_values_id”,$rs2->options_values_id).’">’.getname(“products_options_values_name”,“shop_products_options_values”,“products_options_values_id”,$rs2->options_values_id).’’;
}
$output_attrib .= ‘’;

                } 

[/php]

Just use a PHP line to add it if qty=0…

Something like:

if($rs2[‘quantity’]==0) $output_attrib .= ’ (Out of stock!) ';

You would have to place it inside your $out_attrib code… Not sure, something like:

Instead of:
$output_attrib .= ‘<option value="’.getname(“products_options_values_id”,“shop_products_options_values”,“products_options_values_id”,$rs2->options_values_id).’">’.getname(“products_options_values_name”,“shop_products_options_values”,“products_options_values_id”,$rs2->options_values_id).’’;

This might work:
$output_attrib .= ‘<option value="’.getname(“products_options_values_id”,“shop_products_options_values”,“products_options_values_id”,$rs2->options_values_id).’">’.getname(“products_options_values_name”,“shop_products_options_values”,“products_options_values_id”,$rs2->options_values_id);
if($rs2[‘quantity’]==0) $output_attrib .= ’ (Out of stock!) ';
$output_attrib .= ‘’;

This would be three lines instead of one and would add the out-of-stock notice at the end of the option line just before the end tag. Hope that helps…

Thanks. I got an error with the string below. I tried a few ways to arrange it based on how other if statements are written in that file, but I either got all in stock, or all out of stock. :frowning:

I’ll keep trying different methods, but if you can think of anything else, I’d appreciate it.

Something with this string: [php]if($rs2[‘quantity’]==0) $output_attrib .= ’ (Out of stock!) ';[/php] causes:

Fatal error: Cannot use object of type stdClass as array in

Sorry, that was just a general layout, hence it was not inside of code tags…

Since you used MySQL objects instead of arrays, you would have to do something like
if($rs2->quantity==0)…etc…

It depends where the quantity count came from. RS or RS2??? If you can’t figure it out, post a little more code of this section… I do not know your database layout and do not know where you get the ‘quantity’ from…

Oh, also, I assume this would mean that you would NOT filter the query with quantity>0…

Thank You! That did it!

[php]if($rs2->quantity==0) $output_attrib .= ’ (Out of stock!) '[/php]

Congrats! Always a nice warm fuzzy feeling when fixing a bug… CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service