如何分离单个字符串值?
这是数据库中的字符串:Small:4,Medium:2,Large:5,X-Large:4
我想分别回显尺寸和数量。例如,我有2列表大小和数量。
<table>
<thead>
<th>Sizes</th>
<th>Quantity</th>
</thead>
<tbody>
<tr>
<td><?=“echo sizes here";?></td>
<td><?=“echo quantity here";?></td>
<tr>
</tbody>
那么我如何从数据库中的单个字符串单独回声大小和数量??
这是数据库中的字符串:Small:4,Medium:2,Large:5,X-Large:4
好吧,这不是你有效使用数据库的方式。
您正在使用数据库,那么请使用电源&方便以行和列的形式存储数据。
用(product_ID,size_ID,quantity)
和(size_ID, size)
属性做2个表,用size_ID
属性做映射。
如果您仍然希望对数据库中的给定字符串执行此操作,您可以使用以下命令:
藻类printXYZ:
Scan the string and keep appending characters to an array.
if char = ':', print the characters scanned till now and empty/deallocate the array.
Then print the next character and stop at char=','.
现在你看到它是多么的低效和丑陋了吧?
我想分别回显尺寸和数量。
你必须将它们分开存储。
通过添加另一个包含所有大小类型的表和一个包含product id
、size id
和quantity的交叉表来更改数据库结构。
然后从该表中选择属于特定产品的所有行