0

目前,我从数据库中生成结果,然后将所有结果插入到一个 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':
4

2 回答 2

1

如果您的意思是创建与返回的行一样多的表,只需将开头<table>放在while's 开头,将结尾</table>放在while. 但是,我不明白为什么会这样。你真的想用一行创建很多表吗?它有点违背了桌子的目的。制作一个列表或使用span's 或除表格以外的任何其他内容。

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

更新。另一件事。请不要使用内联样式。你有table class="board",所以你可能已经知道 CSS 的力量了。

第二次更新。我删除了<font ...>标签。例如像这样使用 CSS:

table.board{
  width:100%;
  border:0;
  align:center;
  cellpadding:1;
  cellspacing:1;
}

table.board td{
  font-family: Verdana, Geneva, Arial, sans-serif;
  font-size: 1em;
}

当您想要更改样式时,CSS 可以省去很多麻烦。相信我,总有一天你会想要改变它(或者你的老板想要对网站进行炫耀......)。

于 2012-04-21T20:43:58.240 回答
0

ZZ-bb is absolutely right and as a hint try to put if/else construction in mysql - it will move faster or optimize it in php, like:

$get = mysql_query("SELECT * FROM request WHERE " . ($type == 'all' ? 'type = 1' : 'type = ucfirst($type)' ) . " AND deleted != 'yes' ORDER BY id DESC");
于 2012-04-21T21:17:03.043 回答