PHP显示单个表

时间:2012-04-21 20:35:14

标签: php html-table

目前,我从数据库生成结果,然后将所有结果插入到一个HTML表中。

是否有任何可能的方式使得每个返回的结果都创建了一个单独的HTML表(与进入一个HTML表的所有结果相对)

我的代码:

<table class=\"board\" width='100%' border='0' align='center' cellpadding='1' cellspacing='1'> 
");
            $type = $_GET["type"];
            if ($type == "" || $type == "request") {
                $get = mysql_query("SELECT * FROM request WHERE type='Request' AND deleted != 'yes' ORDER BY id DESC");
            } else {
                if ($type == "shoutout") {
                    $get = mysql_query("SELECT * FROM request WHERE type='Shoutout' AND deleted != 'yes' ORDER BY id DESC");
                } else {
                    if ($type == "competition") {
                        $get = mysql_query("SELECT * FROM request WHERE type='Competition' AND deleted != 'yes' ORDER BY id DESC");
                    } else {
                        if($type == "all") {
                            $get = mysql_query("SELECT * FROM request WHERE deleted != 'yes' ORDER BY id DESC");                            
                        }
                    }
                }
            }
            $num = @mysql_num_rows($get);
            if ($num == 0) {
                echo ("<div class=\"board\"><center><font color=\"red\">There aren't any requests in this category!<br />
Why not ask listeners to send in their requests?</font></center></div>");
            } else {
                while ($r = mysql_fetch_array($get)) {
                    echo " 
 <tr>
    <td><font face=\"verdana\" size=\"1\"><b>User</b></td>
    <td><font face=\"verdana\" size=\"1\"><b>$r[habboname]</b></td>
  </tr>
  <tr>
    <td><font face=\"verdana\" size=\"1\"><b>Date</b></td>
    <td><font face=\"verdana\" size=\"1\">$r[date]</td>
  </tr>
  <tr>
    <td><font face=\"verdana\" size=\"1\"><b>IP</b></td>
    <td><font face=\"verdana\" size=\"1\">$r[ip]</td>
  </tr>
  <tr>
    <td><font face=\"verdana\" size=\"1\"><b>Message</b></td>
    <td><font face=\"verdana\" size=\"1\">$r[message]</td>
  </tr>
   <tr>
    <td><font face=\"verdana\" size=\"1\"><b>Type</b></td>
    <td><font face=\"verdana\" size=\"1\">$r[type]</td>
  </tr>
  <tr>
  <!--  <td><font face=\"verdana\" size=\"1\"><b>Refferer</b></td>
    <td><font face=\"verdana\" size=\"1\">"; 
    if($r[refferer] == 1){ echo "Site.com</td>"; } else { echo "Site1.com</td>"; } 

echo "--></tr>
  <tr>
    <td><font face=\"verdana\" size=\"1\"><b>Commands</b></td>
    <td><a href=\"vrequest.php?id=$r[id]\"><font face=\"verdana\" size=\"1\">Delete</a> - <a href=\"mode.php?ban=1&id=$r[id]&ip=$r[ip]&message=$r[message]\">Ban</a> - <!--<a href=\"site_alert.php?type=user&ip=" . $r["ip"] . "\">Alert</a>--><a href=\"lotw.php\">Nominate LoTW</a></font></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
";
                    $id = htmlspecialchars($_GET['id']);
                    $delete = mysql_query("UPDATE request SET deleted = 'yes' WHERE id = '$id'");
                }
                echo ("</table></div>");
            }
            break;
        case 'delete':

2 个答案:

答案 0 :(得分:1)

如果您要创建与返回的行一样多的表,只需将开头<table>放入while的开头,将结束</table>放到{{{}的末尾。 1}}。但是,我不明白为什么会这样。你真的想用一行创建很多表吗?它有点挫败了桌子的目的。制作一个列表或使用while或除表之外的任何其他内容。

span

更新。还有一件事。请不要使用内联样式。你有while ($r = mysql_fetch_array($get)) { echo "<table class=\"board\"> <tr> <td><b>User</b></td> <td><b>$r[habboname]</b></td> </tr> <tr> <td><b>Date</b></td> <td>$r[date]</td> </tr> <tr> <td><b>IP</b></td> <td>$r[ip]</td> </tr> <tr> <td><b>Message</b></td> <td>$r[message]</td> </tr> <tr> <td><b>Type</b></td> <td>$r[type]</td> </tr> <tr> <!-- <td><b>Refferer</b></td> <td>"; if($r[refferer] == 1){ echo "Site.com</td>"; } else { echo "Site1.com</td>"; } echo "--></tr> <tr> <td><b>Commands</b></td> <td><a href=\"vrequest.php?id=$r[id]\">Delete</a> - <a href=\"mode.php?ban=1&id=$r[id]&ip=$r[ip]&message=$r[message]\">Ban</a> - <!--<a href=\"site_alert.php?type=user&ip=" . $r["ip"] . "\">Alert</a>--><a href=\"lotw.php\">Nominate LoTW</a></font></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table>"; $id = htmlspecialchars($_GET['id']); $delete = mysql_query("UPDATE request SET deleted = 'yes' WHERE id = '$id'"); } echo "</div>"; //remove () after echo ,所以你可能已经知道了CSS的强大功能。

第二次更新。我删除了table class="board"个标签。像这样使用CSS:

<font ...>

当您想要更改样式时,CSS会省去很多麻烦。相信我,有一段时间你想要改变它(或者你的老板想要把网站叮当作响......)。

答案 1 :(得分:0)

ZZ-bb是绝对正确的,作为一个提示尝试将if / else构造放在mysql中 - 它会更快地移动或在php中优化它,如:

$get = mysql_query("SELECT * FROM request WHERE " . ($type == 'all' ? 'type = 1' : 'type = ucfirst($type)' ) . " AND deleted != 'yes' ORDER BY id DESC");