How to make data return in php array

Asked

Viewed 64 times

0

I need to return the data in an array. I’m doing it, but the data doesn’t return:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("conexao.php");
$pdo = conectar();

header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");

/*$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$idCategoria = $request->idCategoria;*/

$id_categoria = $_GET['idCategoria'];
echo $id_categoria;

$buscarSubcategoria=$pdo->prepare("SELECT * FROM sub_cat_entrada WHERE id_categoria = :id_categoria");
$buscarSubcategoria->bindValue(':id_categoria', $id_categoria, PDO::PARAM_INT); 
$buscarSubcategoria->execute();

//$buscarSubcategoria->debugDumpParams();


$return = array();

while ($linha=$buscarSubcategoria->fetch(PDO::FETCH_ASSOC)) {
    $return[] = array(
    'id_subcategoria'   => $linha['id_subcategoria'],
    'subcategoria'  => utf8_encode($linha['subcategoria']),
);
}
echo json_encode($return);
  • Your query return a line or more?

  • More than one line.

  • That’s how it works? while ($linha=$buscarSubcategoria->fetch(PDO::FETCH_ASSOC)) {&#xA; $return[] = $linha; &#xA;}

  • No, only the 1 of the echo $id_categoria appeared;

  • If you give a print_r($return) ve the array correctly? this goes for a javascript?

  • Go pro angular, this result. Gave the print_r($Return) and appeared the data and array "tree" format. I’m running the code on Chrome’s POSTMAN to test.

  • Is the problem not on the javascript side?

  • I put my angular return code in the description of the post @rray

  • I found the problem. The data does not appear because it has accented words. ?

  • 1

    I got... The two drop-down working. .

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.