1

I am getting an output in array:

    array (size=3)
  8 => string 'Mexico' (length=6)
  24 => string 'UAE' (length=3)
  34 => string 'India' (length=5)

    array (size=9)
  2 => string '' (length=0)
  8 => string 'Large' (length=5)
  14 => string '' (length=0)
  19 => string '' (length=0)
  23 => string '' (length=0)
  24 => string 'Micro' (length=5)
  34 => string 'Large' (length=5)
  35 => string '' (length=0)
  38 => string '' (length=0)

I want the output in the following format: Which means need to compare the id of both.

Mexico - Large (8)
UAE - Micro (24)
India - Large (34)

PHP SRIPT

<?php
  $entities = $check_list = [];

  foreach($_POST as $k => $v) {
    if(preg_match("/^check_list(\d+)$/", $k, $matches))
      $check_list[intval($matches[0])] = $v;
    unset($matches);
    if(preg_match("/^entity_selected(\d+)$/", $k, $matches))
      $entities[intval($matches[0])] = $v;
  };
            var_dump($_POST['check_list']);

            var_dump($_POST['entity_selected']);

            echo implode(" ",$_POST['entity_selected'])."<br>";

            echo implode(" ",$_POST['check_list'])."<br>";
?>

I need to compare the variable and insert the data accordingly to the DB. But i am not able to make this data into variables. How can I do that?

This is my PHP Form script:

<form method="POST" action="nextpage1.php">
  <table border="1">
    <?php
      $i = 0;
      while($row1 = mysqli_fetch_array($result_country))  {
        $country1 = $row1['country'];
        $entity1 = $row1['entity'];
        echo "<tr>";
        ?>
          <td>
            <input name="check_list[<?php echo $i; ?>]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
          </td>
          <td>
            <?php
              if($entity1=="Yes") {
                ?>
                  <select class="form-control selectpicker" name="entity_selected[<?php echo $i; ?>]">
                                      <option value="">Select</option>

                    <option value="Micro">Micro</option>
                    <option value="Small">Small</option>
                    <option value="Large">Large</option>
                  </select>
                <?php
              };
            ?>
          </td>
        <?php
        $i++;
        echo "</tr>";
      };
    ?>
  </table>

enter image description here

Basically the form script consists of some countries with checkboxes and a drop down to select the entities. So which ever check box is selected that value (country name) and the entity has to be captured and inserted to the DB.

0

1 Answer 1

1

You are over complicating it. Just loop through $_POST['check_list'] and use its key to find the associated value in $_POST['entity_selected']:

foreach ($_POST['check_list'] as $key => $country) {
    echo $country . ' - ' . $_POST['entity_selected'][$key] . ' (' . $key . ')'. "\n";
}

This assumes there is always a corresponding key in $_POST['entity_selected'] as in $_POST['check_list'].

If you want to make the output a little bit easier on the eyes you can also use printf() to help with the formatting:

foreach ($_POST['check_list'] as $key => $country) {
    printf('%s - $s (%u)%s', $country, $_POST['entity_selected'][$key], $key, "\n");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Here's my personal take for making the code nice-looking. I'd change $key to $id, then $size = $_POST['entity_selected'][$id] ?? 'size unknown'; then echo "{$country} - {$size} ({$id})";
Thank you. @ John. I have updated the question with the form script also from where i am generating this array.
iam getting an error while running the first script. Parse error: syntax error, unexpected '$_POST' (T_VARIABLE), expecting ',' or ';'
And also i need to show the entity name also here. Its not showing that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.