Include data in Json beyond what comes via Mysql

Asked

Viewed 161 times

0

I am building an API in PHP/Mysql that will feed a hybrid APP, also under construction, via Ionic. Data transit is via Json.

I guess I’m new to Ionic, Angular, JS, and Json... Reasonable in PHP and Mysql. I mean... heeeeelllllp 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";

I close the connection

mysqli_close($connection);

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

What I need then?

I want to include data beyond 'select', encoding and sending to Ionic.

Ex.: Place the variable

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

And they, even if they don’t come from Mysql, are added to Json.

1 answer

1

Basically you can do like 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 you turn into a json it is simply a PHP Array, you can manipulate it the way you want and then find as JSON it will be transposed normally.

  • Thank you for answering. It turns out that it didn’t work fully, I’m sorry, due to a detail not mentioned in the question, the following: A $emparray[] item is dynamic (but custom) coming from the comic book. I put inside while{} and even pulls it right but only once, it is not in a looping (in the case 10 items / limit 10).

Browser other questions tagged

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