Hello,

I am in the unfortunate position of being the developer of a popular Drupal module, AND being completely hopeless at programming in general and Drupal in particular. Please help!

I am writing a page which outputs a form with a list of nodes, and a checkbox next to each node.
For now, it looks l iike this:

function return_form(){
 $sql = "SELECT nid from node n where n.type='drigg' AND n.created > UNIX_TIMESTAMP(NOW()) and n.status=0 ORDER BY n.created";

 $result = pager_query($sql, 15, 0, "SELECT count(*) FROM node n WHERE n.type='drigg' AND n.created > UNIX_TIMESTAMP(NOW()) and n.status=0" );
  while( $data=db_fetch_object($result) ){
    $n=node_load( array('nid' => $data->nid));

    # I NEED HELP HERE!!!
    # How do I change this so that it becomes "delete[4545]" ?
    $form[$n->nid]["delete"]=array(
      '#type' => 'checkbox', 
    );

    $form[$n->nid]['preview']=array('#value' => 
      node_view($n,TRUE,FALSE,FALSE)
    );
  }

  $form['submit'] = array(
    '#type'   => 'submit',
    '#value'  => t('Apply')
  );
  return $form;
}

the trouble is, I can't figure out how to chance the code so that the checkbox name is delete[4545] for the nid 4545, for example.

Any hints?

Thanks a lot!

Merc,

Comments

criznach’s picture

Try looking at the node admin form from the "content" page in node.module. I believe it uses the "checkboxes" type rather than the single "checkbox" type. This allows you to create an array of checkboxes.

heine’s picture

The module Elements provides a form element that makes generating these kinds of forms much easier.

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

mercmobily’s picture

Hi,

Thanks for the answers!
For those interested, I think I found a simple solution.

The trick is to use the #tree attribute for a form. So, by typing:

   // Approval links: delete form
    $form[$n->nid]['delete']['#tree']=TRUE;
    $form[$n->nid]["delete"][$n->nid]=array(
      '#type' => 'checkbox',
      '#title' => t('Delete'),
    );

I end up with precisely what I requested.
The _submit() hook then contains:

  foreach($form_values['delete'] as $nid => $flag){
    if($flag){
      node_delete($nid);
    }
  }

Bye,

Merc.