Junior Member
joined:Mar 19, 2009
posts: 165
votes: 0
Your situation is unusual. Given the use of the database, I'm surprised your polling isn't setup in a more friendly manner.
So, assuming an example string is in this form:
a:1:{i:1;a:3:{s:8:"question";s:27:"Will you switch over to VX?";s:6:"choice";a:3:i:1;s:3:"Yes";i:2;s:2:"No"; i:3;s:17:"I'll use both";}s:5:"votes";a:3:{i:1;i:5;i:2;i:7;i:3;i:12;}}}
What makes this "difficult" is that your results are stored in a proprietary, unusual string format. I suppose someone who is really good with regular expressions could tackle this, but that is one of my weak points, lol. So here is how I would go about doing it.... I would create a few functions to help me "process" this.
(suppose that a record is stored as variable "record"). I found this so odd/unusual I actually spent the time to write something up to process it (lol). Below is the code I wrote for a function poll_arrayize(). Its input is a string in the format noted above. Its output is an associative array.
*******************************************************
function poll_arrayize($str)
{
$output = array();
$i = 0;
$mode = 'start';
$parts = explode(';',$str);
if (count($parts)>0)
{
//Run thru "parts"
foreach ($parts as $part)
{
//Process Question/Choices
if (strpos($part,'"')===false) { /*do-nothing;skip-part*/ }
else
{
$quoted = explode('"',$part);
$thisString = $quoted[1];
if ($mode=='start') { if ($thisString=='question') { $mode = 'question'; } }
else if ($mode=='question')
{
if ($thisString=='choice') { $mode = 'choice'; }
else { $output['question'] = $thisString; }
}
else if ($mode=='choice')
{
if ($thisString=='votes') { $mode = 'votes'; }
else { $output['choices'][] = $thisString; }
}
}
//Break Off Loop (if voting)
if ($mode=='votes') { break; }
$i ++;
}
//Run Processing for Votes
if ($mode=='votes')
{
//Setup Votes Info
$offset = ($i+1);
$length = ((count($output['choices']))*2);
$votes = array_slice($parts,$offset,$length);
//Get Vote Counts
$voteCounts = array();
$vc = 0;
if (count($votes)>0)
{
foreach ($votes as $vc=>$voteInfo)
{
if ($vc%2)
{
list($junk,$voteNum) = explode(':',$voteInfo);
$voteCounts[] = $voteNum;
}
$vc++;
}
$output['votes'] = $voteCounts;
}
}
}
//Function Output
return $output;
}
*******************************************************
Note: This forum is messing with the code above I think, so I told it to disable the forum codes. Let me know if it works out. The above function looks uglier without the tabs. This is a "custom" solution. It is tested and verified to work (with your example string/record), but no guarantees are given. Could it be written better? Perhaps... but it will take your 'custom string' and turn it into a friendly PHP array.
If you pass your example "record" into this function, it will output an array in the form (straight from PHP given your record):
Array
(
[question] => Will you switch over to VX?
[choices] => Array
(
[0] => Yes
[1] => No
[2] => I'll use both
)
[votes] => Array
(
[0] => 5
[1] => 7
[2] => 12
)
)
So... given that you query your DB for the records, you can now quickly "process" them for what you want by calling this function on the string. From this resulting array, you can quickly get: (a) the question; (b) the choices; and (c) the votes. The array index for 'choices' and 'votes' should match.
IE: If you know you have choice #2 (value='No' and index='1'). You can get the number of votes by accessing the array as: $array['votes'][1]
Best of luck!
[1][[b]edited by[/b]: CyBerAliEn at 9:46 pm (utc) on Feb 8, 2010][/1]