2011-04-26 64 views
1

HELLO, 我有一个html选择列表,我想要:选定的数字是一个给定的值$content->number,最大值是$content->product_type->stock_2如果它小于5,或5,如果它是大于5。html选择列表选择的项目和最大值可以选择

现在我有:

<select class="number" name="number"> 
    <? $max = $content->product_type->stock_2 > 5 ? 5 : $content->product_type->sale_stock; ?> 
     <option value="<?= $content->number ?>"><?= $content->number; ?> </option>      
    <? for ($i = 1; $i <= $max; $i++):?>       
     <option <?php if($content->product_type->stock_2 == $i) echo 'selected="selected"' ;?> value="<?= $i ?>"><?= $i; ?></option>    
    <? endfor; ?> 
</select> 

但它显示我两次选定值$content->number。我确定我在某个地方犯错。

有什么建议吗? 谢谢!

+0

达纳是你的问题与此'<? $ max = $ content-> product_type-> stock_2> 5? 5:$ content-> product_type-> sale_stock; “我似乎无法理解整个问题。如果这个'$ content-> product_type-> stock_2> 5'为true,并且您希望它成为这个'$ content-> product_type-> sale_stock',如果$ content-> product_type-> stock_2> 5'是错误的,对吧? – Bastardo 2011-04-26 13:10:03

+0

正是:)这就是我想要的 – dana 2011-04-26 13:23:51

+0

:)但在你的问题中,你说**的最大值是$ content-> product_type-> stock_2,如果它小于5 **我的意思是你需要写一个2在'<?的结尾$ max = $ content-> product_type-> stock_2> 5? 5:$ content-> product_type-> sale_stock; ?>'不是吗? – Bastardo 2011-04-26 14:04:12

回答

0

我不知道我是否正确理解你想做的事,但试试这个:

<select class="number" name="number"> 
    <? $max = $content->product_type->stock_2 > 5 ? 5 : $content->product_type->sale_stock; ?> 
     <option value="<?= $content->number ?>"><?= $content->number; ?> </option>  
    <? for ($i = 1; $i <= $max; $i++):?> 
     <? if ($i != $content->number): ?>       
      <option <?php if($content->product_type->stock_2 == $i) echo 'selected="selected"' ;?> value="<?= $i ?>"><?= $i; ?></option> 
     <? endif; ?>   
    <? endfor; ?> 
</select> 
+0

很酷,但它没有选择$ content-> number的值。 – dana 2011-05-02 08:26:58

+0

@dana:我更新了我的答案。尝试一下 – Tomas 2011-05-02 11:58:19