我的问题比标题要广泛一些,但我会尝试解释我在做什么。
好的开始让我们假装我已经去了两张桌子:
表名:主机
+------------+---------+
| 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";
?>
如果用户输入了"示例 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"的数组的一部分。
抱歉,如果措辞不好,但我很难理解这个过程。
方法可以做到这一点。
一种简单的方法是在命名复选框时使用项目 ID,这样您就可以在触发相应的事件以提交信息时获取复选框本身所需的值。
另一个是隐藏输入,其命名方式与相应的复选框类似(即复选框 ID= 'c_1',隐藏输入 id= 'h_1',其中隐藏输入的值='item_id')