0

I am trying to make a table in PHP.

There is form where I am taking in Value - Market, Production Date, Sales Date and sending it in POST to Table to get it populated.

At the POST I am calling a set of data from production table from MySQL and populating in the same table.

I am able to populate the table from SQL and Form POST Action but I am not able to align them as per the required output format.

Please help in finding fix for the below Code:

<?php
if(isset($_POST['for_post']))
{
try
    {
        ?>            
        <div class="card-box" style="padding-left:5px;padding:10px;padding-bottom:50px">
            <div class="row">
                <div class="col-sm-12">
                <?php
                if(isset($_POST['for_post_market'])){ $pname = $_POST['for_post_market']; } 
                if(isset($_POST['for_post_prod_date'])){ $pcat = $_POST['for_post_prod_date']; } 
                if(isset($_POST['for_post_sale_date'])){ $pprice = $_POST['for_post_sale_date']; } 
                $query = 'SELECT * FROM product';
                    $stmt = $DB_con->prepare($query);
                    $stmt->execute();
                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    $result[$row['prod_cat_name']][] = $row['prod_name'];
                                                }
                ?>
                <table id="invoices" border="1">
                    <thead>
                        <th>Category</th>
                        <th>Product</th>
                        <th>Production Date</th>
                        <th>Sales Date</th>
                        <th>Market</th>
                    </thead>
                    <tbody>
                <?php
                    foreach($result as $id => $invoices) {
                    echo '<tr>';
                    echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
                        $count = 0;
                            foreach ($invoices as $invoice) {
                                if ($count != 0) {
                            echo '<tr>';
                                    }
                            echo "<td>$invoice</td>";
                            echo "<td>".$pcat."</td>";
                            echo "<td>".$pprice."</td>";

                $count++;
                                                            }
                                                        }

                $a=count($pname);

                for($i=0;$i<$a;$i++)
                    {
                    echo "<td>".$pname[$i]."</td></tr>";
                    }

                    echo "</tbody>";
                echo "</table>";
    }
catch(PDOException $e)
    {
        echo $e->getMessage();
    }

}
else
{                   ?>
            </div>
            </div>
        </div>
<div class="card-box" style="padding-left:5px;padding:10px;padding-bottom:50px">
<div class="row">
<div class="col-sm-12">
    <form class="form-inline" method="post">
    <div class="form-group m-r-10">
        <label for="exampleInputName2">Market : </label>
        <div class="input-group">
        <select class="selectpicker" multiple data-selected-text-format="count" data-style="btn-white" name="for_post_market[]">
                <?php
                    $stmt = $DB_con->prepare('Select * from location');
                    $stmt->execute();
                        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                        echo '<option>'.$row['location_name'].'</option>';
                                                                     }
                ?>
        </select>
        <div>
    &nbsp
    <div class="form-group m-r-10">
        <label for="exampleInputEmail2">Production Date : </label>
        <div class="input-group">
                    <input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose" name="for_post_prod_date">
                    <span class="input-group-addon bg-custom b-0 text-white"><i class="icon-calender"></i></span>
                </div>
    </div>
    <div class="form-group m-r-10">
        <label for="exampleInputEmail2">Sales Date : </label>
        <div class="input-group">
                    <input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker2-autoclose" name="for_post_sale_date">
                    <span class="input-group-addon bg-custom b-0 text-white"><i class="icon-calender"></i></span>
                </div>
    </div>
    <button type="submit" class="btn btn-default waves-effect waves-light btn-md" id="for_post" name="for_post">
        Submit
    </button>
</form> 
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>

The Current Output that I am able to get from this code is :

enter image description here

What I am trying to get is :

enter image description here

Updated Changes Suggested and worked:

<table id="invoices" border="1">
                                                                <thead>
                                                                    <th>Category</th>
                                                                    <th>Product</th>
                                                                    <th>Production Date</th>
                                                                    <th>Sales Date</th>
                                                                    <th>Market</th>
                                                                    <th>Input</th>
                                                                </thead>
                                                                <tbody>
                                                            <?php
                                                                $a=count($pname);
                                                                foreach($result as $id => $invoices) {
                                                                echo '<tr>';
                                                                echo '<td rowspan='. count($invoices)*$a . '>' . $id . '</td>';
                                                                    $count = 0;
                                                                        foreach ($invoices as $invoice) {
                                                                            if ($count != 0) {
                                                                        echo '<tr>';
                                                                                }
                                                                        echo '<td rowspan='. count($pname) . '>' . $invoice . '</td>';
                                                                        echo '<td rowspan='. count($pname) . '>' . $pcat . '</td>';
                                                                        echo '<td rowspan='. count($pname) . '>' . $pprice . '</td>';
                                                                        for($i=0;$i<$a;$i++)
                                                                            {
                                                                        echo '<td>'. $pname[$i] . '</td>';
                                                                        echo '<td><input type="text" class="form-control"></td></tr>';
                                                                            }
                                                                            $count++;
                                                                                                        }
                                                                                                    }
                                                                        echo '</tbody>';
                                                                        echo '</table>';


                                                }
                                            catch(PDOException $e)
                                                {
                                                    echo $e->getMessage();
                                                }

                                        }
                                        else
                                        {                   ?>
2
  • 2
    Please format your code and keep the example concise. Commented Dec 16, 2015 at 12:21
  • You will have to be clever with this output - always output the correct amount of cells, counting the row spans, etc... You should start by simply listing the cities inside a <td>, maybe start comma-seperated, after that, try to split it into a separate table inside that cell etc... This seems like something you have to take slower. You are trying to build complex from the get go, why not try to build basics and then make it more complicated? Also, as a sidenote, have a look at PHP's shorthand foreach: endforeach; etc... Commented Dec 16, 2015 at 12:26

1 Answer 1

1

You have to set Category column rowspan to count($invoices)*count($pname) and set rowspan to Product, Production date and Sales date columns to count($pname)

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

1 Comment

Worked Well :) Got me to the right direction. Thank you so much

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.