我有以下代码要运行,但问题是$ this->类型是在通过指定请愿,提案或修改创建类时设置的。正如您所看到的,我的$ sql语句是所有三个的UNION,我想指定每行数据来自哪个表(pet,prop或revision)。
public function userProposals() {
$username = User::getUsername();
$sql = "SELECT * FROM petition WHERE author = '$username'
UNION SELECT * FROM proposition WHERE author = '$username'
UNION SELECT * FROM amendment WHERE author = '$username'";
$query = mysql_query($sql);
$state = User::userState();
while ($row = mysql_fetch_assoc($query)) { // $this->type needs to specify pet,prop,amend
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$this->type&id=$row[id]'>$row[title]</a></td>
<td>$this->type</td>
<td>$state</td>
</tr>";
}
}
如您所见,$ this-&gt;类型只会说三者中的一个。为了让我的功能按照我想要的方式工作,我做了这个(我感觉太长了,而且必须有一个更短的路)。
public function userProposals() {
$username = User::getUsername();
$state = User::userState();
$sql = "SELECT * FROM petition WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=petition&id=$row[id]'>$row[title]</a></td>
<td>Petition</td>
<td>$state</td>
</tr>";
}
$sql = "SELECT * FROM proposition WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=proposition&id=$row[id]'>$row[title]</a></td>
<td>Proposition</td>
<td>$state</td>
</tr>";
}
$sql = "SELECT * FROM amendment WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=amendment&id=$row[id]'>$row[title]</a></td>
<td>Amendment</td>
<td>$state</td>
</tr>";
}
}
答案 0 :(得分:2)
答案 1 :(得分:1)
我通常会做以下事情:
public function userProposals() {
$username = User::getUsername();
$state = User::userState();
$tables = array('petition', 'proposition', 'amendment');
foreach($tables as $table) {
$label = ucwords($table);
$sql = "SELECT * FROM $table WHERE author = '" . mysql_real_escape_string($username) . "'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$table&id=$row[id]'>$row[title]</a></td>
<td>$label</td>
<td>$state</td>
</tr>";
}
}
}
答案 2 :(得分:0)
为什么不尝试修改后的SQL:
SELECT 'petition' as typ,title,id FROM petition
WHERE author = '$username'
UNION SELECT 'proposition' as typ,title,id FROM proposition
WHERE author = '$username'
UNION SELECT 'amendment' as typ,totle,id FROM amendment
WHERE author = '$username'"
然后使用每个返回的行typ
而不是($row[typ])
中的$this->type
?
整个事情应该是:
public function userProposals() {
$username = User::getUsername();
$sql = "SELECT 'petition' as typ,id,title FROM petition
WHERE author = '$username'
UNION SELECT 'proposition' as typ,id,title FROM proposition
WHERE author = '$username'
UNION SELECT 'amendment' as typ,id,title FROM amendment
WHERE author = '$username'"
$query = mysql_query($sql);
$state = User::userState();
while ($row = mysql_fetch_assoc($query)) {
echo "<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$row[typ]&id=$row[id]'>
$row[title]
</a></td>
<td>$row[typ]</td>
<td>$state</td>
</tr>";
}
}
根据你问题中的内容。