anarchoi Posted April 1, 2008 Share Posted April 1, 2008 ok i'm a newbie, and i'm trying to make a small script that allow my members to add Custom Fields to the calendar (i use the calendar to announce gigs/concerts so the custom fields are used to display a list of concert places) The problem is that the custom fields looks like this inside of the database: a:5:{i:0;s:0:"1111111111111111";i:0;s:0:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";} Where the numbers are the different custom fields so after searching through vbulletin's calendar.php file and PHP.NET i found out that it looks like this because serialize(); was used is there a quick way to insert a new "custom field" into this serialized thing, while keeping all the other ones intact?? Could anyone please give me an example? thanks for your time Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/ Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 you can convert it back to an array using deserialize() Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/#findComment-506103 Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 You would have to unserialize it back to an array, add the fields, and re-serialize it: <?php $a = 'a:5:{i:0;s:16:"1111111111111111";i:1;s:11:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";}'; $ary = unserialize($a); $ary[] = '999999999999999'; $b = serialize($ary); echo $b; ?> Doing the above code would give: a:6:{i:0;s:16:"1111111111111111";i:1;s:11:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";i:6;s:15:"999999999999999";} Ken Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/#findComment-506105 Share on other sites More sharing options...
anarchoi Posted April 1, 2008 Author Share Posted April 1, 2008 your code works, but when i try to apply it to my query instead of $a it just displays a:1:{i:0;s:15:"999999999999999";} here is the code i tryed: $query = "SELECT options from quebec_calendarcustomfield WHERE calendarid='1'"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $a = $row["options"]; $ary = unserialize($a); $ary[] = '999999999999999'; $b = serialize($ary); echo $b; } i don't understand why it doesnt work, when i try to echo $a it shows the correct result : a:5:{i:0;s:0:"1111111111111111";i:0;s:0:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";} Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/#findComment-506120 Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 The string a:5:{i:0;s:0:"1111111111111111";i:0;s:0:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";} is not a valid serialized array. The format is: a:5 = 5 elements in the array i:x = the element x s:n = length of element x "xyz" = value of element x as you can see, the string you show isn't correct, so when you unserialize it you get a null array. The correct string in your case would be: a:5:{i:0;s:16:"1111111111111111";i:1;s:11:"22222222222";i:3;s:11:"33333333333";i:4;s:9:"444444444";i:5;s:4:"5555";} See the difference? Ken Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/#findComment-506124 Share on other sites More sharing options...
anarchoi Posted April 1, 2008 Author Share Posted April 1, 2008 oh, i'm dumb! that's because i messed around with the entry in the database while trying to figure what these weirds numbers were about! Corrected it, and now it works! thanks a lot Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/#findComment-506128 Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 Sorry for the deserialize(). Glad you figured it out Link to comment https://forums.phpfreaks.com/topic/98913-need-help-with-serialize/#findComment-506151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.