我有一个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
答案 0 :(得分: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/>";
}
答案 1 :(得分: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/>";
}