Page 1 of 1

Tickable-class 'checked' - how to?

Posted: Tue Nov 09, 2021 9:19 pm
by Kofod95
I have a bunch of checkboxes passing values to $_GET['attrib_1'] (and a bunch more to $_GET['attrib_2'] and so on), to allow filtering through products according to their assigned attributes.
However, I seem to fail at marking the correct checkboxes as checked, so I'm surely doing something stupid..

Below is my code:

Code: Select all

			 
foreach ($products_attributes['options_array'] as $options_value) {
  $checked = ''; 
  if (isset($_GET['attrib_' . $products_attributes['options_id']]) && $_GET['attrib_' . $products_attributes['options_id']] === $options_value['products_options_values_id']) $checked = 'checked'; 

  $attribute_filter .= new Tickable('attrib_' . $products_attributes['options_id'], [
                                 'value' => $options_value['products_options_values_id'],
				 'id' => 'po-id-' . $options_value['products_options_values_id'],
				 'aria-describedby' => $options_value['products_options_values_name'],
				 'class' => 'custom-control-input filter-input filter-' . $products_attributes['options_id'],
				 'onchange' => 'this.form.submit()',
				 $checked => '',
				 ], 'checkbox');
				 ...
When choosing the last value (with demo attributes: 24), it does as intended, but when choosing one above that, it marks that and all boxes below as checked (it still passes the right value(s), but visually it is wrong).
I expected the loop to reset the 'checked' => 'checked' thing, but I guess it is not, and I don't know how to correct it - can someone maybe help?

(There might a lot of other problems with my code - I don't really know what I'm doing, but usually I manage to try enough times to make it work. Suggestions for improvements on other stuff is welcome as well, but the main thing is the checkbox-checking)

//Daniel

Re: Tickable-class 'checked' - how to?

Posted: Tue Nov 09, 2021 9:58 pm
by ecartz

Code: Select all

$option_key = 'attrib_' . $products_attributes['options_id'];
$tickable = new Tickable("{$option_key}[]", [
  'class' => 'custom-control-input filter-input filter-' . $products_attributes['options_id'],
  'onchange' => 'this.form.submit()',
], 'checkbox');
foreach ($products_attributes['options_array'] as $options_value) {
  $value_id = $options_value['products_options_values_id'];
  $tickable->set('value', $value_id);
  $tickable->set('id', 'po-id-' . $value_id);
  $tickable->tick(isset($_GET[$option_key]) && $_GET[$option_key] === $value_id); 
  $attribute_filter .= "$tickable";
I removed the aria-describedby. That should be an ID of another element.

You also might consider if the problem lies outside the code that you posted.

Re: Tickable-class 'checked' - how to?

Posted: Wed Nov 10, 2021 9:45 am
by Kofod95
Thank you @ecartz for helping!
ecartz wrote: Tue Nov 09, 2021 9:58 pm You also might consider if the problem lies outside the code that you posted.
This might be where the dog is burried. Using the code you posted checks all boxes all the time, which makes me question whatever I have before it.
However, setting the $value_id in the tick-check manually to '1' checks all boxes, setting it to '2' checks from '2' and down. Could that also be caused be previous code?

Just in case someone has the time to look at it, I've posted the entire function below - I will keep trying myself, to see if I can find something :)

Code: Select all

    
public function listen_drawForm() {
  global $current_category_id, $languages_id;

  $products_attributes_array = array();

  $products_options_name_query = tep_db_query("select distinct po.* from products_options po, products_attributes pa, products_to_categories p2c, products p where pa.products_id = p2c.products_id  and p.products_id = p2c.products_id and p.products_status = '1' and po.products_options_id = pa.options_id and p2c.categories_id = '" . (int)$current_category_id . "' and po.language_id = '" . (int)$languages_id . "' order by po.products_options_id");

  if (tep_db_num_rows($products_options_name_query)) {
    while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {
      $products_attributes_array[$products_options_name['products_options_id']] = array('options_id' => $products_options_name['products_options_id'],
                   'options_name' => $products_options_name['products_options_name']);

      $products_options_query = tep_db_query("select distinct pov.* from products_options_values pov,  products_attributes pa, products_to_categories p2c, products p where pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pa.products_id = p2c.products_id and p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int)$current_category_id . "' and pov.language_id = '" . (int)$languages_id . "' order by products_options_values_id");

      while ($products_options = tep_db_fetch_array($products_options_query)) {
        $products_attributes_array[$products_options_name['products_options_id']]['options_array'][] = $products_options;
      }
    }
        
    $attribute_filter = null;
    $attribute_filter .= '<ul class="list-group">';
	   
    foreach ($products_attributes_array as $products_attributes) {

      $attribute_filter .= '<li class="list-group-item text-center filters filter-' . $products_attributes['options_id'] . '"><h6>' . $products_attributes['options_name'] . '</h6>';
			 
      $form = new Form('attrib_' . $products_attributes['options_id'], $GLOBALS['Linker']->build('index.php', [], false), 'get');			 

        if (empty($_GET['manufacturers_id'])) {
          $form->hide('cPath', $GLOBALS['cPath']);
        } else {
           $form->hide('manufacturers_id', $_GET['manufacturers_id']);
        }
	$form->hide('sort', $_GET['sort']);
             
        $attribute_filter .= $form;
			 
          $option_key = 'attrib_' . $products_attributes['options_id'];
          $tickable = new Tickable($option_key, [
                             'class' => 'custom-control-input filter-input filter-' . $products_attributes['options_id'],
                             'onchange' => 'this.form.submit()',
                            ], 'checkbox');
          foreach ($products_attributes['options_array'] as $options_value) {
            $value_id = $options_value['products_options_values_id'];
            $tickable->set('value', $value_id);
            $tickable->set('id', 'po-id-' . $value_id);
            $tickable->tick(isset($_GET[$option_key]) && $_GET[$option_key] === $value_id); 
            $attribute_filter .= "$tickable";
								 
	   $attribute_filter .= '<label class="custom-control-label" for="po-id-' . $options_value['products_options_values_id'] .'">' . $options_value['products_options_values_name'] . '</label>';
	   $attribute_filter .= '<br>';
				
         }
			 
	 $attribute_filter .= '</form>';
	 $attribute_filter .= '</li>';
			 
      }
		
      $attribute_filter .= '</ul>';
		
    }
	  
    if(isset($attribute_filter) && !empty($attribute_filter)) return $attribute_filter;
  }
Again thank you for the help so far!

//Daniel

Re: Tickable-class 'checked' - how to?

Posted: Wed Nov 10, 2021 10:00 am
by ecartz
What is the HTML that it produces? I.e. what do you get if view the HTML source in the browser.

Re: Tickable-class 'checked' - how to?

Posted: Wed Nov 10, 2021 10:21 am
by Kofod95

Code: Select all

<ul class="list-group">
  <li class="list-group-item text-center filters filter-1"><h6>Box Size</h6>
    <form name="attrib_1" action="http://localhost/1.0.8.4/index.php" method="get">
      <input name="cPath" type="hidden" value="1_3" class="form-control">
      <input name="sort" type="hidden" value="1a" class="form-control">
      <input name="attrib_1" type="checkbox" class="custom-control-input filter-input filter-1" onchange="this.form.submit()" value="1" id="po-id-1">
      <label class="custom-control-label" for="po-id-1">12</label>
      <br>
      <input name="attrib_1" type="checkbox" class="custom-control-input filter-input filter-1" onchange="this.form.submit()" value="2" id="po-id-2">
      <label class="custom-control-label" for="po-id-2">24</label>
      <br>
    </form>
  </li>
</ul>

Re: Tickable-class 'checked' - how to?

Posted: Wed Nov 10, 2021 10:42 am
by ecartz
Try changing

Code: Select all

              $tickable = new Tickable($option_key, [
to

Code: Select all

              $tickable = new Tickable("$option_key[]", [
and see if that makes it start working as you'd expect. Alternately, instead of that, try changing from a checkbox to a radio.

Re: Tickable-class 'checked' - how to?

Posted: Wed Nov 10, 2021 12:50 pm
by Kofod95
Thank you again.

Code: Select all

$tickable = new Tickable("$option_key[]", [
throws:

Code: Select all

Parse error
: syntax error, unexpected ']', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
I tried putting numbers in, which didn't help, neither did it help to put in $value_id (didn't expect it to, but tried anyway).
Changing to radio makes the last box ticked no matter the $_GET value

I tried

Code: Select all

$tickable = new Tickable("$option_key" . '[]', [ 
as well with array to string conversion error (probably obviously).

//Daniel

Re: Tickable-class 'checked' - how to?

Posted: Thu Nov 25, 2021 1:36 pm
by Kofod95
I didn't manage to solve this, but took another approach to make progress. If I ever mange to solve this properly, I will implement that as well, and let my solution known here.

The attribute-filter I'm working on together with @Omar_one should soon be ready for release as an addon.

//Daniel