douli6605 2009-07-09 23:57 采纳率: 100%
浏览 12
已采纳

冷凝此代码

I have the following code I want to run, but the problem is $this->type is set when the class is created by specifying either petition, proposal, or amendment. As you can see my $sql statement is a UNION of all three, and I want to specify which table (pet,prop,or amend) each row of data comes from.

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>";
    }
}

As you can see, $this->type will only say one of the three. To get my function work how i wanted to, I did this (which I feel is too long & there must be a shorter way).

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>";
    }
}
  • 写回答

3条回答 默认 最新

  • doukang2003 2009-07-10 00:20
    关注

    I would normally do something like the following:

    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条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向