Jump to content


DB array

  • Please log in to reply
2 replies to this topic

#1 dannybrazil


    Advanced Member

  • Members
  • PipPipPip
  • 178 posts

Posted 16 January 2013 - 09:49 PM


I have a DB table which contains a col with information [code_of_product - quantity]:

12345 - 12 , 6789 - 8 , 54321 - 3.

Now as they are all in ONE CELL I wanted to know if there is a way to SEPARATE them according to the "," and "-"

Like this:

Product 12345 - Quantity 12
Product 6789 - Quantity 8

any help?

#2 Jessica


    This is not my name.

  • Staff Alumni
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 16 January 2013 - 09:51 PM

Google "data normalization". You need to store your data differently, as you've discovered the way you're doing it causes problems as soon as you want to actually use the data.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 Barand


    Sen . ( ile || sei )

  • Moderators
  • 16,257 posts
  • LocationCheshire, UK

Posted 16 January 2013 - 11:50 PM

I totally agree with Jessica about the normalisation. This is not the way you should be storing your data.

But you will need to know how to extract the data in order to create a properly normalised table, so

$col_data = "12345 - 12 , 6789 - 8 , 54321 - 3";

$items = explode(' , ', $col_data);

foreach ($items as $item) {
    list($product, $qty) = explode(' - ', $item);
    echo "Product $product - Quantity $qty<br>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users