Include data in Json beyond what comes via Mysql

0

I'm building an API in PHP / Mysql that will feed a hybrid APP, also under construction, via Ionic. The data traffic is via Json.

I'm a newcomer to Ionic, Angular, JS, and Json ... Reasonable in PHP and Mysql. I mean ... heeeeelllllllp please !!!

In practice I make a select:

$sql = "select id, titulo, ano from tabela WHERE titulo!='' ORDER by id DESC LIMIT 10";

$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

I create an array

$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}

Encodo

echo json_encode($emparray), "\n";

Close the connection

mysqli_close($connection);

It works !!! Each item is encoded correctly and Ionic interprets ok (relatively ok ... html formatting, line break etc are not interpreted in the app, but it's a question for another question.)

What do I need then?

I want to include data beyond what's 'select', encode and send it to Ionic.

Eg: Placing the variable

$site=www.com.br;
$autor=Pedro;

And they, even if they do not come from Mysql, are added in Json.

    
asked by anonymous 14.09.2016 / 00:20

1 answer

1

Basically you can do this:

$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}

$emparray['site'] = 'www.com.br';
$emparray['autor'] = 'Pedro';

echo json_encode($emparray), "\n";

Remember that before turning a json it is simply a PHP Array, you can manipulate it the way you please and then encode as JSON that it will be transposed normally.

    
14.09.2016 / 00:39