从foreach打印数据

时间:2017-04-06 22:37:02

标签: codeigniter foreach

我有这样的清单:

foreach ($productos as $p) { 
  echo '<tr>'; 
  echo '<td>' . $p->codigoProducto . '</td>'; 
  echo '<td>' . $p->categoriaNombre . '</td>'; 
  echo '<td>' . $p->marcaNombre . '</td>'; 
  echo '<td>' . $p->detalleProducto . '</td>'; 
  echo '<td>' . $p->precioProducto . '</td>'; 
  echo '</tr>'; 
}  

我需要打印:

  

分类A

     

Código|马卡| Detalle | PRECIO
  1
  2
  3

  

分类B

     

Código|马卡| Detalle | PRECIO
  1
  2
  3
  ...

只显示categoriaNombre然后显示产品,但使用foreach我不知道如何显示它。

1 个答案:

答案 0 :(得分:0)

第一个循环是类别标题的循环,在你的情况下,它是分类A或分类B.制作一个查询以区分类别标题。这是查询示例

package com.example.rahul.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    Button one, two, three, four, five, six, seven, eight, nine, zero, plus,      minus, multiply, division, equal, dot, clear;
    TextView textViewresult = null;
    double val1, val2;

    boolean add, sub, div, mul;
    boolean equalcheck = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        one = (Button) findViewById(R.id.btn_1);
        two = (Button) findViewById(R.id.btn_2);
        three = (Button) findViewById(R.id.btn_3);
        four = (Button) findViewById(R.id.btn_4);
        five = (Button) findViewById(R.id.btn_5);
        six = (Button) findViewById(R.id.btn_6);
        seven = (Button) findViewById(R.id.btn_7);
        eight = (Button) findViewById(R.id.btn_8);
        nine = (Button) findViewById(R.id.btn_9);
        zero = (Button) findViewById(R.id.btn_0);
        plus = (Button) findViewById(R.id.btn_plus);
        minus = (Button) findViewById(R.id.btn_minus);
        multiply = (Button) findViewById(R.id.btn_multiply);
        division = (Button) findViewById(R.id.btn_divide);
        equal = (Button) findViewById(R.id.btn_equal);
        textViewresult = (TextView) findViewById(R.id.tv_result);
        dot = (Button) findViewById(R.id.btn_dot);
        clear = (Button) findViewById(R.id.btn_clear);


        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                } else {
                    textViewresult.setText(textViewresult.getText() + "1");
                }
            }
        });


        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                } else

                    textViewresult.setText(textViewresult.getText() + "2");
            }
        });


        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "3");
            }
        });


        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "4");
            }
        });


        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "5");
            }
        });


        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "6");
            }
        });


        seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "7");
            }
        });


        eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "8");

            }
        });


        nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + "8");
            }
        });

        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //textViewresult.setText("");
                textViewresult.setText(textViewresult.getText() + "0");
            }
        });


        dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (equalcheck == true) {
                    textViewresult.setText("");
                    equalcheck = false;
                }
                textViewresult.setText(textViewresult.getText() + ".");
            }
        });

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (textViewresult.getText() == "") {

                    textViewresult.setText("");
                } else {
                    val1 = Double.parseDouble(textViewresult.getText() + "");
                    add = true;

                    textViewresult.setText(null);
                }
            }
        });

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (textViewresult.getText() == "") {

                    textViewresult.setText("");
                } else {
                    val1 = Double.parseDouble(textViewresult.getText() + "");
                    sub = true;
                    textViewresult.setText(null);
                }
            }
        });

        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (textViewresult.getText() == "") {

                    textViewresult.setText("");
                } else {
                    val1 = Double.parseDouble(textViewresult.getText() + "");
                    mul = true;
                    textViewresult.setText(null);
                }
            }
        });
        division.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (textViewresult.getText() == "") {

                    textViewresult.setText("");
                } else {
                    val1 = Double.parseDouble(textViewresult.getText() + "");
                    div = true;
                    textViewresult.setText(null);
                }
            }
        });

        equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (textViewresult.getText() == "") {

                    textViewresult.setText("");
                } else {

                    equalcheck = true;
                    val2 = Double.parseDouble(textViewresult.getText() + "");
                    if (add == true) {


                        textViewresult.setText(val1 + val2 + "");
                        add = false;

                    }
                    if (sub == true) {
                        textViewresult.setText(val1 - val2 + "");
                        sub = false;
                    }
                    if (mul == true) {
                        textViewresult.setText(val1 * val2 + "");
                        mul = false;
                    }
                    if (div == true) {
                        textViewresult.setText(val1 / val2 + "");
                        div = false;
                    }

                }

            }

        });
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewresult.setText("");
            }
        });
    }
}

 

这就是我做到的。因为我做的和你做的一样。我做的是动态的。如果类别只有2个类别会更容易,你可以通过在数据库中添加新字段来设置静态类别确定它链接到哪个项目。