0

This is my code, it retreives data from xampp external database and store it to an array that is parsed to json

<?php


/*
* Following code will list all the products
*/

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/conectaDB.php';

// connecting to db
$db = new DB_CONNECT();

// get all products from products table
$result = mysql_query("select cd.id, cd.dt_ini_camp, cd.dt_fim_camp, cd.descricao, + (select sum(quantidade) from lin_doc where id_cab_doc = cd.id) as qtd from cab_doc cd where cd.id_tipo_doc = 1") or die(mysql_error());


 // check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node



while ($row = mysql_fetch_array($result)) {

    // temp user array
    $product = array();
    $product["id"] = $row["id"];
    $product["dt_ini_camp"] = $row["dt_ini_camp"];
    $product["dt_fim_camp"] = $row["dt_fim_camp"];
    $product["descricao"] = $row["descricao"];
    $product["qtd"] = $row["qtd"];      



    // push single product into final response array
    $response["cab_doc"] = array();
    array_push($response["cab_doc"], $product);
}




// success
$response["success"] = 1;


// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";

// echo no users JSON
echo json_encode($response);
}
?>

Output:

{"cab_doc":[{"id":"69","dt_ini_camp":"2012-12-22","dt_fim_camp":"2012-12-  26","descricao":"cadqadadad","qtd":null}],"success":1}

My problem is that this is the last record and i want all, whats missing please help

6
  • 1
    Could possibly be because you keep emptying $response["cab_doc"] = array(); in your loop? Commented Apr 3, 2014 at 2:59
  • do i? lol thats not my intention what should i change Commented Apr 3, 2014 at 3:02
  • Comment it out of your while() loop and tell us the results. Commented Apr 3, 2014 at 3:03
  • sorry ive commented it and now it shows this Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\webprojecto4\index_pesagem.php on line 42 Commented Apr 3, 2014 at 3:13
  • and if i place it outside the loop it shows me a blanck page Commented Apr 3, 2014 at 3:16

1 Answer 1

1

Try something like this for your while() loop:

while ($row = mysql_fetch_array($result)) {

    // temp user array
    $product = array(
        'id' => $row["id"],
        'dt_ini_camp' => $row["dt_ini_camp"],
        'dt_fim_camp' => $row["dt_fim_camp"],
        'descricao' => $row["descricao"],
        'qtd' => $row["qtd"]
    );

    // push single product into final response array

    array_push($response["cab_doc"], $product);
}

Optionally you could try adding it to the $response['cab_doc'] array like this:

$response['cab_doc'][] = $product;

What does that return for you?

EDIT: try this:

$response["cab_doc"] = array();
while ($row = mysql_fetch_array($result)) {

    // temp user array
    $product = array(
        'id' => $row["id"],
        'dt_ini_camp' => $row["dt_ini_camp"],
        'dt_fim_camp' => $row["dt_fim_camp"],
        'descricao' => $row["descricao"],
        'qtd' => $row["qtd"]
    );

    // push single product into final response array

    array_push($response["cab_doc"], $product);
}

EDIT #2:

<?php


/*
* Following code will list all the products
*/

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/conectaDB.php';

// connecting to db
$db = new DB_CONNECT();

// get all products from products table
$result = mysql_query("select cd.id, cd.dt_ini_camp, cd.dt_fim_camp, cd.descricao, + (select sum(quantidade) from lin_doc where id_cab_doc = cd.id) as qtd from cab_doc cd where cd.id_tipo_doc = 1") or die(mysql_error());


 // check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node



$response["cab_doc"] = array();
while ($row = mysql_fetch_array($result)) {

// temp user array
$product = array(
    'id' => $row["id"],
    'dt_ini_camp' => $row["dt_ini_camp"],
    'dt_fim_camp' => $row["dt_fim_camp"],
    'descricao' => $row["descricao"],
    'qtd' => $row["qtd"]
);

// push single product into final response array

array_push($response["cab_doc"], $product);
//or
// $response['cab_doc'][] = $product;
}




// success
$response["success"] = 1;


// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";

// echo no users JSON
echo json_encode($response);
}
?>
Sign up to request clarification or add additional context in comments.

13 Comments

so with this line commented $response["cab_doc"] = array(); and your first resolution it shows an warning array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\webprojecto4\index_pesagem.php on line 43 thats this line here:array_push($response["cab_doc"], $product);
using $response['cab_doc'][] = $product; it show a blank page
@user3121133 see recent edit and try again, remember to var_dump($response) / print_r($response)
blanck page without var_dump($response). with var dump it shows me the data but then it cant be encoded to json
@user3121133 You have to encode it to json yourself using json_encode() as you did previously in your script..
|

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.