Bu kodu Yoğuşmalı

3 Cevap php

Ben çalıştırmak istiyorum aşağıdaki kodu var, ama sorun sınıf dilekçe, öneri, ya da değişiklik ya belirterek oluşturulduğunda $ this-> tip ayarlanmış olmasıdır. Gördüğünüz gibi benim $ sql deyimi üçünün birliğidir, ve hangi tablo (pet, pervane, ya da tadil) belirtmek gelen her veri satırı istiyorum.

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

Gördüğünüz gibi, $ this-> türü yalnızca üç birini diyecekler. Ben istedim nasıl benim işlevi iş elde etmek için, (Ben kısa bir yolu olmalı çok uzun ve hissediyorum) bu yaptım.

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 Cevap

Ben normalde aşağıdaki gibi bir şey yapardı:

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

alt text

Neden değiştirilmiş SQL kalkma:

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'"

ve daha sonra her bir satırın geri ($row[typ]) yerine gelen typ kullanımı $this->type?

Her şey olmalıdır:

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

Sorunuzu ne göre.