6
I have the following structure json
test:
{
"CE": {
"Fortaleza": [
{
"nome": "Teste",
"email": "[email protected]",
"cel": "(xx) xx-xx"
},
{
"nome": "Teste",
"email": "[email protected]",
"cel": "(xx) xx-xx"
}
],
"Abaiara": [
{
"nome": "Cidade Abaiara 1",
"email": "[email protected]",
"cel": "(xx) xx-xx"
},
{
"nome": "Cidade Abaiara 2",
"email": "[email protected]",
"cel": "(xx) xx-xx"
}
]
},
"rn": {
"pindaiba": [
{
"nome": "rtyretyerty",
"email": "[email protected]",
"cel": "(xx) xx-xx"
},
{
"nome": "zzzzzz",
"email": "[email protected]",
"cel": "(xx) xx-xx"
}
],
"sorisal": [
{
"nome": "sor",
"email": "[email protected]",
"cel": "(xx) xx-xx"
},
{
"nome": "sorsor",
"email": "[email protected]",
"cel": "(xx) xx-xx"
}
]
}
}
I created a database and I’m having doubts about how to create my table structures from the json
above, but no data is required.
[PERGUNTA]
The question is about programming, because I can’t write a codeSQL
set up this structure.Goal: Create a
echo json_encode($stm->fetchAll(PDO::FETCH_ASSOC));
building the same structurejson
above.
What I already have:
- Connection to the bank:
-
function Conectar(){
try{
$opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$con = new PDO("mysql:host=localhost; dbname=teste;", "root", "", $opcoes);
return $con;
} catch (Exception $e){
echo 'Erro: '.$e->getMessage();
return null;
}
}
- Search of all criminal records:
-
function getFilterCidade($estado){
$pdo = Conectar();
$sql = 'SELECT * FROM locais WHERE uf = ?';
$stm = $pdo->prepare($sql);
$stm->bindValue(1, $estado);
$stm->execute();
sleep(1);
echo json_encode($stm->fetchAll(PDO::FETCH_ASSOC));
$pdo = null;
}
- Name of json structure for **columns creation:**
Realize that a state poderá ter várias cidades
and a city poderá ter vários usuários
with: nome , email e celular.
In this example structure we have 2 states with 2 cities and each city with 2 users.
You need to mount a select(or more) that the return is equal to json? if it is that could put the table structures?
– rray
@rray, I need to know exactly what this table structure would look like from the json I presented and yes, the NCODE should be just as it is in the same example. I should add something you love?
– Lollipop
a question, why does it return in json? json will be read in php or javascript?
– Daniel Omine
Will be read via
$.getJSON('data.php', function(data) {...
– Lollipop
So it’s javascript.. the whole thing comes down to "Read a json with javascript and mount the data into an html table". PHP and Mysql are off topic.
– Daniel Omine
You don’t need to worry, in your answer, about how to request, but rather how to do this structure
mySQL
and return thejson
in a.php
, as asked in the question. Still confused? Don’t change the question if you don’t understand.– Lollipop
Qu not want "Read Mysql data returned in json with Javascript and mount an HTML table". I want code
SQL
and ajson_encode
that manages me the same structure I gave in the question. It has nothing to do with javascript the answer.– Lollipop
the point is that you will not create a mysql structure.. the structure already exists.. rsrsr .. what I understood and you had confirmed in the comments above is, "php reads the data in mysql, returns in json. So you want to build an html table with this json data, with javascript".. you even confirmed this above.. now I don’t understand anything..
– Daniel Omine
Now it’s not "molezinha".. rsrs... we’re talking about data modeling.. you don’t have the modeling? (table structure). This can’t be done in an informal post in 2 or 3 minutes.. requires a certain amount of time. At least the modeling of tables you should do, because it is your job. I can merely suggest a modeling. But there are N ways to do this.
– Daniel Omine
I posted an answer but I haven’t had time to test it yet, please see if it solves the problem, I believe to be close to this.
– Inkeliz
I’ve done all the tests and it’s exactly as you asked.
– Inkeliz