0

My String in php is like 0:12,1:123,2:234 .... n times

I want to store it in database as -->

0:12 --- 0 as room_number 12 as value
1:12 --- 1 as room_number 12 as value

databse structure is like --

id(primary) room_number value 

Thanks in advance

6
  • Have you come across PHP's explode() function? Commented Jan 24, 2013 at 10:43
  • @MarkBaker yes i explode it but not get desired result . Commented Jan 24, 2013 at 10:44
  • Can you post the code where you put it into the database? Commented Jan 24, 2013 at 10:46
  • First explode(',',$string) then explde each array key of resulting array like this: explode(':',$result) Commented Jan 24, 2013 at 10:46
  • 1
    @user2001057 - if you've tried something and it hasn't worked, you should show us what you've tried; you might be really close to getting the answer yourself, but we can't tell from the question. If we don't know how far you've got we have to give much more detailed answers, which means people are less likely to want to help at all. Commented Jan 24, 2013 at 10:47

2 Answers 2

1

Use PHP's explode() function to split by commas to get a string for each room, then explode() each one of the room strings by colon to get the individual values you're after. Now you've got the values, you should be able to use the normal DB handling functions to save the records.

Or to do it directly in the database without needing any PHP code (assuming you're using MySQL), you could use MySQL's LOAD DATA INFILE command, specifying a comma as the record terminator and colon as the field terminator.

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

Comments

0
$string = '0:12,1:123,2:234';

$elements = explode(',', $string);

foreach($elements as $value){

        $value = explode(':', $value);
        echo 'Room: '.$value[0].', Value: '.$value[1].'<br/>';

        // Write some code here to insert into your database.
}

1 Comment

@user2001057 Upvote and Accept if it's the correct answer :-)

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.