0

How to separate single string values?

This is the string in the database: Small:4,Medium:2,Large:5,X-Large:4

I want to echo the sizes and quantity separately. For example I have 2 column table Sizes and Quantity.

<table>
<thead>
    <th>Sizes</th>
    <th>Quantity</th>
</thead>
<tbody>
    <tr>
        <td><?=“echo sizes here";?></td>
        <td><?=“echo quantity here";?></td>
    <tr>
</tbody>

So how can I echo sizes and quantity separately from a single string in the database??

2
  • Are you wanting to convert a string to array? Commented Sep 26, 2015 at 22:45
  • In php you have explode and substr. Use them. Commented Sep 26, 2015 at 22:54

2 Answers 2

1

This is the string in the database: Small:4,Medium:2,Large:5,X-Large:4

Well, this is not how you use the database efficiently.
You are using a database, then please use the power & convenience of storing data in rows and columns.
Make 2 tables with attributes (product_ID,size_ID,quantity) and (size_ID, size) and do mapping using size_ID attribute.

If you still wish to do it with the given string in your database, you can use something like:

ALGO 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=','.

Now you see how inefficient and ugly it is?

Sign up to request clarification or add additional context in comments.

Comments

0

I want to echo the sizes and quantity separately.

You have to store them separately in the first place.

Change your database structure by adding another table with all the size types and one cross table with product id, size id and quantity.

And then just select all the rows from this table that belong to particular product.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.