Type conversion from phparray to jsonarray and sending back in response

Shubham Sharma

i am creating webservice in which i had made a function which is providing me multidimensional array in php but to sending these data to android we have to convert it into response of json after tried lot now i got various problem

Now these function which is providing php array name is getabsendstudent :

public function getabsendstudents($lecture1, $lecture2) {

    function bool2str($bool) {
        if ($bool === false)
            return 'FALSE';
        else
            return 'TRUE';
    }

    function compareObjects(&$o1, &$o2) {
        echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n";
    }

    ///queries
    $ss  = "SELECT * FROM attendence WHERE lecturenumber = $lecture1";
    $ss2 = "SELECT * FROM attendence WHERE lecturenumber = $lecture2";   

    $result = mysql_query($ss);
    $result2 = mysql_query($ss2);

    // fetching number of rows
    $no_of_rows  = 0;
    $no_of_rows2 = 0;   
    $no_of_rows  = mysql_num_rows($result);
    $no_of_rows2 = mysql_num_rows($result2);

    ///// array objects
    $testforstudent  = array();
    $testforstudent2 = array();
    $absentstudent   = array();
    $presentstudent  = array();

    //// variable declaration
    $len=0;
    $result_array_for_lec1[] = array();
    $result_array_for_lec2[] = array(); 

    ///fetching whole students in arrays
    if ($no_of_rows > 0) {
        $acount = 0;
        $pcout = 0;

        while ($row = mysql_fetch_array($result)) {
            $testforstudent[] = $row;
            $acount = $acount + 1;
        }
    }

    if ($no_of_rows > 0) {
        $pcount = 0;
        while ($row2 = mysql_fetch_array($result2)) {
            $testforstudent2[] = $row2;
            $pcount = $pcount + 1;          
        }
    }

    /// here we are comparing in status of student
    // otherwise student had bunked classes          
    if ($no_of_rows > $no_of_rows2)
        $len = $no_of_rows2;
    else
        $len = $no_of_rows;

    global $p;
    global $a;
    for ($i = 0; $i < $len; $i++) {
        $bo = strtolower($testforstudent[$i]["status"]) == strtolower($testforstudent2[$i]["status"]);

        if ($bo) {
            ////present student     
        }
        else {
            $absentstudent[$a] = $testforstudent2[$i];
            $a++;
        }
    }

    echo json_encode($absentstudent);
    return $absentstudent;
}

Data which is generated by these function

{
    "": {
            "0": "17",
            "id": "17",
            "1": "\"12:30\"",
            "starttime": "\"12:30\"",
            "2": "9",
            "classid": "9",
            "3": "\"2:30\"",
            "endtime": "\"2:30\"",
            "4": "cs602",
            "lecturecode": "cs602",
            "5": "1102",
            "teacherid": "1102",
            "6": "0827cs131235",
            "enrollmentnumber": "0827cs131235",
            "7": "\"P\"",
            "status": "\"P\"",
            "8": "\"24/04/2016\"",
            "dateof": "\"24/04/2016\"",
            "9": "\"sunday\"",
            "dayof": "\"sunday\"",
            "10": "2",
            "lecturenumber": "2"
    },
    "1": {
            "0": "18",
            "id": "18",
            "1": "\"12:30\"",
            "starttime": "\"12:30\"",
            "2": "9",
            "classid": "9",
            "3": "\"2:30\"",
            "endtime": "\"2:30\"",
            "4": "cs602",
            "lecturecode": "cs602",
            "5": "1102",
            "teacherid": "1102",
            "6": "0827cs131236",
            "enrollmentnumber": "0827cs131236",
            "7": "\"P\"",
            "status": "\"P\"",
            "8": "\"24/04/2016\"",
            "dateof": "\"24/04/2016\"",
            "9": "\"sunday\"",
            "dayof": "\"sunday\"",
            "10": "2",
            "lecturenumber": "2"
    }
}

Now i had created one more function which is catching $absentarray with variable of $resultarray.and i need code in these function code is

$result_array = $db->getabsendstudents($lecture1, $lecture2);
if ($result_array) {
    $count = 0;

    // result array is catching absentstudent array
    $response["success"] = 1;
    $response["user"][$count]["classid"] = $result_array[$count]["classid"];

    $count = $count + 1;
}
else {
    // user failed to store
    $response["error"] = 1;
    $response["error_msg"] = "JSON Error occured in Registartion";
    echo json_encode($response);
}
Shubham Sharma

