Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php foreach结果分为两列_Php_Html_Mysql - Fatal编程技术网

Php foreach结果分为两列

Php foreach结果分为两列,php,html,mysql,Php,Html,Mysql,亲爱的专家: 我想将我的foreach分为两列,我的脚本是: <?php $items = $wpo_wcpdf->get_order_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?> <tr> <td class="description"> <?php echo $item['name'];

亲爱的专家:

我想将我的foreach分为两列,我的脚本是:

<?php $items = $wpo_wcpdf->get_order_items();
   if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?>
      <tr>
        <td class="description">
            <?php echo $item['name']; ?><?php echo $item['meta']; ?>
            <?php echo $item['quantity']; ?>
        </td>
      </tr>
<?php endforeach; endif; ?>

(例如结果:ANDREW 10)


我想将结果分成两列(第1列:ANDREW 10、第2列、SITO 11?

您将它们排列为行,而不是列

解决方案:
标记放在foreach循环之外

更新:

<style>
    .items {
        -webkit-column-count: 2; /* Chrome, Safari, Opera */
        -moz-column-count: 2; /* Firefox */
        column-count: 2;
        -webkit-column-gap: 40px; /* Chrome, Safari, Opera */
        -moz-column-gap: 40px; /* Firefox */
        column-gap: 40px;
    }
    .description{
        display: block;
    }
</style>
<div class="items">
<?php 
//$items = $wpo_wcpdf->get_order_items();
$items = array(array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""));
   if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?>
    <span class="description">
      <?php echo $item['name']; ?><?php echo $item['meta']; ?>
      <?php echo $item['quantity']; ?>
    </span>
<?php endforeach; endif; ?>
</div>
之后,将foreach更改为for循环并创建两个
(列)。然后在元素索引和第一列中的元素数量之间进行比较(使用
if

if ($i < 10) {
  // put in the first column
} else {
  // put in the second column
}
示例: 假设在第一列中有10个元素

if ($i < 10) {
  // put in the first column
} else {
  // put in the second column
}
if($i<10){
//放在第一栏
}否则{
//放在第二栏
}
,其中,
$i
是for循环的索引。

更改为

<?php $items = $wpo_wcpdf->get_order_items();
   if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?>
      <tr>
        <td class="description">
            <?php echo $item['name']; ?><?php echo $item['meta']; ?>
            <?php echo $item['quantity']; ?>
        </td>
      </tr>
<?php endforeach; endif; ?>


您可以使用列计数,您可以根据需要更改样式,然后更改描述类。您可以设置边框。这两列的间距由items属性
列间距:40px;
确定,您也可以更改

试试这个, CSS:

<style>
    .items {
        -webkit-column-count: 2; /* Chrome, Safari, Opera */
        -moz-column-count: 2; /* Firefox */
        column-count: 2;
        -webkit-column-gap: 40px; /* Chrome, Safari, Opera */
        -moz-column-gap: 40px; /* Firefox */
        column-gap: 40px;
    }
    .description{
        display: block;
    }
</style>
<div class="items">
<?php 
//$items = $wpo_wcpdf->get_order_items();
$items = array(array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""));
   if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?>
    <span class="description">
      <?php echo $item['name']; ?><?php echo $item['meta']; ?>
      <?php echo $item['quantity']; ?>
    </span>
<?php endforeach; endif; ?>
</div>

.项目{
-webkit列数:2;/*Chrome、Safari、Opera*/
-moz列计数:2;/*Firefox*/
列数:2;
-webkit列间距:40px;/*Chrome、Safari、Opera*/
-moz列间距:40px;/*Firefox*/
柱间距:40px;
}
.说明{
显示:块;
}
PHP:

<style>
    .items {
        -webkit-column-count: 2; /* Chrome, Safari, Opera */
        -moz-column-count: 2; /* Firefox */
        column-count: 2;
        -webkit-column-gap: 40px; /* Chrome, Safari, Opera */
        -moz-column-gap: 40px; /* Firefox */
        column-gap: 40px;
    }
    .description{
        display: block;
    }
</style>
<div class="items">
<?php 
//$items = $wpo_wcpdf->get_order_items();
$items = array(array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""), array("name" => "andrew", "quantity" => "10", "meta" => ""));
   if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?>
    <span class="description">
      <?php echo $item['name']; ?><?php echo $item['meta']; ?>
      <?php echo $item['quantity']; ?>
    </span>
<?php endforeach; endif; ?>
</div>


我需要这个:andrew 10 siti 10 rine 13 johan 13 fredy 14 anto 13 hendro 19 andi 14(两列),但我的脚本结果:andrew 10 siti 10 rine 13 johan 13 fredy 14 anto 13 hendro 19(一列)第一列中有多少个元素?这个数字是常量吗?请提供
$items
。[原始数组]更改后的结果:我希望这样:检查并让我知道..Frayne Konok那样他将在第一列中获得第一个,在第二列中获得第二个,但第三个将在第一列中。他希望在第一列中放置元素(1,n),然后(n+1,m)在第二栏中,@dhruvjadia,为什么在这里提到我?