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
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);
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.