修复PHP的表输出

时间:2015-12-16 12:06:18

标签: php mysql

我正在尝试用PHP创建一个表。

我有价值的形式 - 市场,生产日期,销售日期,并将其发送到表格以便填充。

在POST中,我从MySQL的生产表中调用一组数据并填充在同一个表中。

我能够从SQL和Form POST Action填充表格,但我无法按照所需的输出格式对齐它们。

请帮助找到以下代码的修复程序:

<?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
}
?>

我能从这段代码中获得的当前输出是:

enter image description here

我想要的是:

enter image description here

更新的更改建议和工作:

<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
                                        {                   ?>

1 个答案:

答案 0 :(得分:1)

您必须将Category列rowspan设置为count($ invoices)* count($ pname)并将rowspan设置为Product,Production date和Sales date列为 count($ pname)