Jump to content


Photo

DB array


  • Please log in to reply
2 replies to this topic

#1 dannybrazil

dannybrazil

    Advanced Member

  • Members
  • PipPipPip
  • 178 posts

Posted 16 January 2013 - 04:49 PM

Hello

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

Example:
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
(<br>)
Product 6789 - Quantity 8
.
.
.
etc

any help?

#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 16 January 2013 - 04: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

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,223 posts
  • LocationCheshire, UK

Posted 16 January 2013 - 06: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>";
}

|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

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com