JessicaGonzalez
JessicaGonzalez

Reputation: 31

php multidimensional array as radio buttons with ID and array value

I am php noob. I have searched Google very thoroughly past couple of days and can't figure that out.

I have multidimensional array I have to convert to radio buttons with unique ID and values, but I can't seem to do it.

The json array:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [available] => 1
                [courier] => 1
                [type] => 1
                [price] => 42.89
                [transitDays] => 3
            )

        [1] => Array
            (
                [available] => 1
                [courier] => 1
                [type] => 3
                [price] => 50.50
                [transitDays] => 4
            )

        [2] => Array
            (
                [available] => 0
            )

        ...

    )

[1] => Array
    (
        [0] => Array
            (
                [available] => 1
                [courier] => 2
                [type] => 1
                [price] => 111
                [transitDays] => 11
            )

        [1] => Array
            (
                [available] => 0
                [courier] => 2
                [type] => 4
                [price] => 22
                [transitDays] => 22
            )

        ...
    )
)

I need to make every output some of the values of every ['available']==1 array into radio buttons and on select be able to retrieve the data after form submission.

<p class="row"><input type="radio" id="option-<?php echo $i ?> value="service-<?php echo $i ?>" name="type" "> <?php echo $service['type']; ?> will cost <?php echo $service['price']; ?> and take <?php echo $service['days']; ?></p>

I have tried flattening arrays and spew available results, but I can't assign unique ID's then. I tried

  foreach ($providers as $provider) {
  $mergeProvider = array_merge($provider);
  foreach ($provider as $services){
    $service = array_merge($services);
      if( $service['available'] == 0 ) { unset($service); }
      $serviceCount = count($service);
      else {
          include('offer.php'); //where is input type="button"
      }

but this does not allow me unique ID's.

If I do:

foreach ($providers as $provider) {
  $mergeProvider = array_merge($provider);
  foreach ($provider as $services){
    $service = array_merge($services);
        $serviceCount = count($services);
        for( $i = 1; $i < $serviceCount; $i++ ) {
        echo "<pre>";
        echo $serviceCount . "</pre>";

it spews out $serviceCount amount of different options where same option has different ID within it.

What can I do?

Upvotes: 0

Views: 661

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

As an answer to the question in your comment:

You mean how to map the service-10 back to the array? You then you need a way to get the '10' from the string 'service-10'. But that can go wrong when the numbers become greater than 10. For example 110 (1 and 10). So I've added another example of how to could do this. I've updated the code with a pipe to separate the $key and the $subkey: $uniqueKey = $key . '|' . $subKey;

I've also added a var_dump so you can see the mapped data that it matches.

// for example, this is your index.php

<html>
<head></head>
<body>
<form id="theForm" name="theForm" method="POST" action="submit.php">
    <?php
    $items = array(
        0 => array(
            0 => array(
                "available" => 1,
                "courier" => 1,
                "type" => 1,
                "price" => 42.89,
                "transitDays" => 3
            ),
            1 => array(
                "available" => 1,
                "courier" => 1,
                "type" => 3,
                "price" => 50.50,
                "transitDays" => 4
            ),
        ),
        1 => array(
            0 => array(
                "available" => 1,
                "courier" => 2,
                "type" => 1,
                "price" => 111,
                "transitDays" => 11
            ),
            1 => array(
                "available" => 0,
                "courier" => 2,
                "type" => 4,
                "price" => 22,
                "transitDays" => 22
            ),
        )
    );

    foreach($items as $key => $item) {
        foreach($item as $subKey => $subItem) {
            if ($subItem["available"] === 1) {
                $uniqueKey = $key . '|' . $subKey;
                echo sprintf(
                    '<p class="row"><input type="radio" id="option-%1$s" value="service-%1$s" name="type">%2$s will cost %3$s and take %4$s</p>',
                    $uniqueKey,
                    $subItem["type"],
                    $subItem["price"],
                    $subItem["transitDays"]
                );
            }
        }
    }
    ?>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

For example this is your submit.php

<?php
$items = array(
    0 => array(
        0 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 1,
            "price" => 42.89,
            "transitDays" => 3
        ),
        1 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 3,
            "price" => 50.50,
            "transitDays" => 4
        ),
    ),
    1 => array(
        0 => array(
            "available" => 1,
            "courier" => 2,
            "type" => 1,
            "price" => 111,
            "transitDays" => 11
        ),
        1 => array(
            "available" => 0,
            "courier" => 2,
            "type" => 4,
            "price" => 22,
            "transitDays" => 22
        ),
    )
);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['type'])) {
        $type = $_POST['type'];
        $positionDash = strpos($type, '-');
        $positionPipe = strpos($type, '|');
        if (false !== $positionDash && false !== $positionPipe) {
            $tail = substr($type, $positionDash+1);
            $tree = explode('|', $tail);
            $mappedData = $items[$tree[0]][$tree[1]];
            var_dump($mappedData);
        }
    }
}

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163207

Maybe you can create a unique key based on the keys of the foreach loops.

Then when you post the form, the name field will contain a unique value like service-00, service-01, service-10

For example:

$items = array(
    0 => array(
        0 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 1,
            "price" => 42.89,
            "transitDays" => 3
        ),
        1 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 3,
            "price" => 50.50,
            "transitDays" => 4
        ),
    ),
    1 => array(
        0 => array(
            "available" => 1,
            "courier" => 2,
            "type" => 1,
            "price" => 111,
            "transitDays" => 11
        ),
        1 => array(
            "available" => 0,
            "courier" => 2,
            "type" => 4,
            "price" => 22,
            "transitDays" => 22
        ),
    )
);

// then loop through the $items and create unique key

foreach($items as $key => $item) {
    foreach($item as $subKey => $subItem) {
        if ($subItem["available"] === 1) {
            $uniqueKey = $key . $subKey;
            echo sprintf(
                '<p class="row"><input type="radio" id="option-%1$s" value="service-%1$s" name="type">%2$s will cost %3$s and take %4$s</p>',
                $uniqueKey,
                $subItem["type"],
                $subItem["price"],
                $subItem["transitDays"]
            );
        }
    }
}

Upvotes: 0

Related Questions