Question

So basically a return value of my function is a 2D array and I plan to display every single row and column; however, I am having trouble accessing them individually i.e. row[0][0] I've tried the stringify function from what I've seen in similar questions though have had no luck in making it work. This is the code:

ajax:

$.ajax({
    url: "viewDayDocuments",
    type: 'post',
    data: {currentDay: currentDay, currentMonth: currentMonth, currentYear: currentYear},
    success: function(result){
        $('.list').text('');
        $('.list').remove();
        incomingDoc = JSON.stringify(result);
        $(".listIncoming").html("<p class = 'list'>This is the: "+ incomingDoc['referenceNo'] + "</p>");
        $("#myform").show(500);
    }
 });

controller code:

$date = $data['year']."-".$data['month']."-".$data['day'];

        $this->load->model('search_form');
        $output['details'] = $this->search_form->searchDateRetrievedIncoming($date);

        echo json_encode($output);

model code returning a 2D array:

        $rows[] = array();
        $rows2[] = array();
        $rows3[] = array();

        $i = 0;
        $companyName = $this->db->query("SELECT id, name from company");
        foreach($companyName->result_array() as $row2){
            $rows2[$i]['id'] = $row2['id'];
            $rows2[$i]['name'] = $row2['name'];
            $i++;
        }
        //get all company names

        $i = 0;
        $staffName = $this->db->query("SELECT id, surname, firstName, middleInitial from employee");
        foreach($staffName->result_array() as $row3){
            $rows3[$i]['id'] = $row3['id'];
            $rows3[$i]['name'] = $row3['surname'].", ".$row3['firstName']." ".$row3['middleInitial'];
            $i++;
        }
        //get all employee names
        $i= 0;

        $output = $this->db->query("SELECT * from incoming WHERE dateReceived BETWEEN '$searchQuery' AND DATE(NOW())
                                    ORDER BY incomingId LIMIT 20");
        if ($output->num_rows() > 0) {
            foreach($output->result_array() as $row){
                $count = 0;
                $j = 0;
                $rows[$i]['incomingId'] = $row['incomingId'];
                $rows[$i]['referenceNo'] = $row['referenceNo'];
                $rows[$i]['documentTypeId'] = $row['documentTypeId'];
                $rows[$i]['documentDate'] = $row['documentDate'];
                $rows[$i]['dateReceived'] = $row['dateReceived'];
                $rows[$i]['sender'] = $row['sender'];

                while($count < sizeof($rows2)){
                    if($rows2[$j]['id'] != $row['companyId']){
                        $j++;
                    }else{
                        $rows[$i]['companyName'] = $rows2[$j]['name'];
                        break;
                    }
                    $count++;
                }
                $j= 0;
                $count = 0;
                while($count < sizeof($rows3)){
                    if($rows3[$j]['id'] != $row['responsibleStaffId']){
                        $j++;
                    }else{
                        $rows[$i]['responsibleStaffName'] = $rows3[$j]['name'];
                        break;
                    }
                    $count++;
                }

                $rows[$i]['subject'] = $row['subject'];
                $rows[$i]['actionDone'] = $row['actionDone'];

                $rows[$i]['track'] = $row['track'];
                $rows[$i]['completed'] = $row['completed'];
                $rows[$i]['remarks'] = $row['remarks'];
                $i++;
            }

            return $rows;
        }
        return false;

Working Answer:

incomingDoc = JSON.parse(result);
        for(var i = 0; i <  incomingDoc.details.length; i++){
            $(".listIncoming").html("<p class = 'list'>This is the: "+ incomingDoc.details[i].referenceNo + "</p>");
        }      
Was it helpful?

Solution

DO NOT stringify. PARSE. Based on the object you returned....

$.ajax({
url: "viewDayDocuments",
type: 'post',
data: {currentDay: currentDay, currentMonth: currentMonth, currentYear: currentYear},
success: function(result){
    $('.list').text('');
    $('.list').remove();
    incomingDoc = JSON.parse(result);

    incomingDoc.details[/*indexhere*/].referenceNo; //gives you a reference number.

   }
 });

I'll leave it up to you to loop through the indeces of incomingDoc.details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top