dimanche 2 février 2020

jQuery convert checkbox selections to array ( when unchecked make it empty)

There is a solution here but I have a problem . when check box is checked and unchecked i wanted it to be empty but it results in

{ "category": [] }

but I wanted it to empty {}

const array = {};

$('[name^="pgggo-"]').on('click', function() {
  const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
  const id = attr.split('[')[0];
  const checked = $(this).prop('checked');
  
  array[taxonomy] = array[taxonomy] || [];
  const index = array[taxonomy].indexOf(id);
  index == -1 ? array[taxonomy].push(id) : array[taxonomy].splice(index, 1);
  
  console.log(array);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="pgggo-list-taxon">
  <li>
    <label>
            <input type="checkbox" name="pgggo-category-sep-56[]">
            <div class="icon-box">
                <div name="development">Development</div>
            </div>
        </label>
  </li>
  <li>
    <label>
            <input type="checkbox" name="pgggo-category-sep-14[]">
            <div class="icon-box">
                <div name="food">Food (category)</div>
            </div>
        </label>
  </li>
  <li>
    <label>
            <input type="checkbox" name="pgggo-home-sep-14[]">
            <div class="icon-box">
                <div name="food">Food (home)</div>
            </div>
        </label>
  </li>
  <li>
    <label>
            <input type="checkbox" name="pgggo-category-sep-8[]">
            <div class="icon-box">
                <div name="medical">Medical</div>
            </div>
        </label>
  </li>
  <li>
    <label>
            <input type="checkbox" name="pgggo-category-sep-1[]">
            <div class="icon-box">
                <div name="uncategorized">Uncategorized</div>
            </div>
        </label>
  </li>
  <li>
    <label>
            <input type="checkbox" name="pgggo-category-sep-2[]">
            <div class="icon-box">
                <div name="wordpress">WordPress</div>
            </div>
        </label>
  </li>
</div>

Please help




Edit previous record in the checkbox with php

I have a problem to edit previous record by checkbox in php.My problem is when I press the edit button,all can show me previous info record,but only in the checkbox unable to show me the previous record. Below is my coding:

Checkbox html:


    <div class="form-group col-lg-6">
                        <label class="control-label col-lg-4">Pricing<span style="color:red;">&nbsp;</span></label>
                        <div class="col-lg-8">
                    <input type="checkbox" name="rm_option" id="rm_option" value="1"><strong> RM </strong></input>&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="point_option" id="point_option" value="1"><strong> Full Point </strong></input>&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="partial_option" id="partial_option" value="1"><strong> Partial Point + RM </strong></input>


                        </div>
                        </div>

Checkbox echo edit function:

<?php
$sql = "select * from promotion_list where id=" . $_GET['id'];
$arr_sql = db_conn_select($sql);
foreach ($arr_sql as $rs_sql) {
    foreach ($rs_sql as $key => $value) {
        ?>
                                $("#<?php echo $key ?>").val("<?php echo $value ?>");
        <?php
    }
    ?>
                            $("#filter_id").val('<?php echo $rs_sql['id'] ?>');
                            $("#promotion_content").jqteVal('<?php echo $rs_sql['promotion_content'] ?>');
                            $("#promotion_terms").jqteVal('<?php echo $rs_sql['promotion_terms'] ?>');
                            $("#promotion_instruction").jqteVal('<?php echo $rs_sql['promotion_instruction'] ?>');
                            $("#promotion_policy").jqteVal('<?php echo $rs_sql['promotion_policy'] ?>');
                            $("#rm_option").jqteVal('<?php echo $rs_sql['rm_option'] == 1:'checked':'' ?>');
                            $("#point_option").jqteVal('<?php echo $rs_sql['point_option'] == 1:'checked':'' ?>');
                            $("#partial_option").jqteVal('<?php echo $rs_sql['partial_option'] == 1:'checked':'' ?>');
    <?php
}
?>

I think should be my these coding problem (in the below coding), because these function cannot work to let me show the previous record in the checkbox, anyone can guide me how to solve it? Thanks.

$("#rm_option").jqteVal('<?php echo $rs_sql['rm_option'] == 1:'checked':'' ?>');
                            $("#point_option").jqteVal('<?php echo $rs_sql['point_option'] == 1:'checked':'' ?>');
                            $("#partial_option").jqteVal('<?php echo $rs_sql['partial_option'] == 1:'checked':'' ?>');

Below is my output only the checkbox unable to show the previous the tick record:

Output




How change state in flutter?

I'm trying to make a checkbox with images. But i don't know how make the stateChange, i have declared a variable named selected. In the GestureDetector i change the value of selected, if the value of selected is the same of the index i put the border. But the border of the checkbox doesn't change:

class FormularioTransferencia extends StatefulWidget {
  final Champion champion;

  const FormularioTransferencia({Key key, @required this.champion})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return FormularioTransferenciaState();
  }
}

class FormularioTransferenciaState extends State<FormularioTransferencia> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.champion.feName),
        ),
        body: FutureBuilder(
          future: fetchPost(widget.champion.feName),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final champion = json.decode(snapshot.data.toString());
              return _championDetail(champion);
            } else {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
          },
        ));
  }
