0

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])};
    });
}
5
  • your arrayVal have 3 different array in it , you can't split that Commented Jun 18, 2013 at 20:14
  • When I alert totalData, it shows me all one array. Can't we split totalData somehow. Commented Jun 18, 2013 at 20:17
  • What do you want to show in select list ? Commented Jun 18, 2013 at 20:20
  • All these var data values. Which is coming from database exactly in that way. Commented Jun 18, 2013 at 20:25
  • So, yo want only three item's in you select list ? Commented Jun 18, 2013 at 20:26

3 Answers 3

1

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/

Sign up to request clarification or add additional context in comments.

7 Comments

That's great. Works fine. The only problem is I am getting these values from database as json_encode and in jQuery my var data is like that. var data = user.groupContent; and your code is not working. Is there anything do I need to do like make a string or something.
Hi pXL, I just updated the code above. Last part is my original code.
Hi pXL, Thanks for your help. I just figured it out. Great solution. Thanks again.
Now I need to find out the dropdown text inarray. Just updated my code above. It seems like it can't see the value in array. Please help.
Nothing happened PXL. Actually let me tell you. I also need to split different arrays from var data after each '/'. And then I need to find out wich array has this dropdown value. They are groups. so if we find the dropdown value in any of three arrays (right now) that will be the group of this value.
|
0

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.

Comments

0

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.

2 Comments

How to split this for data into three different eras. Splitting after '/'.
[,\/] means split ',' or '/'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.