Skip to content

Instantly share code, notes, and snippets.

@hmaesta
Created October 24, 2019 12:42
Show Gist options
  • Save hmaesta/8e5631262745829a80668a635a1f74f4 to your computer and use it in GitHub Desktop.
Save hmaesta/8e5631262745829a80668a635a1f74f4 to your computer and use it in GitHub Desktop.
<?php
function Carro_Consumo_Shortcode($params = array()) {
// default parameters
extract(
shortcode_atts(
array(
'marca' => '',
'modelo' => '',
'titulo_tag' => 'h2'
),
$params
)
);
$m = new MongoClient();
// select database "dados_carros_brasil"
$db = $m->mydb;
$collection = $db->dados_carros_brasil;
$query = array(
'marca' => $params['marca'],
'modelo' => $params['modelo']
);
$cursor = $collection->find($query);
$data = '';
$data.= '<' . $params['titulo_tag'] . '>\n';
$data.= 'Consumo ' . $params['modelo'] . ' ' . $params['modelo'] . '\n';
$data.= '</' . $params['titulo_tag'] . '>\n';
$data.= '<table>';
$data.= '<thead>';
$data.= '<tr>';
$data.= ' <th><span style="color:#999";>' . $document['ano'] . '</span></th>';
$data.= ' <th colspan="2">Consumo urbano</th>';
$data.= ' <th colspan="2">Consumo rodoviário</th>';
$data.= '</tr>';
$data.= '<tr>';
$data.= ' <th>Versão</th>';
$data.= ' <th>Etanol</th>';
$data.= ' <th>Gasolina</th>';
$data.= ' <th>Etanol</th>';
$data.= ' <th>Gasolina</th>';
$data.= '</tr>';
$data.= '</thead>';
$data.= '<tbody>';
foreach ($cursor as $document) {
$data.= '<tr>';
$data.= ' <td>' . $document['versao'] . '</td>';
$data.= ' <td>';
$data.= ' ' . $document['data']['combustivel']['0']['consumo']['cidade'];
$data.= ' ' . $document['unidades']['consumo'];
$data.= ' </td>';
$data.= ' <td>';
$data.= ' ' . $document['data']['combustivel']['1']['consumo']['cidade'];
$data.= ' ' . $document['unidades']['consumo'];
$data.= ' </td>';
$data.= ' <td>';
$data.= ' ' . $document['data']['combustivel']['0']['consumo']['estrada'];
$data.= ' ' . $document['unidades']['consumo'];
$data.= ' </td>';
$data.= ' <td>';
$data.= ' ' . $document['data']['combustivel']['1']['consumo']['estrada'];
$data.= ' ' . $document['unidades']['consumo'];
$data.= ' </td>';
$data.= '</tr>';
}
$data.= '</tbody>';
$data.= '</table>';
}
add_shortcode('carro_consumo', 'Carro_Consumo_Shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment