如何列出表单的字段名称

发布于 2024-12-01 14:58:36 字数 304 浏览 5 评论 0原文

我有一个表单,想要生成表单字段名称的列表。这是我目前的做法:

$fieldnames = array();  
foreach ($form as $key=>$val){
    if (substr($key, 0, 6) === 'field_'){ 
      $fieldnames[] = $key;
    }
}

有更好的方法吗?

更新: 只是为了澄清......我想知道是否有一种不那么“笨拙”的方式来做到这一点。例如,内容模块是否提供了循环字段的 api 函数。 (我找不到一个。)

I have a form, and want to generate a list of the form's field-names. Here is how I currently do it:

$fieldnames = array();  
foreach ($form as $key=>$val){
    if (substr($key, 0, 6) === 'field_'){ 
      $fieldnames[] = $key;
    }
}

Is there a better way to do this?

UPDATE:
Just to clarify ... I am wondering whether there is a less "kludgey" way of doing this. For example, does the content module provide an api function that loops through fields. (I couldn't find one.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2024-12-08 14:58:37

您通过 cck 添加的字段...或从 UI 字段系统添加的字段以“field_”开头
这个字段通常在节点中......所以如果你谈论的是节点形式
以及由 cck 添加的字段...您的方式是正确的...但是如果此字段是通过编程添加的...那么您的方式是错误的

the field that you added by cck...or from UI field system are begin with "field_"
and this fields are usually in the nodes...so if you are talkin about nodes form
and fields that added by cck....you are in the correct way... but if this fields are added programmatically....so you are in the wrong way

喵星人汪星人 2024-12-08 14:58:37

抱歉,我不是 100% 确定,但我认为您无法获取以编程方式添加的所有字段..但是如果您从 cck 或从 '/admin/content/node-type/stores/fields' 添加此字段,其中 {stores } 是您正在使用的内容类型,然后您可以从 {content_node_field_instance} 表获取此字段名称,如下所示

$result_handle = db_query("select field_name from {content_node_field_instance} where 
`type_name` = '%s'","yourContentTypeName") ;
while($result_object = db_fetch_object($result_handle)){
 $fields[] = result_object->field_name ; 
}

现在您拥有数组 $fields,其中包含您的内容类型的所有字段...我希望这会对您有所帮助

sorry im not 100% sure but i don't think you can get all the fields that added programatically..but if you added this fields from cck or from '/admin/content/node-type/stores/fields' where {stores} is your content type that you are working with then you can get this fields name from {content_node_field_instance} table as the following

$result_handle = db_query("select field_name from {content_node_field_instance} where 
`type_name` = '%s'","yourContentTypeName") ;
while($result_object = db_fetch_object($result_handle)){
 $fields[] = result_object->field_name ; 
}

now you have the array $fields which hav all the fields of your content type...i hope that will help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文