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

Recommended Answers

All 2 Replies

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";

Thank you.

I included within a routine and it worled perfectly.

Regards
Mark

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.