我的问题比标题更广泛,但我会尝试解释我在做什么。
好的开始让我们假装我有两张桌子:
表名:主机
+------------+---------+
| host | hostid |
+------------+---------+
| EXAMPLE 1 | 10010 |
| EXAMPLE 2 | 10011 |
| EXAMPLE 3 | 10012 |
| EXAMPLE 4 | 10013 |
+------------+---------+
表名:项目
+---------+--------------+---------+---------+
| itemid | itemname | value | hostid |
+---------+--------------+---------+---------+
| 27037 | examplename1 | DATA 1 | 10010 |
| 27038 | examplename2 | DATA 2 | 10011 |
| 27039 | examplename3 | DATA 3 | 10012 |
| 27040 | examplename4 | DATA 4 | 10013 |
| 27041 | examplename5 | DATA 5 | 10013 |
| 27042 | examplename6 | DATA 6 | 10013 |
| 27043 | examplename7 | DATA 7 | 10013 |
+---------+--------------+---------+---------+
现在我使用以下代码将它们放入网页中:
<?php
$host = 'This is entered by user previously'
$sql = "SELECT hosts.host, items.itemname, items.value
FROM items
LEFT JOIN hosts ON hosts.hostid = items.hostid
WHERE hosts.host LIKE '$host'";
$Connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
$Db = @mysql_select_db(DB_DATABASE, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
$result = @mysql_query($sql,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
mysql_select_db(DB_DATABASE);
$num_rows = mysql_num_rows($result);
print "<table width=1000px border=1>\n";
$cols = 0;
while ($get_info = mysql_fetch_assoc($result)){
if($cols == 0)
{
$cols = 1;
print "<tr>";
print "<th>Host</th>";
print "<th>Item Name</th>";
print "<th>Item Value</th>";
print "<tr>\n";
}
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=3/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
如果用户输入了“EXAMPLE 4”,这将是结果表:
+------------+--------------+------------+
| Host | Item Name | Item Value |
+------------+--------------+------------+
| EXAMPLE 4 | examplename4 | DATA 4 |
| EXAMPLE 4 | examplename5 | DATA 5 |
| EXAMPLE 4 | examplename6 | DATA 6 |
| EXAMPLE 4 | examplename7 | DATA 7 |
+------------+--------------+------------+
我现在需要对此进行调整,以便在每行旁边显示一个复选框来显示结果表。我需要这些复选框与每行的“itemid”相对应(未显示在表格中),以便在进入下一页时我可以从中设置变量。例如,如果我在下一页勾选了“DATA 4”和“DATA 7”,它们将成为值“27040”和“27043”的数组的一部分。
如果措辞不当,我深表歉意,但我很难理解这个过程。