如何在同一个表中打印相同的类别数据,在foreach循环中打印不同表中的不同数据

时间:2016-02-18 06:03:12

标签: php mysql foreach

如果cate_id不同则cat_id对于数据相同,我想在同一个表中打印所有数据组创建新表并打印该类别数据 Plz帮帮我

stdClass Object
(
    [id] => 5
    [cat_id] => 5
    [title] => Canara Bank Recruitment 2016
)
stdClass Object
(
    [id] => 4
    [cat_id] => 4
    [title] => Canara Bank Recruitment 2016
)
stdClass Object
(
    [id] => 3
    [cat_id] => 3
    [title] => Canara Bank Recruitment 2016
)
stdClass Object
(
    [id] => 8
    [cat_id] => 3
    [title] => Canara Bank Recruitment 2016  
)
$current_cat = 0;
foreach ($post as $key => $value)
                {
                        if($current_cat ==0)
                        {   
                            $current_cat = $value->cat_id;
                            echo '<div class="panel panel-default" style="width: 75%;margin: auto;">  
                                        <div class="panel-heading">'.$current_cat.'</div>  
                                            <table class="table table-bordered table-striped"> 
                                                <thead> 
                                                    <tr> 
                                                        <th>Title</th> 
                                                    </tr> 
                                                </thead> 
                                                <tbody>';
                        }

                        if($current_cat !=0 && $current_cat!= $value->cat_id)
                        {   
                            echo '</tbody> 
                                    </table> 
                                </div>';
                        }

                        echo '<div class="panel panel-default" style="width: 75%;margin: auto;">  
                                        <div class="panel-heading">'.$current_cat.'</div>  
                                            <table class="table table-bordered table-striped"> 
                                                <thead> 
                                                    <tr> 
                                                        <th>Title</th> 
                                                    </tr> 
                                                </thead> 
                                                <tbody>';
                        $current_cat = $value->cat_id;
                        echo '<tr>

                                    <td>'.$value->title.'</td>
                                </tr>';
                    }
                    if($current_cat !=0)
                    {   
                        echo '</tbody> 
                                </table> 
                            </div>';
                    }

我的结果有很多数据,但我这里没有提到。 Qt::Key enum

1 个答案:

答案 0 :(得分:0)

  1. 按cat_id查询您的数据排序:public class PaySclip extends Activity { HashMap<String, String> hashMap; String name="",bank,id; ArrayList<Login_Vo> login_VosArrayList; SqllietDatabaseConnection sqllietDatabaseConnection; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fragment_payslip); login_VosArrayList=new ArrayList<Login_Vo>(); sqllietDatabaseConnection=new SqllietDatabaseConnection(PaySclip.this); Login_Master_Vo login_Master_Vo=new Login_Master_Vo(); ArrayList<Login_Master_Vo> login = sqllietDatabaseConnection.getAll_LoginMaster(); if(login!= null && login.size() !=0) { for(int i=0;i<=login.size();i++){ Login_Vo login_Vo=new Login_Vo(); login_Vo.setEMPLOYEE_ID(login.get(i).toString()); login_VosArrayList.add(login_Vo); Log.v("LoginDe====>",""+login_VosArrayList); } }
  2. 在php中声明$ cat varible并将其值设置为空字符串(order by cat_id)。这将保存cat_id字段以确定您是否需要新表。
  3. 开始循环查询结果的php循环。
  4. 如果$ cat为空,则启动一个新表并将$ cat设置为当前记录的cat_id。如果它不为空,但$ cat不等于当前记录的cat_id,则结束当前表,启动一个新表,将$ cat设置为当前记录的cat_id。
  5. 无论前一点是什么结果,都要将当前记录打印到表格中。
  6. Php循环结束。
  7. 如果$ cat不为空,请结束表。
  8. 更新

    1. 你不会在任何地方回复$ end
    2. 循环体中有一个无法回应,为每条记录创建表头。该echo应该是前一个if()的一部分,如果当前记录的类别与存储的类别匹配,则启动一个新表。