1

Rather then creating a db column for each value, I want to include them into one field, and then call those comma separated values into a PHP array to be called individually later.

Example; db value for 'data_set' is; 111, 222, 333

$sql_data = $get['data_set'];

$data_set = array ( $sql_data ); 

I want $data_set[0] to return 111, $data_set[1] to return 222, etc

Instead, it returns the entire db value, indicating that the import is not recognizing the comma separated values.

How can I fix this? Or is there a better approach?

3
  • 1
    Not a good idea at all! Normalize your DB at least to level 3 Commented Jan 30, 2015 at 21:23
  • 1
    You will not like this design later trust me. Split the data out into individual rows, or at least columns. But to answer the question explode(). Commented Jan 30, 2015 at 21:23
  • 1
    $data_set = explode(',', $sql_data ); :-) your question is not clear :-( Commented Jan 30, 2015 at 21:26

2 Answers 2

1

I highly suggest using a one to many or many to many design for this rather than cramming the values into a single field. Doing this will cause issues in the future and maintenance will be a nightmare. If you provide a bit more information about what kind of data you're working with (What do the numbers represent? What uses the numbers?) I'd be happy to illustrate a more usable design.

That said, if you insist on having multiple values in a single field, you can use the PHP function explode():

$data_set = explode(',', $sql_data);

Updated Answer Based On Comment

In that case, there are a few ways you could do this. If the properties are always in the same order and represent the same thing (and there are only 5-6), I'd recommend just breaking them out into separate columns and naming the columns appropriately.

If there are a lot of potential properties and they're variable, I'd recommend creating a profiles table, a properties table (just id and name), and a profilesToProperties table. That will allow the most flexible adjustments to properties over time. To get all properties for a given profile, you'd do something like:

SELECT prof.username, prop.name
FROM profiles prof
    LEFT JOIN profilesToProperties ptp ON prof.id = ptp.profile_id
    LEFT JOIN properties prop ON prop.id = ptp.property_id
WHERE prop.id = :id

This would still return profiles with zero properties.

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

2 Comments

Thank Jim. I have purchased a javascript for some image mapping/zoom functionality. The script resides on the main template page, which is used to create various profile pages dynamically. This script requires certainly numeric perimeters, which differ from one profile to the next. There are a set of many 5-6 numeric values. So I've been doing something like this; imageWidth:<?php echo $data_set[0]; ?>,
@noy-hadar - check out the updated answer. I'd recommend creating a many to many relationship if properties are variable.
0

What it can be used fore: Quiz webpage where every question have for example 4 different choice 1-4. The user goes to next question (program extracts numbers from field in db (4,4,2,4,1,3)) representing previous question answered, ads the number from present question to the string and update the field in db (4,4,2,4,1,3,1) With 50 questions it will otherwise be 50+ columns in the table for answers.

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.