fguru
fguru

Reputation: 71

multiple data split into array

I have data coming from mysql and the need to show in the drop down list. The code is below.

This data value will come from mysql. So I need to show all these values in the dropdown list. Looking for a solution.

    var data = "user data1, user data2, user data3, user data4/ user data6, user data7, user data8/ user data5, user data9";


function studentPopulate(){
    $.each(newTotalData, function(key, value) {
    $('#sList')
    .append($("<option></option>")
    .attr("value",key)
    .text(value));
});
}

var totalData = [];
var data_array = []; 
var arrayVal = [];
var newTotalData = new Array();
function studentInfo(){
$.getJSON("checkData.php", {section_id:section_id, uID: uID}, function(data) {
    $.each(data, function(i, user) {
    var data = user.groupContent;
    data_array = data.split('/').join(',');
    totalData.push(data_array);
    arrayVal = totalData;
    newTotalData = arrayVal[0].split(',');
    studentPopulate();
    });
});
}

PHP code is here:

<?php

include 'connection.php'; 
$uID = $_GET["uID"];
$cid = $_GET["section_id"];
mysqli_select_db($con, "DB");

$Query="SELECT * from table WHERE uid='".$uID."' and section_id='".$cid."'";
$result = mysqli_query($con, $Query);
$totalRecords = mysqli_num_rows($result);
//echo $totalRecords;
if($totalRecords) {
    while ($row = mysqli_fetch_array($result)) {
    $returnData[]=array(    //for Json data array
        'userName' => $row['fullName'],
        'groupContent' => $row['groupContent']

);
}
}


mysqli_close($con);
echo json_encode($returnData);

?>

Change of dropdown value. I need to find out the dropdown text inarray. Just updated my code. It seems like it can't find the value in array. Please help. It can find newTotalData but I need to find out the group(array) of that dropdown value.

$('#sList').change(function() 
    {
        var str = $(this).find(":selected").text();
        if($.inArray(str,data_array[0])>=0){alert(data_array[0])};
    });
}

Upvotes: 0

Views: 2819

Answers (3)

user2494495
user2494495

Reputation: 29

You should use regular expression to split data string to an array.

var data = "user data1, user data2, user data3, user data4/ user data6, user data7, user data8/ user data5, user data9";

newTotalData = data.split(/[,\/]/); //split string to array.

newTotalData is an array.

If you need a object, then you need transfer this array to a object.

Upvotes: 0

VeeTheSecond
VeeTheSecond

Reputation: 3176

It is not clear from your example which part of the user data comprises the list box value, and which part comprises the list box text. To illustrate what I think you might be after, I have created this fiddle for you:

HTML:

<select id='sList'></select>

Javascript:

function test() {
    var data = "100, Bob Smith/200, John Wayne";
    studentPopulate(data.split('/'));
}

function studentPopulate(data) {
    $.map(data, function (student) {
        var studentRecord = student.split(',');
        var id = studentRecord[0].trim();
        var name = studentRecord[1].trim();
        $('#sList')
        .append($("<option></option>")
                .val(id)
                .html(name));
    });
}

test();

In this example, I am assuming that the list box value is the first element in the list, and the text, the second element.

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

You need to do it like this -

var totalData = [];
function test(){
    var data = "user data1, user data2, user data3, user data4/ user data6, user data7, user data8/ user data5, user data9";
    data_array = data.split('/').join(',');
    totalData.push(data_array);
    arrayVal = totalData;
    newTotalData = arrayVal[0].split(',');
    studentPopulate();
}

function studentPopulate(){
    $.each(newTotalData, function(key, value) {
    $('#sList')
    .append($("<option></option>")
    .attr("value",key)
    .text(value));
});
}

test();

Demo ----> http://jsfiddle.net/ht3Y7/1/

Upvotes: 1

Related Questions