SOLVED: The problem had solved by just applying the logic of foreach loop and in if($result_array) selection .The code is

  if ($result_array) {
                $count=0;       
        foreach ($result_array as $value)
            {

                // user stored successfully
            $response["success"] = 1;
            $response["user"][$count]["classid"] = $value["classid"];
            $response["user"][$count]["enrollmentnumber"]= $value["enrollmentnumber"];
            $response["user"][$count]["teacherid"]=$value["teacherid"];


            $count=$count+1;

            }

    echo json_encode($response);

    } 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type conversion from phparray to jsonarray and sending back in response

From Dev

Sending custom response back from varnish through VCL

From Dev

vector::push_back conversion from const Type* to type*

From Dev

How do you wait for a delivery response from Mandrill before sending response back to client?

From Dev

Ajax POST sending back empty response

From Dev

JQuery Ajax call not sending a response back

From Dev

NodeJS Error when sending response back

From Dev

Decrypting Data From Database Before Sending Response Back Node.js

From Dev

Sending data back from php

From Dev

Return Type for JsonArray from Action to JSP having multiple JSONArray Objects?

From Dev

Return Type for JsonArray from Action to JSP having multiple JSONArray Objects?

From Dev

Sending a response back when a chain of actors are involved in Spray

From Dev

Sending xml request to url and receiving xml response back

From Dev

Is it possible to execute a method after sending a response back to the browser in Rails?

From Dev

Sending a response back when a chain of actors are involved in Spray

From Dev

Sending xml request to url and receiving xml response back

From Dev

at response of type org.json.JSONObject cannot be converted to JSONArray

From Dev

Sending data from a browser to a server and back

From Dev

Sending a list from JSP back to servlet

From Dev

Sending data back from cloud code

From Dev

Sending Information Back From Servlet to JSP

From Dev

Sending a list from JSP back to servlet

From Dev

Sending an array from jquery to PHP and back

From Dev

Sending back to sender, from supervisor, in case of failure

From Dev

node.js sending a response from a callback

From Dev

Sending Response from Spring WS Interceptor

From Dev

Sending a response out from a Camel route

From Dev

Prevent expressjs from sending text in bare response

From Dev

How to retrieve response from multiple url using Jsonarray in android

Related Related

  1. 1

    Type conversion from phparray to jsonarray and sending back in response

  2. 2

    Sending custom response back from varnish through VCL

  3. 3

    vector::push_back conversion from const Type* to type*

  4. 4

    How do you wait for a delivery response from Mandrill before sending response back to client?

  5. 5

    Ajax POST sending back empty response

  6. 6

    JQuery Ajax call not sending a response back

  7. 7

    NodeJS Error when sending response back

  8. 8

    Decrypting Data From Database Before Sending Response Back Node.js

  9. 9

    Sending data back from php

  10. 10

    Return Type for JsonArray from Action to JSP having multiple JSONArray Objects?

  11. 11

    Return Type for JsonArray from Action to JSP having multiple JSONArray Objects?

  12. 12

    Sending a response back when a chain of actors are involved in Spray

  13. 13

    Sending xml request to url and receiving xml response back

  14. 14

    Is it possible to execute a method after sending a response back to the browser in Rails?

  15. 15

    Sending a response back when a chain of actors are involved in Spray

  16. 16

    Sending xml request to url and receiving xml response back

  17. 17

    at response of type org.json.JSONObject cannot be converted to JSONArray

  18. 18

    Sending data from a browser to a server and back

  19. 19

    Sending a list from JSP back to servlet

  20. 20

    Sending data back from cloud code

  21. 21

    Sending Information Back From Servlet to JSP

  22. 22

    Sending a list from JSP back to servlet

  23. 23

    Sending an array from jquery to PHP and back

  24. 24

    Sending back to sender, from supervisor, in case of failure

  25. 25

    node.js sending a response from a callback

  26. 26

    Sending Response from Spring WS Interceptor

  27. 27

    Sending a response out from a Camel route

  28. 28

    Prevent expressjs from sending text in bare response

  29. 29

    How to retrieve response from multiple url using Jsonarray in android

HotTag

Archive