3

我有一个 while 循环获取一行,我正在尝试创建一个结构,以便每行将创建 2 个链接。第一个是德语单词,第二个是英语单词。我得到的输出是重复的,就好像行没有增加一样。我把它缩小到这个:

PHP:

while ($row = $database->row()->fetch()) {
    foreach ($row as $value) {
        $this->data .= $value . "!";
    }
    list($this->pid, $this->german, $this->english) = explode("!", $this->data);
    $this->links .= "<a href=\"#\" class=\"german $this->pid\">$this->german</a><br/><a href=\"#\" class=\"english $this->pid\">$this->english</a><br/>";
}

输出:

die Männer
men
die Männer
men
4

2 回答 2

0

我通过查看键并基于简单的匹配字符串测试创建变量解决了这个问题。如果有人可以改进我的答案,我想听听。谢谢。

PHP:

while ($row = $database->row()->fetch()) {
    while(list($key, $value) = each($row)) {
        if ($key == "PID") {
            $this->pid = $value;
        }
        if ($key == "german") {
            $this->german = $value;
        }
        if ($key == "english") {
            $this->english = $value;
        }
    }
    $this->links .= "<a href=\"#\" class=\"german $this->pid\">$this->german</a><br/><a href=\"#\" class=\"english $this->pid\">$this->english</a><br/>";
}
于 2013-08-29T21:47:03.067 回答
0
    $row = array();
    while ($row = $database->row()->fetch()) {
        $row[] = $row;
    }

    foreach ($row as $value)
    {
        $this->data .= $value . "!";
        list($this->pid, $this->german, $this->english) = explode("!", $this->data);
        $this->links .= "<a href=\"#\" class=\"german $this->pid\">$this->german</a><br/><a href=\"#\" class=\"english $this->pid\">$this->english</a><br/>";
    }
于 2013-08-29T17:34:02.033 回答