You are struggling with some basic php concepts.   I’m guessing your $options array contains integers as the value and the 0th element of the array is a ‘0’.  which is the crux of the problem.

 

‘0’ > 0 will always return false.  

 

Perhaps the comparison that you need to write  write is !== 0.    the extra = sign in php comparisons means exactly equal in both type and value.  Otherwise the ‘0’ string data gets converted to the 0 integer before the comparison.

 

My point about $index vs. $key was really about your code snippet:

 

    foreach($form_state['values']['category_boundary']['category_fieldset']['catg'] as $key => $value)
    {
        if($form_state['values']['category_boundary']['category_fieldset']['catg'][$index] > 0)
        {
               //row is selected.
        }
        else
       {
                 //row is not selected.
        }
    }

 

In this code “as $key => $value” means that the array index will be placed in the variable $key, but then you’re using an undefined variable $index in the $form_state[‘values’] test.  $index is an undefined variable as far as I can tell, but I’m betting it get set by code that you omitted to keep the example sanely short.  Sometimes though when people post these they just haven’t noticed, so I wanted to point it out. 

 

In short changing y our >0 comparison to !==0 should solve the problem.

 

 


From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Kamal Palei
Sent: Thursday, September 13, 2012 5:42 PM
To: support@drupal.org
Subject: Re: [support] Tableselect in forms issue

 

Hi All

Thanks a lot for the input.

 

I am little bit confused with $index stuff. When I add tableselect element, I use the key "['category_boundary']['category_fieldset']['catg']" .  Somewhere deep in form code, for every row's checkbox, what index is used by form.inc I really do not know. So instead of $index what I should use, I am not sure.

 

Tried to debug more. When I am trying to add rows (means none of the rows were selected), if I print the array using dpm(), I get below output.

 

... (Array, 4 elements)

  • 0 (Integer) 0 
  • 1 (Integer) 0
  • 2 (Integer) 0
  • 3 (Integer) 0

When I am tying to delete rows (means one or more rows were selected, in this particular case I had selected rows 2, 3 and 4), if I print the array $form_state['values']['category_boundary']['category_fieldset']['catg']  using dpm(), I get below output,

... (Array, 4 elements)

  • 1 (String, 1 characters ) 1
  • 2 (String, 1 characters ) 2
  • 3 (String, 1 characters ) 3
  • 0 (Integer) 0

Thats the reason, thought of taking $index as key where $index can be one of 0,1,2,3.

Using the logic mentioned in my first email, I will be able to delete the rows 2, 3 and 4.

But the problem is if I try to delete the 1st row  it does not get deleted. I tried to print the array, it looks as below.

 

... (Array, 4 elements)

  • 0 (String, 1 characters ) 0
  • 1 (Integer) 0
  • 2 (Integer) 0
  • 3 (Integer) 0

Since irrespective of fact, 0th row is selected or not, always value is 0.  Thats the reason, why logic (if($form_state['values']['category_boundary']['category_fieldset']['catg'][$index] > 0) ) does not work.

But from above prints, one thing is clear while I have selected 0th row, 0th element is

0 (String, 1 characters ) 0 and when 0th row is not selected, it looks as 0 (Integer) 0  , (please note the difference here) hence, there should be someway to determine if 0th row is selected or not. I am not a PHP expert , so probably missing something here.

 

Can somebody kindly help here, how do I determine if the 0th row is selected or not.


Best Regards

Kamal

Net Cloud Systems, Bangalore

 

On Thu, Sep 13, 2012 at 11:35 PM, Metzler, David <metzlerd@evergreen.edu> wrote:

Wow I can’t believe I didn’t catch that either.  Good eye!

 


From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Anthony
Sent: Thursday, September 13, 2012 10:01 AM
To: support@drupal.org
Subject: Re: [support] Tableselect in forms issue

 

Shouldn't it be greater equal zero to get the first element?

On Thu, Sep 13, 2012 at 8:13 AM, Metzler, David <metzlerd@evergreen.edu> wrote:

Seems like it should work provided you really meant $index to be $key.   That’s not in your real code, right? If it is in your cod that way, that’s the bug.

 


From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Kamal Palei
Sent: Thursday, September 13, 2012 5:32 AM
To: support@drupal.org
Subject: [support] Tableselect in forms issue

 

Hi All

I am trying to use tableselect in forms as per below code.

 

    $form['category_boundary']['category_fieldset']['catg'] = array
          (
          '#type' => 'tableselect',
          '#header' => $header,
          '#options' => $options,
          '#multiple' => TRUE,
          '#js_select' => FALSE,
          );

 By manipulating options array, I can add the rows in forms table.

 

Sometimes I need to remove the selected rows from form table.

My question is, how can I determine if a particular row is selected.

 

Currently using below code to determine which row is selected.

 

    foreach($form_state['values']['category_boundary']['category_fieldset']['catg'] as $key => $value)
    {
        if($form_state['values']['category_boundary']['category_fieldset']['catg'][$index] > 0)
        {
               //row is selected.
        }
        else
       {
                 //row is not selected.
        }
    }

 

I can determine all the rows if those are selected or not except the 0th row.

Can somebody tell me, what is the RIGHT way to determine if a particular row in a table is selected or not.

 

Best Regards

Kamal

 

 

 

 

 

 

 

 

 


--
[ Drupal support list | http://lists.drupal.org/ ]




--

Anthony Stefan Maciejowski

 

 


--
[ Drupal support list | http://lists.drupal.org/ ]