I have spent the time working with the posts from here (old time ones as I don't have the new shiny server) and a little trial and error .... although looking at this the hardest thing was the SQL aspect of things so it may work with the new versions with some tweaks... and I thought I would share it here with anyone that is interested.
What this does
It takes answers from a participant and then allocates them to segments that they qualify for on a "least filled" basis so if I am qualified to be in segments 3, 5 and 7 then this approach will look at the counts from the database and assign me to the segment that has the lowest number of respondents in there.
Step 1: Create a multiple choice question which has all the segments in there and populate it however you want to ... mine is a mix of data from sample along with answers they have given (an equation question populates this)
Step 2: Use a second equation question to concatenate the answers from the first, this allows me only to return the relevant codes from the database here is the equation script I used ...
{join ( if (Q1Set_1=="Y","1,",""),if (Q1Set_2=="Y","2,",""),if (Q1Set_3=="Y","3,",""),if (Q1Set_4=="Y","4,",""),if (Q1Set_5=="Y","5,",""),if (Q1Set_6=="Y","6,",""),if (Q1Set_7=="Y","7,",""),if (Q1Set_8=="Y","8,",""),if (Q1Set_9=="Y","9,",""),if (Q1Set_10=="Y","10,",""),if (Q1Set_11=="Y","11,",""),if (Q1Set_12=="Y","12,",""),if (Q1Set_13=="Y","13,",""),if (Q1Set_14=="Y","14,",""),if (Q1Set_15=="Y","15,",""))}
I had 15 possible segments ... note this adds in a final comma and some whitespace that is removed in the next step
Step 3 in a short text question I added the following script ... I used a short text question as this gave the ability to process the Ajax and also a place to hold the data that was returned from the call ... I am sure there is more elegant way of doing this .... but this works for me
Here is the script I added
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var apiFilePath = 'https://yoursurvey/balance.php';
var sid = 134625;
var gid = 1393;
var qid = 31145;
var ans= "{GroupList.shown}";
var answers = ans.substr(0,ans.length - 5); //IMPORTANT TO STRIP THE LAST COMMA AND WHITESPACE
$.ajax({
url: apiFilePath, /
async: true,
cache : false,
data: {
sid: sid,
gid: gid,
qid: qid,
answers: answers
},
success: function(results){
$('#answer134625X1393X31146').val(results);
},
error: function(){
alert('Could not connect!');
}
});
$('#answer134625X1393X31146').hide();
});
</script>
So this calls an external PHP file that accesses the data base and then returns the code with the least counts associated to it .. here is the PHP file (username and password changed) .. The way the SQL code is structured means that it returns all answers to the question even those with 0 entries so far .. there is no need in this method to add in dummy data as I was thinking about doing in an earlier post.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = 'localhost';
$dbname = 'surveys';
$username = 'xxxx';
$password='xxxx' ;
$iSurveyID = $_GET["sid"];
$iGroupID = $_GET["gid"];
$iQuestID = $_GET["qid"];
$iAnswers = $_GET["answers"];;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT answers.code, COUNT(survey_".$iSurveyID.".`".$iSurveyID."X".$iGroupID."X".$iQuestID."`) AS Total, answers.answer FROM survey_".$iSurveyID." RIGHT OUTER JOIN answers ON survey_".$iSurveyID.".`".$iSurveyID."X".$iGroupID."X".$iQuestID."` = answers.code WHERE answers.qid = ".$iQuestID." and answers.code IN ( ".$iAnswers." ) GROUP BY answers.code ORDER BY Total Limit 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["code"];
}
} else {
echo "0 results";
}
$conn->close();
?>
Then once this is back in the survey ... I have a final single choice question that is set by an equation question with the answer returned from the script and the logic can be used as normal ... also this allows me to set quotas against this to ensure that we hit targets.
{Q1=Q1Outcome.shown}
.......
So there it is ... not the best code I am sure and again there are probably steps that I have taken that I didn't need to, but it works so there is that and I have seen some questions on here asking for something similar so I thought I would share ....
All this is run on version 2.6.4 (lts or not lts dependent on your point of view)
The topic has been locked.