自动检测同一条目的号码并根据号码做一个行间隔? [英] Auto detect number of same entry and do a rowspan according to the number?

查看:76
本文介绍了自动检测同一条目的号码并根据号码做一个行间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



获得的结果看起来像这样。

  id / cart_id / title / price / quantity 
1/1 /book1/10.00/ 2
2/1 / book2 /19.00/ 1

在for循环中正常创建2个表格行以保存这2个结果。然而,我想要一个rowspan,并只显示cart_id1一次。



这是我当前的代码

< ($ i = 0; $ i $ a = $ history [code $< $ php
> $ I] [ 'cart_id'];
echo'< tr>';
if($ history [$ i] ['cart_id'] == $ history [$ i] ['cart_id']){
echo'< td rowspan =2>'。元。 '< / TD>';
}
echo'< td>'。$ history [$ i] ['title']。'< / td>';
echo'< td>'。$ history [$ i] ['price']。'< / td>';
echo'< td>'。$ history [$ i] ['quantity']。'< / td>';
echo'< td>'。$ history [$ i] ['cart_id']。'< / td>';
echo'< / tr>';
}
?>

编辑

 原始输出:
[id:1] [book1] [19.90] [2]
[id:1] [book2] [10.90] [1]

我想要的东西:id行将根据同一个ID的数量进行rowspan。因此显示ony 1id:1

[] [book1] [19.90] [2]
[id:1] [book2] [10.90] [1]

我想要吗?

解决方案 c 您可以使用额外的

 <?php 
$ lastcart = FALSE;
for($ i = 0; $ i $ a = $ history [$ i] ['cart_id'];
echo'< tr>';
$ rowspan = 0;
for $(
$ j = $ i;
$ history [$ i] ['cart_id']!== $ lastcart&&&
$ j< count( $ history)&&
$ history [$ j] ['cart_id'] == $ history [$ i] ['cart_id'];
$ j ++
){
$ rowspan ++;
}
if($ rowspan> 0){
$ lastcart = $ history [$ i] ['cart_id'];
echo'< td rowspan ={$ rowspan}>'。$ a。'< / td>';
}
echo'< td>'。$ history [$ i] ['title']。'< / td>';
echo'< td>'。$ history [$ i] ['price']。'< / td>';
echo'< td>'。$ history [$ i] ['quantity']。'< / td>';
echo'< td>'。$ history [$ i] ['cart_id']。'< / td>';
echo'< / tr>';
}
?>


i wanted to create a forloop which will create a table from the data obtained from the database.

the obtained result looks something like this.

id/cart_id/title/price/quantity
1 / 1     /book1/10.00/ 2
2 / 1     /book2/19.00/ 1

In the for loop its normal to create 2 table row to hold these 2 results. However i want one of the to rowspan and show only cart_id "1" once.

this is my current code

<?php
                for($i = 0; $i < count($history); $i++) {
                    $a = $history[$i]['cart_id'];
                    echo '<tr >';
                    if($history[$i]['cart_id'] == $history[$i]['cart_id']){                     
                    echo '<td rowspan="2">'.$a.'</td>';
                    }
                    echo '<td>'.$history[$i]['title'].'</td>';
                    echo '<td>'.$history[$i]['price'].'</td>';
                    echo '<td>'.$history[$i]['quantity'].'</td>';
                    echo '<td>'.$history[$i]['cart_id'].'</td>';
                    echo '</tr>';
                }
                ?>

edit

original output:
[id:1][book1][19.90][2]
[id:1][book2][10.90][1]

what i wanted: The id row will rowspan according to the number of same ID.Thus showing ony 1 "id:1"

[    ][book1][19.90][2]
[id:1][book2][10.90][1]

How is it possible to achieve what i want?

解决方案

You can achieve this with an extra for loop:

<?php
    $lastcart = FALSE ;
    for($i = 0; $i < count($history); $i++) {
        $a = $history[$i]['cart_id'];
        echo '<tr >';
        $rowspan = 0;
        for(
            $j=$i;
            $history[$i]['cart_id'] !== $lastcart &&
            $j < count($history) && 
            $history[$j]['cart_id']==$history[$i]['cart_id'];
            $j++
        ){
            $rowspan++ ;
        }
        if($rowspan > 0){
            $lastcart = $history[$i]['cart_id'] ;
            echo '<td rowspan="{$rowspan}">'.$a.'</td>';
        }
        echo '<td>'.$history[$i]['title'].'</td>';
        echo '<td>'.$history[$i]['price'].'</td>';
        echo '<td>'.$history[$i]['quantity'].'</td>';
        echo '<td>'.$history[$i]['cart_id'].'</td>';
        echo '</tr>';
    }
?>

这篇关于自动检测同一条目的号码并根据号码做一个行间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