0

My final question.

I have values which have been serialized into a table forum_polls as are in the following format as shown in the two examples below:

a:2:{i:0;a:2:{s:6:"answer";s:3:"Yes";s:5:"votes";s:1:"1";}i:1;a:2:{s:6:"answer";s:2:"NO";s:5:"votes";s:1:"2";}}
a:5:{i:0;a:2:{s:6:"answer";s:2:"CA";s:5:"votes";s:1:"1";}i:1;a:2:{s:6:"answer";s:4:"CIMA";s:5:"votes";s:1:"1";}i:2;a:2:{s:6:"answer";s:4:"ACCA";s:5:"votes";s:1:"0";}i:3;a:2:{s:6:"answer";s:5:"CIPFA";s:5:"votes";s:1:"1";}i:4;a:2:{s:6:"answer";s:24:"MBA(specify in comments)";s:5:"votes";s:1:"1";}}

which unsererilized (I cheated and used an online unserializer) give:

array (
  0 => 
  array (
    'answer' => 'Yes',
    'votes' => '1',
  ),
  1 => 
  array (
    'answer' => 'NO',
    'votes' => '2',
  ),
)

and

array (
  0 => 
  array (
    'answer' => 'CA',
    'votes' => '1',
  ),
  1 => 
  array (
    'answer' => 'CIMA',
    'votes' => '1',
  ),
  2 => 
  array (
    'answer' => 'ACCA',
    'votes' => '0',
  ),
  3 => 
  array (
    'answer' => 'CIPFA',
    'votes' => '1',
  ),
  4 => 
  array (
    'answer' => 'MBA(specify in comments)',
    'votes' => '1',
  ),
)

Basically there are three pieces of information for each possible answer:
id e.g. 0,1,2,3,4 etc
answer e.g. MBA(specify in comments)
votes - i.e. the number of votes received

Of course there are polls with different number of answers etc.

What I need to do is take:

topic_id from forum_polls and

unserialize the values which are contained in table forum_polls column poll_answers

and then insert the following into table forum_poll_options

topic_id into topic_id
id into option_id
answer into option_text
votes into option_total

Can anyone help please as I haven't experience of dealing with arrays.

Thanks
Mark

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by magicmarkuk
0

I assume that you are working with php.
To feed your values into a database, use something like the following model and feed the output to mysql:

<?php
$x = 'a:5:{i:0;a:2:{s:6:"answer";s:2:"CA";s:5:"votes";s:1:"1";}i:1;a:2:{s:6:"answer";s:4:"CIMA";s:5:"votes";s:1:"1";}i:2;a:2:{s:6:"answer";s:4:"ACCA";s:5:"votes";s:1:"0";}i:3;a:2:{s:6:"answer";s:5:"CIPFA";s:5:"votes";s:1:"1";}i:4;a:2:{s:6:"answer";s:24:"MBA(specify in comments)";s:5:"votes";s:1:"1";}}';

$a = unserialize($x);
foreach( $a as $id => $entry )
	echo "insert into mytable (id, answer, votes) values ($id,'$entry[answer]', $entry[votes]);\n";
0

Thank you.

I included within a routine and it worled perfectly.

Regards
Mark

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.