将数组的内容拆分为不同的行

时间:2018-05-22 23:11:31

标签: php for-loop

我正在尝试让一个数组的每个元素在一个新行上开始,同时允许其他内容在同一行上。

//arrays of plannames
$planname_num=array(1=>"Alpha","Omega","Gamma");

//arrays of planlevels
$planlevels_num=array(1=>"Bronze","Silver","Gold");
/* 
 * 
 * Get The Length of Plan Name Array
 * 
 * 
 */
$planname_array_length=count($planname_num);
/* 
 * 
 * Get The Length of Plan Level Array
 * 
 * 
 */
$planlevel_array_length=count($planlevels_num);

$pnArrayLength=$planname_array_length;
$plArrayLength=$planlevel_array_length;

for ($pn=1; $pn <= $pnArrayLength; $pn++) {//loop to create plan name indicators 
            for ($pl=1; $pl <= $plArrayLength; $pl++) {//loop to create plan level indicators 


             //Create pricing for each plan from db
                $getplansql = "SELECT SUM(`planPrice`) AS planPrice,planDescription FROM `plans`
                        WHERE planName = '$planname_num[$pn]' AND planLevel = '$planlevels_num[$pl]'";
                $resultplans = $conn->query($getplansql);

                while($plan = mysqli_fetch_assoc($resultplans)) {$i++;// output data of each row
                         //calculate sales price
                         $finalpricing = ($plan['planPrice'] * 0.7) + ($plan['planPrice']);

                //create plan card
                $resultplanarray.= '

                        <div class="col-lg-2">
                         <div class="panel price panel-red">
        <div class="panel-heading text-center">
                        <h3>'.$planname_num[$pn].'</h3>
                                                <h3>'.$planlevels_num[$pl].'</h3>
                        </div>
                        <div class="panel-body text-center">
                            <p class="lead panel-body-pricing" style=""><strong>$'.round($finalpricing).'/month</strong></p>
                        </div>
                        <ul class="list-group list-group-flush text-center">
                            <li class="list-group-item"><i class="icon-ok text-danger"></i>
                                                       '.$plan['planDescription'].'
                                                        </li>
                                                        </ul>
                        <div class="panel-footer">
                        <a class="btn btn-lg btn-block btn-danger" href="#">BUY</a>
                        </div>
                                                </div>
                                                </div>';

}}}


//print result outside of while loop
echo $resultplanarray;

此代码将输出如下,直到12的网格填满:

Alpha  Alpha   Alpha Omega  Omega   Omega 
Bronze Silver  Gold  Bronze Silver  Gold

我希望数组能够拆分并打印不同的计划组,每个计划组都在这样一个新行上:

Alpha  Alpha   Alpha 
Bronze Silver  Gold 

Omega  Omega   Omega 
Bronze Silver  Gold 

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

为所有计划名称添加外部容器

$planname_num = array(1=>"Alpha","Omega","Gamma");
$planlevels_num = array(1=>"Bronze","Silver","Gold");
$planname_array_length = count($planname_num);
$planlevel_array_length = count($planlevels_num);

$resultplanarray = '';
for($i = 1; $i <= $planname_array_length ; $i++)
{
    $resultplanarray .= '<div class="row">';
    for($j = 1; $j <= $planlevel_array_length ; $j++)
    {
        //YOUR QUERY DATA
    }
    $resultplanarray .= '</div>';
}

我添加了<div class='row'>

如果您有任何问题,请检查并告诉我