0

我使用带有数据库连接的 google map api,我有一个输出 xml 文件和 JavaScript 读取坐标的函数。那里没问题。

现在我想添加另一个函数来打印出在数据库中保存坐标的用户。

但是我的 xml 文件是这样打印出来的。

<blog title="Ipsum" text="IpsumLorem" lat="31.968599" lng="-99.901810"/>
<blog username="user1"/>

而不是像这样在第一行打印 user1

<blog title="Ipsum" text="IpsumLorem" lat="31.968599" lng="-99.901810" username="user1" />

这是我的两个功能

function fetch_articles() {
    $query = $this->link->query("SELECT * FROM blog ");  
    $query->setFetchMode(PDO::FETCH_ASSOC);
    while($row = $query->fetch()) {  
        $node = $dom->createElement("blog");  
    $newnode = $parnode->appendChild($node);   
    $newnode->setAttribute("title",$row['title']);
    $newnode->setAttribute("text", $row['text']);  
    $newnode->setAttribute("lat", $row['lat']);  
    $newnode->setAttribute("lng", $row['lng']);  
    } 
}

function fetch_users() {
    $query = $this->link->query("SELECT * FROM user ");  
    $query->setFetchMode(PDO::FETCH_ASSOC);
    while($row = $query->fetch()) {  
        $node = $dom->createElement("blog");  
    $newnode = $parnode->appendChild($node);   
    $newnode->setAttribute("username",$row['username']);
    } 
}



 $area = new Areas();
$area_info = $area->fetch_articles(); 
$user_info = $area->fetch_users(); 
echo $dom->saveXML();

有任何想法吗 ?

4

2 回答 2

2

您需要将属性更改应用于相同的DOMElement. 因为这两种方法实际上包含完全相同的代码,所以也值得封装它:

$blog = $dom->createElement("blog");

$apply = new DomAttributeApplyIterator(
    new PdoQueryIterator($pdo, "SELECT * FROM blog"), $blog
);
$apply->apply();

$apply = new DomAttributeApplyIterator(
    new PdoQueryIterator($pdo, "SELECT * FROM user"), $blog
);
$apply->apply();

echo $dom->saveXML();

使用的两种新类型:

class DomAttributeApplyIterator extends IteratorIterator
{
    /**
     * @var DOMElement
     */
    private $element;

    public function __construct(Traversable $iterator, DOMElement $element) {
        parent::__construct($iterator);
        $this->element = $element;
    }

    public function apply() {
        foreach($this as $key => $value) {
            $this->element->setAttribute($key, $value);
        }
    }
}

class PdoQueryIterator implements IteratorAggregate
{
    private $pdo, $query;

    public function __construct(PDO $pdo, $query) {
        $this->pdo = $pdo;
        $this->query = $query;
    }

    public function getIterator() {
        $stmt = $this->pdo->query($this->query);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        return $stmt;
    }
}
于 2013-05-19T15:38:10.710 回答
1

您拥有的代码缺少将用户与博客文章联系起来的方法。因此,最简单的方法可能是编写一个在单个结果中获取两者的查询,然后将其添加到 DOM 节点。它可以像这样工作:

function fetch_articles_with_username() {
    $query = $this->link->query("SELECT * FROM blog LEFT JOIN user WHERE blog.userid = user.id ");  
    $query->setFetchMode(PDO::FETCH_ASSOC);
    while($row = $query->fetch()) {  
        $node = $dom->createElement("blog");  
        $newnode = $parnode->appendChild($node);   
        $newnode->setAttribute("title",$row['title']);
        $newnode->setAttribute("text", $row['text']);  
        $newnode->setAttribute("lat", $row['lat']);  
        $newnode->setAttribute("lng", $row['lng']);  
        $newnode->setAttribute("username",$row['username']);
    } 
}

您可以像上面一样使用它,只需调用fetch_articles_with_username().

于 2013-05-19T15:38:35.370 回答