1
    <?php
?>
<html>
    <head>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }
            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }
            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>
                    </th>
                    <?php
for($i = 1; $i <=31;$i++){
    echo '<th>'.$i.'</th>';
}
                    ?>
                </tr>
            </thead>
            <tbody>
                <td>
                    Item A
                </td>
                <?php 
$qty_n_day = '1/2,3/6';
$qty_day = explode(',',  $qty_n_day);
foreach ($qty_day as $qd) {
    list($qty,$day) = explode('/', $qd);
    for($i = 1; $i <=31;$i++){
        if($day == $i)
            echo '<td>'.$qty.'</td>';
        else
            echo '<td>-</td>';
    }
}
                ?>
            </tbody>
        </table>
    </body>
</html>

Output result enter image description here My expected result enter image description here

  1. The 31 column indicate as days.
  2. I stored quantity and days together, and then extract it after into a list.
  3. After that, i want to compare it with day column and show qty value for the column.

How can i do that? Is my logic wrong?

3
  • 1
    Your foreach is looping over two items, in there you're looping over 31 days. So you're generating 62 <td>s. Commented Dec 28, 2017 at 14:03
  • Design wise, I would consider maybe loading this into an associative array. Then you can display the chart with the array and using key=>value pairs and you can also easily manipulate the data. Using a for loop to print the data works fine but a foreach loop would probably be more straightforward. Also your use of strings to pair quantity with day could also be solved with something such as an associative array, which is ultimately what you get when you explode the data but still this is sort of confusing and hard to grasp from a glance. Assoc. array is designed for tasks like these. Commented Dec 28, 2017 at 14:04
  • Ikr. How can i insert the value correctly according to day? Commented Dec 28, 2017 at 14:05

3 Answers 3

3

Try this way, creating an associative array first with the day and value:

<?php 
    $qty_n_day = '1/2,3/6';
    $qty_day = explode(',',  $qty_n_day);

    $days = [];
    foreach ($qty_day as $day) {
        if (($res = explode('/', $day))) {
            $days[$res[1]] = $res[0];
        }
    }
    /*
    the array should stay like this
    $days = [
        2 => 1,
        6 => 3
    ];
    */

    for($i = 1; $i<=31;$i++){ 
        if (isset($days[$i])) { // if the key exists set the value
            echo '<td>' . $days[$i] . '</td>';
        } else {
            echo '<td>-</td>';
        }
    }

?>
Sign up to request clarification or add additional context in comments.

Comments

1

You have to change the order of your loops: Your foreach loop loops through the quantities and contains the for loop, that loops through the days. This leads to the behavior, that the for loop runs completely through for each quantity, therefore echoing 31 days. This means that for 2 quantities 62 days are printed.

You need to flip the loops and add a conditional output to them:

for ($i = 1; $i <= 31; $i++) {
    $quantity = '-';
    foreach ($qty_day as $qd) {
        list($qty,$day) = explode('/', $qd);
        if ($day == $i) {
            $quantity = $qty;
            break;
        }
    }
    echo '<td>' . $quantity . '</td>';
}

Comments

1

The problem arises from the fact that you are performing two iterations, the first one processing 2 cycles and the second one processing 31 cycles... for a total of 62 elements being generated.

I propose you a much more compact solution, that builds up the final array first and then simply prints it:

<?php 

    $arr = array_fill(1, 31, "-");

    $qty_n_day = '1/2,3/6';
    $qty_day = explode(',',  $qty_n_day);

    foreach ($qty_day as $qd)
    {
        list($qty,$day) = explode('/', $qd);
        $arr[$day] = $qty;
    }

    for ($i = 1; $i <= 31; ++$i)
    {
        echo '<td>'.$arr[$i].'</td>';
    }

?>

Comments

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.