.....

Widget _championDetail(champion) {
  return SingleChildScrollView(
    child: Column(
      children: <Widget>[
        _avatarPage(champion),
        _titlePage(champion),
        _titleTopic('talents', 30.0),
        _legendaries(champion),
        _titleTopic('abilites', 45.0),
        _abilites(champion),
      ],
    ),
  );
}

Widget _abilites(champion) {
  String selected = '1';

  return Container(
    child: Row(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: GestureDetector(
            onTap: () {
              selected = '1';
              debugPrint(selected);
            },
            child: _abilityIcon(champion, '1', selected),
          ),
        ),
        Expanded(
          flex: 1,
          child: GestureDetector(
            onTap: () {
              selected = '2';
              debugPrint(selected);
            },
            child: _abilityIcon(champion, '2', selected),
          ),
        ),
        Expanded(
          flex: 1,
          child: GestureDetector(
            onTap: () {
              selected = '3';
              debugPrint(selected);
            },
            child: _abilityIcon(champion, '3', selected),
          ),
        ),
        Expanded(
          flex: 1,
          child: GestureDetector(
            onTap: () {
              selected = '4';
              debugPrint(selected);
            },
            child: _abilityIcon(champion, '4', selected),
          ),
        ),
        Expanded(
          flex: 1,
          child: GestureDetector(
            onTap: () {
              selected = '5';
              debugPrint(selected);
            },
            child: _abilityIcon(champion, '5', selected),
          ),
        ),
      ],
    ),
  );
}

Widget _abilityIcon(champion, index, selected) {
  var colorBorder =
      index == selected ? const Color(0x547a8c00) : const Color(0x00000000);

  return Container(
    decoration: BoxDecoration(
      border: Border(
        top: BorderSide(width: 10.0, color: colorBorder),
        left: BorderSide(width: 10.0, color: colorBorder),
        right: BorderSide(width: 10.0, color: colorBorder),
        bottom: BorderSide(width: 10.0, color: colorBorder),
      ),
    ),
    child: Image.network(
        champion[0]['api_information']['Ability_' + index]['URL']),
  );
}

enter image description here




How to make an adaptive display of csv data using the csv's header in check boxes and select option as sum, avg and mode USING php?

View image Please view this image to have an idea. Thank you




samedi 1 février 2020

React: Checkbox does not uncheck when next question comes

I am working on a quiz component, where user can appear for test. Questions are shown one after another to user and user checks the right answer.

But I am facing the below issue.

Description: Checkbox does not uncheck for next question. It remains checked, once user click on any of the checkbox.

Steps: 1. Click on any checkbox options for the question. enter image description here 2. Click on next for next question. [checkbox is checked from previous question] [enter image description here]2

Expected: When next question appears, the checkbox should not be checked.

Actual: When next questions appears, the checkbox is checked.

Code: On click of next, this component gets its data as a props from parent component.

// This component show one question at a time
import React from 'react';
import TextEditorDisplay from '../../texteditor/TextEditorDisplay';
import Form from 'react-bootstrap/Form';

class TestComponent extends React.PureComponent {

    handleCheck = (e, idx) => {
        console.log('inside handleCheck',e.target.value)
        this.props.setAnswerGivenByUser(idx, e.target.checked);
    };

