我正在创建一个包含网站上销售的产品的页面.我想在每个产品附近添加一个"添加到购物车"按钮,其中列出的标记类似于:
<h4 class="productHeading">Product Name 1</h4>
<div>
Extra information on the product 1.
</div>
<h4 class="productHeading">Product Name 2</h4>
<div>
Extra information on the product 2.
</div>
...
Run Code Online (Sandbox Code Playgroud)
由于提交输入将具有不同的名称(包括产品的代码),最大的问题是:我应该将整个产品列表包装在一个表单中,还是应该为每个产品创建一个表单?在代码中:
<form method="post" action="process.php">
<h4 class="productHeading">Product Name 1</h4>
<div>
Extra information on the product 1.
<input type="submit" name="submit1" value="Add to Cart">
</div>
<h4 class="productHeading">Product Name 2</h4>
<div>
Extra information on the product 2.
<input type="submit" name="submit2" value="Add to Cart">
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
要么…
<h4 class="productHeading">Product Name 1</h4>
<div>
Extra information on the product 1.
<form …Run Code Online (Sandbox Code Playgroud) 获取NSView的CGContext -drawRect:并使用它以后执行更多绘图是否安全?在像这样的简单测试中:
CGContextRef context = NULL;
- (void)drawRect:(NSRect)r
{
if (!context)
context = [[NSGraphicsContext currentContext] graphicsPort];
}
- (void)drawSomething
{
CGContextSetRGBFillColor(context, 1, 0, 0, 1);
CGContextFillRect(context, CGRectMake (0, 0, 100, 100));
CGContextFlush(context);
}
Run Code Online (Sandbox Code Playgroud)
-drawSomething调用时似乎一切正常,但它是否保证上下文不会改变?
你可以看到并猜测,我试图绕过标准的绘图方式-drawRect:.它适用于无数场合,但是在我的特定情况下,更加程序化的绘图方式会让生活变得更轻松.
什么是实现数组减法的最快方法?例如:
array a1 = [1, 3, 4, 5, 8];
array a2 = [2, 4, 5];
array a3 = a1 - a2; /* [1, 3, 8] */
Run Code Online (Sandbox Code Playgroud)
这array是我的程序用来表示用作容器的结构的类型.其余部分是伪代码,当然我不会创建像这样的数组也不会减去.
我能想到的最简单的解决方案涉及嵌套循环:
/* a1 - a2 */
for (i = 0; i < a1.size; ++i) {
int is_the_same = 0;
for (j = 0; i < a2.size; ++j)
if (a1[i] == a2[j]) {
is_the_same = 1;
break;
}
}
if (!is_the_same)
a3.push a1[i];
}
Run Code Online (Sandbox Code Playgroud)
但这看起来效率不高.另一种方法是什么?