使用 DOMDocument 解析 HTML
到目前为止我还没有真正使用过 DOMDocument,我想它现在适合我的需要,无论如何,我有以下代码:
while ($cat = mysql_fetch_assoc($r))
{
$inputholder .= "<input type='checkbox' name='$cat[cat_name]' value='$cat[cat_id]' />$cat[cat_name]";
}
这基本上根据类别名称和 id 构建了一个复选框列表。
现在我想在处理这个 HTML 后解析它(例如,当我发现是否要检查它们的 sevrel 时。
所以我知道我需要做什么
$dom = new DOMDocument();
$dom -> loadHTML($inputholder);
现在,我想检查是否有 $cat[ 的记录) 'cat_name'] 在我的数据库中,如果有的话,我想添加一个属性 ("checked='checked'")
到正确的元素(在本例中,名称等于我的元素) 。
任何帮助 将不胜感激
编辑: 所以我基本上使用3个表,一个是联结表: 1 个表保存参数,1 个表保存站点名称,第 3 个表保存 site_id 和 param_id(或 cat_id),因此例如:
表 1
site_id url
1 http://google.com
2 http://youtube.com
3 http://facebook.com
表 2
cat_id cat_name
1 social
2 tubes
3 search
和表 3
uid site_id cat_id
1 1 3
2 2 1
3 2 2
4 3 1
5 3 3
现在我用来连接这些表的查询是 LEFT JOIN
SELECT u.id, u.name, u.url, u.url2, ca.cat_id, ca.cat_name FROM urls u
LEFT JOIN cat_urls cu ON cu.url_id = u.id
LEFT JOIN categories ca ON ca.cat_id = cu.cat_id
ORDER BY u.id ASC
当 cat_id 为 NULL 时,我知道我可以安全地打印未选中的复选框,但如果每个站点有超过 1 个类别,我无法知道这一点,那就是为什么我想运行 DOMManipulation。
希望它能澄清我的问题。
I haven't really used DOMDocument up until now, and I guess it suits my needs now, anyway, I have the following code:
while ($cat = mysql_fetch_assoc($r))
{
$inputholder .= "<input type='checkbox' name='$cat[cat_name]' value='$cat[cat_id]' />$cat[cat_name]";
}
This basically builds a list of checkboxes according to category names and id's.
Now I want to parse out this HTML once it's being processed (eg. when I find out wether to check sevrel of them or not.
so what I know I need to do
$dom = new DOMDocument();
$dom -> loadHTML($inputholder);
Now, I want to check there's a record for a $cat['cat_name'] in my database, if there's, I want to add an attribute("checked='checked'")
to the correct element (in this case, the one whose name is equal to my query.
Any help will be appreciated.
EDIT:
So I basically use 3 tables, one is a junction table:
1 table holds parameters, 1 table holds site names and the 3rd table hold both the site_id and the param_id (or cat_id), so for example:
table 1
site_id url
1 http://google.com
2 http://youtube.com
3 http://facebook.com
table 2
cat_id cat_name
1 social
2 tubes
3 search
and table 3
uid site_id cat_id
1 1 3
2 2 1
3 2 2
4 3 1
5 3 3
Now the query I use to join those tables is a LEFT JOIN
SELECT u.id, u.name, u.url, u.url2, ca.cat_id, ca.cat_name FROM urls u
LEFT JOIN cat_urls cu ON cu.url_id = u.id
LEFT JOIN categories ca ON ca.cat_id = cu.cat_id
ORDER BY u.id ASC
When it's NULL on cat_id, I know that I can safely print the checkboxes unchecked, but if there's more than 1 category per site I have no way of knowing that, that's why I wanted to run a DOMManipulation.
Hope it clarifies my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
迈克尔是对的。您可以(并且应该)在第一次请求数据库时执行这两项操作。它更快,需要更少的计算,更有意义并且需要更少的代码。
注意:我还为您的复选框添加了标签。
Michael is right. You could (and should) do both at the moment you first request your database. It is faster, requires less computing, makes more sense and needs less code.
NB: I also added labels for your checkboxes.