    render() {
        return (
            <div className="container">
                <h3 className="quiz-question">
                    <TextEditorDisplay editorContent={this.props.quizQuestion.question} />
                </h3>
                <Form>
                    <table>
                        <tbody>
                            {this.props.quizQuestion.options && this.props.quizQuestion.options.map((option, idx) => (
                                <tr key={idx}>
                                    <td>
                                        <Form.Group controlId="formBasicCheckbox">
                                            <Form.Check type="checkbox" value={option.data} onChange={e => this.handleCheck(e, idx)}/>
                                        </Form.Group>
                                    </td>
                                    <td>
                                        <p key={idx}>{option.data}</p>
                                    </td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                </Form>
            </div>
        );
    }
}

export default TestComponent;



How to display an adaptive result of a CSV file using checkboxes and select option

As you can view in the image here View Image , I am using a form which have check-boxes and select option. Upon import of my CSV files using PHP, it displays the data correctly with the column headers. It also displays the CHECKED-boxes with its selected option only.

my csv file:

year, Exports of goods(F.O.B), Domestic exports, Re-exports.

1, 5, 10,20

1, 5, 10, 20

1, 5, 10, 20

1, 5, 10, 20

However am having real problem in the displaying the selected option only once. Also if I do not choose the first column of my CSV file 'year' , it displays me 'no links'. Meaning it only works when column 1 is selected.

I just need a result which is adaptive. That is for example, if I checked on row 2 and 4 for EX. 'Export of goods' and 're-exports', I need it to display the checked data with its selected option. I can't figure out on how to make it adaptive.View Image

Can someone help me please. Thank you.

<body>
<?php
   $uploadfile = $_SESSION['uploadCsv'];
    $file = fopen($uploadfile,"r");
    $sum = array();
    while(!feof($file)) {
      $csv = fgetcsv($file,1024);
      array_push($sum,$csv[1]);
    }
    echo array_sum($sum)."\n sum of column 2";
?>
<?php
   $uploadfile = $_SESSION['uploadCsv'];
    $file = fopen($uploadfile,"r");
    $sum = array();
    while(!feof($file)) {
      $csv = fgetcsv($file,1024);
      array_push($sum,$csv[2]);
    }
    echo array_sum($sum)."\n sum of column 3";
?>

  
  <table border="0"  >

   

  <?php
      #read CSV file
      $uploadfile = $_SESSION['uploadCsv'];
      
      if (($handle = fopen($uploadfile, "r")) !== FALSE) {
        $mycsv = array();
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
        $mycsv[] = $data;
        fclose($handle);

        $row_length = count($mycsv);


      #Find sum of csv
      foreach($mycsv[1] as $col_num => $col)
      {
          

        echo "<tr>";
          
    
    
          for($x=0; $x<$row_length; $x++)
          {
              array_push($sum, $mycsv[$row][$key]);
          }
      
        }     

      #Find the length of the transposed row

      $row_length = count($mycsv);
      
      echo "<form method='post' action=''><table><tr>";
      echo "<th><input type='checkbox' onClick='toggle(this)' /> Select/Deselect All<br/>
      </th>";
      echo "<th>Column</th>";
      echo "<th>Filter</th>";


      echo "</tr>";

      #Loop through each row (or each line in the csv) and output all the columns for that row
      foreach($mycsv[0] as $col_num => $col)
      {
          

        echo "<tr>";
          
    
    
          for($x=0; $x<1; $x++)
          {
              
          

          echo "<td><input type='checkbox' name='lang[]'  value='".$mycsv[$x][$col_num]."' ></td>";
          echo "<td align='center'>"  .$mycsv[$x][$col_num]."</td>";
          echo "<td>
            <select name='method[]' >
             <option value=''>Choose</option>
              <option  value='sum'>Sum</option>
              <option  value='mean'>Mean</option>              
              <option  value='average'>Average</option>
              <option  value='highest'>Highest Value</option>
              <option  value='lowest'>Lowest Value</option>
            </select>
              </td>";
          echo "</tr>";
          }
          
      }
          echo "<tr>";
          echo "<td><input type='checkbox' name='lang[]' ></td>";
          echo "<td align='center'><input type='text' name='own_column' placeholder='Type your new column here'><br></td>";
          echo "<td><input type='text' name='own_column' placeholder='Own formula'><br></td>";
          echo "</tr>";

          //echo "<tr>";
          //echo "<td colspan='3' align='center'><input type='submit' name='Order'/></td>";
          //echo "</tr>";
          echo "<tr>";
          echo "<td colspan='3'><div class='wrapper'><br/><br/><input type='button' class='button' value='Input Button'>
          </div></td>";
          echo "</tr>
          <tr><td><input type='submit' value='submit' name='submit'</td></tr><table></form>";
            }

  ?>
  </tbody>


 <div>
 <?php
if (($handle = fopen("sample1.csv", "r")) !== FALSE) {
                   $mycsv = array();
                   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                       $mycsv[] = $data;
                   fclose($handle);
                 
                   $post_data  = $_POST["checklist"];
                   //$SelectedOption = $_POST['demo'];
                   for($i=0; $i < count($post_data); $i++)
                   {
                      echo "Selected : " . $post_data[$i]."</br>" ;
                   }
                     
                   $row_length = count($mycsv);
                   $col_length = count($post_data);
                   $flag = True;
                  }

$package = $_POST['method'];
if(isset($_POST['submit']))
{

   if(!empty($_POST['lang']))
    {

       foreach($_POST['lang'] as $key => $value)
        {
          echo "<tr><td>";
           echo " Selected value : ".$mycsv[0][$value].'<br/>';
           echo "</td></tr>";

           

              foreach($_POST['method'] as $method)
                {

                  
                    $method = $package[$key];
                    echo "<tr><td>";
                    if ($method == 'sum') 
                    {   
                      $file = fopen($uploadfile,"r");
                      $sum = array();
                      while(!feof($file)) {
                      $csv = fgetcsv($file,1024);
                      array_push($sum,$csv[$key]);           
                      }
                      echo "Sum of " . $value. " = ".array_sum($sum);
                    }
                    else if ($method == 'mean') 
                    {
                      echo "mean of " . $value;
                    }
                    else if ($method == 'average') 
                    {
                      echo "Averge of " . $value;
                    }
                    else if ($method == 'highest') 
                    {
                      echo "highest value of " . $value;
                    }else if ($method == 'lowest'){
                      echo "highest value of " . $value;
                    } else {
                    echo "no method chosen.";
                    }
                }   
                
        }
    } 

}


?>

View Image form here




Make label and input on same line

I would like to display, label and inputs on same line :

My HTML structure is like:

<div class="checkbox">
    <label for="fld_4293217_1_opt961337">
        <input type="checkbox">
                    Disabled                
    </label>
</div>

And it is coming on a page like:

Pictorial representation:

Any help on this?