从数据库返回一条记录

时间:2016-05-17 21:55:42

标签: php oop return records

谢谢你的回答。现在它的工作,但我有下一个问题。 我重新构建了一个显示数据库记录的方法:

 public $id, $nazwa, $quantity, $data, $godzina, $sn, $added, $kategoria, $numrows;

    public function magazyn() {
        $resultmag = $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `id`");
        $resultmag = $this->conn->set_charset("utf8");
        $resultmag = $this->magazyn->execute();
        $resultmag = $this->magazyn->get_result();

        if ($isset = $resultmag->num_rows > 0) {
            while ($row = $resultmag->fetch_object()) {
            $this->id = $row->id;
            $this->nazwa = $row->nazwa;
            $this->quantity = $row->quantity;
            $this->data = $row->data;
            $this->godzina = $row->godzina;
            $this->sn = $row->sn;
            $this->added = $row->added;
            $this->kategoria = $row->kategoria;
            }
            $this->numrows = $resultmag->num_rows;
        } else {
            echo "No results database";
        }
    }

    function __toString() {
        return (string) $this->numrows;
    }

    //-------------------------------
    $mag = new Magazyn();
    $mag->magazyn();

    ?>
            <table class="table table-bordered">
                <tr><th style='text-align:center'>ID</th><th style='text-align:center'>DODAŁ</th><th style='text-align:center'>NAZWA</th><th style='text-align:center'>KATEGORIA</th><th style='text-align:center'>SERIAL</th><th style='text-align:center'>ILOŚĆ</th><th style='text-align:center'>DATA</th><th style='text-align:center'>GODZINA</th><th style='text-align:center' colspan='2'>AKCJA</th></tr>
                <?php
    $color = NULL;
    $color1 = '#99bbff';
    $color2 = '#b3ccff';

    $color == $color1 ? $color = $color2 : $color = $color1;
    echo "<tr class='active'>";
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->id . "</td>";
    echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=userpanel&user=" . $mag->added . "'>" . $mag->added . "</a></td>";
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->nazwa . "</td>";
    echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=category&cat=" . $mag->kategoria . "'>" . $mag->kategoria . "</a></td>";
    echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=editserial&sn=" . $mag->sn . "'>" . $mag->sn . "</a></td>";
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->quantity . "</td>";
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->data . "</td>";
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->godzina . "</td>";
    echo "<td align='center'><a href='panel.php?page=magazyn&action=edit&sn=" . $mag->sn . "&nazwa=" . $mag->nazwa . "&kategoria=" . $mag->kategoria . "&id=" . $mag->id . "'</a><button  class='btn btn-default'>Edytuj</button></td>";
    echo "<td align='center'><a href='panel.php?page=magazyn&action=delete&sn=" . $mag->sn . "'</a><button  class='btn btn-default'>Usuń</button></td>";
    echo "</tr>";

问题是它从数据库中显示 ONLY ONE RECORD 。 当我添加到echo时:

while ($row = $resultmag->fetch_array()) {
    echo $this->id = $row['id'];
    echo $this->nazwa = $row['nazwa'];
    echo $this->quantity = $row['quantity'];
    echo $this->data = $row['data'];
    echo $this->godzina = $row['godzina'];
    echo $this->sn = $row['sn'];
    echo $this->added = $row['added'];
    echo $this->kategoria = $row['kategoria'];
}

显示所有记录,但是:

$mag->magazyn();

enter image description here

1 个答案:

答案 0 :(得分:1)

正如Dagon上面的评论所说,将您的值添加到多维数组中。而不是

public class WorkoutListFragment extends ListFragment {
ArrayList<String> listItems = new ArrayList<String>(); //List items

ArrayAdapter<String> adapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.workout_list_layout, container, false);
    return rootView;
}

public void onListItemClick(ListView listView, View view, int position, long id){
    ViewGroup viewGroup = (ViewGroup) view;
    TextView textView =  (TextView) viewGroup.findViewById(R.id.textItem);
    Toast.makeText(getActivity(), textView.getText().toString(), Toast.LENGTH_SHORT).show();

}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" , "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(String workoutTitle, View v) {
    listItems.add(workoutTitle);
    adapter.notifyDataSetChanged();
}
}

只做

public $id, $nazwa, $quantity, $data, $godzina, $sn, $added, $kategoria, $numrows;

然后你要将所有记录追加到这个数组中:

public $records = array();
public $numrows;

然后在您的HTML中循环public function magazyn() { $resultmag = $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `id`"); $resultmag = $this->conn->set_charset("utf8"); $resultmag = $this->magazyn->execute(); $resultmag = $this->magazyn->get_result(); if ($isset = $resultmag->num_rows > 0) { while ($row = $resultmag->fetch_object()) { $record['id'] = $row->id; $record['nazwa'] = $row->nazwa; // etc... Add all like above $this->records[] = $record; } $this->numrows = $resultmag->num_rows; } else { echo "No results database"; } }

$mag->records