} if (!isset($add) || $add) { $post = $doc->createElement('post'); $post->setAttribute('post-id', $post_row['id']); $post->setAttribute('group-name', htmlspecialchars($post_row['name'])); $post->setAttribute('author-name', htmlspecialchars($post_row['author_name'])); $post->setAttribute('created', htmlspecialchars(formatdatetime($post_row['created']))); if (isset($post_row['image'])) { $post->setAttribute('image', htmlspecialchars($post_row['image'])); } //leave room for adding comments $comment_stmt = dbexec($select_db, 'SELECT c.id, c.message, c.created, (SELECT u.username FROM users u JOIN comments c2 ON c2.author_id = u.id WHERE c2.author_id = c.author_id LIMIT 1) as author_name FROM comments c WHERE c.post_id = ?', array($post_row['id']), array(PDO::PARAM_INT)); $comments = $doc->createElement('comments'); while ($comment_row = $comment_stmt->fetch(PDO::FETCH_ASSOC)) { $comment = $doc->createElement('comment'); $comment->setAttribute('id', htmlspecialchars($comment_row['id'])); $comment->setAttribute('author-name', htmlspecialchars($comment_row['author_name'])); $comment->setAttribute('created', htmlspecialchars(formatdatetime($comment_row['created']))); $comment->appendChild($doc->createTextNode($comment_row['message'])); $comments->appendChild($comment); } $post->appendChild($comments); $post->appendChild($doc->createTextNode($post_row['message'])); $root->appendChild($post); } } header('Content-type: text/xml'); echo $doc->saveXML();
/** * Método que recebe uma query como parâmetro executa esta query guardando o resultado na variável local $a. * Cria uma variável $fields do tipo array que irá armazenar todos os campos da query em questão por meio do resultado da função fetch_fields(), usando um forech ele armazenará o nome do campo, seu datatype e quantos caracteres são permitidos nesse campo. *Cria uma variável $data do tipo array que irá armazenar o retorno dos dados obtidos pela query, antes porém ela executa um foreach para formatar os campos datetime, decimal e money no padrão americano e tirando espaços para os demais tipos. alimenta o array $data com os registros formatados em $record. * @metodo arrays */ public function arrays($query, $array = false, $params = array()) { $a = $this->query($query, $params); $fields = array(); $finfo = $a->fetch_fields(); foreach ($finfo as $val) { array_push($fields, array("field" => $val->name, "type" => strtoupper($val->type), "max_length" => $val->max_length)); } $data = array(); while ($a1 = $a->fetch_array()) { $record = array(); foreach ($fields as $f) { switch ($f['type']) { case 'DATETIME': $record[$f['field']] = strtotime(formatdatetime($a1[$f['field']], 8)); break; case 'MONEY': $record[$f['field']] = number_format($a1[$f['field']], 2, '.', ''); break; case 'DECIMAL': $record[$f['field']] = number_format($a1[$f['field']], 2, '.', ''); break; default: $value = $this->utf8 === true ? utf8_encode(trim($a1[$f['field']])) : trim($a1[$f['field']]); $record[$f['field']] = $value; unset($value); break; } } array_push($data, $record); } if (!$array) { return $data; } else { if (count($data) == 1 && $array) { return $data[0]; } else { return $data; } } }