Reputation: 2651
I have a script that will insert a value in a cell, in my database. That cell will contain 4 values total. It should look like this: 0;0;0;0 (Each '0', represents a value)
How can I insert example value 100, at the place where '0' number 3 is, so it will look like this:
0;0;100;0
Thanks in advance.
Upvotes: 0
Views: 1891
Reputation: 62392
This is bad database design and breaks the first normal form of database design.
I would recommend re-thinking your schema and data architecture.
Maybe break them out into individual columns.
Your data should be designed relationally to minimize repeating patterns in your columns (see link above)
I can almost guarantee you that there is a better way...
Upvotes: 2
Reputation: 58521
<?php
// original string of values seperated by colon
$string = "0;0;0;0";
// create array of values by splitting at colons
$vals = preg_split('/;/', $string);
// modify the value of any elements in your array
$vals[2] = 100;
// glue your array of values together with semicolons at the joins
$string = implode(';',$vals);
// check the value has been changed coreectly
echo $string;
?>
Upvotes: 0
Reputation: 10880
look into serialize()
and unserialize()
$array = array(0,20,103,330);
$string = serialize($array); // store this in db
then get the string from db:
$array = unserialize($string);
access/update values with the array and re-store in db
Or if you are stuck with the format:
$string = '0;0;100;0'; // coming from db
$array = explode(';' , $string);
$array[2] = 100;
$string = implode(';' , $array);
Upvotes: 1