我正在开发一个针对uni的小型网络应用程序,其中一个要求是它必须有一个列表,其中包含来自数据库的数据,可以通过单击某个类别进行排序,然后只需单击该项目中的项目。类别将显示。 我已设法将数据库中的数据检索到网站上的表格中,但是,下拉菜单选择您要查看的类别不起作用。这意味着它显示数据库中的所有项目,无论如何。我没有收到任何错误,真的不知道如何解决这个问题。 希望有人能够帮助我。 该函数的代码应该只显示该类别的项目:
//Get ItemEntity objects from the database and return them in an array.
function GetItemByCategory($category) {
// require 'dbconnect.php';
//Open connection and Select database.
$con = mysqli_connect("localhost", "root", "", "login") or die(mysqli_error);
$query = "SELECT * FROM items WHERE category LIKE '$category'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$itemArray = array();
//Get data from database.
while ($row = mysqli_fetch_array($result)) {
$name = $row[1];
$category = $row[2];
$place = $row[3];
$date = $row[4];
$description = $row[5];
$image = $row[6];
//Create item objects and store them in an array.
$item = new ItemEntity(-1, $name, $category, $place, $date, $description, $image);
array_push($itemArray, $item);
}
//Close connection and return result
mysqli_close($con);
return $itemArray;
}
}
我还是很陌生,所以它可能是一个简单的修复,但就像我说的那样,我迷失了,所以这就是我伸出手的原因。 在此先感谢您,如果您需要我的更多代码,请告诉我。
//编辑 显示数据的HTML表单。如果需要其他任何东西,请告诉我。谢谢你的帮助。
function CreateItemTables($categories)
{
$itemModel = new ItemModel();
$itemArray = $itemModel->GetItemByCategory($categories);
$result = "";
// Generate an item table for each item entity in the array
foreach ($itemArray as $key => $item) {
$result = $result . "<table class = 'itemTable'>
<tr>
<th rowspan='6' width = '150px' ><img runat = 'server' src = '$item->image'/></th>
<th width = '75px' >Name: </th>
<td>$item->name</td>
</tr>
<tr>
<th>Category: </th>
<td>$item->category</td>
</tr>
<tr>
<th>Place: </th>
<td>$item->place</td>
</tr>
<tr>
<th>Date: </th>
<td>$item->date</td>
</tr>
<tr>
<td colspan='2' >$item->description</td>
</tr>
</table>";
}
return $result;
}
//编辑2整个ItemController类
<?php
require ("Model/ItemModel.php");
/**
* Contains non-db related functions for the Item page
*/
class ItemController
{
function CreateItemDropdownList()
{
$itemModel = new ItemModel();
$result = "<form action = '' method = 'post' width = '200px'>
Please select a category: <select name = 'types' >
<option value = '%' >ALL</option>".$this->CreateOptionValues($itemModel->GetCategories())."
</select>
<input type = 'submit' value = 'Search' /></form>";
return $result;
}
function CreateOptionValues(array $valueArray)
{
$result = "";
foreach ($valueArray as $value)
{
$result = $result . "<option value ='$value'>$value</option>";
}
return $result;
}
function CreateItemTables($categories)
{
$itemModel = new ItemModel();
$itemArray = $itemModel->GetItemByCategory($categories);
$result = "";
// Generate an item table for each item entity in the array
foreach ($itemArray as $key => $item) {
$result = $result . "<table class = 'itemTable'>
<tr>
<th rowspan='6' width = '150px' ><img runat = 'server' src = '$item->image'/></th>
<th width = '75px' >Name: </th>
<td>$item->name</td>
</tr>
<tr>
<th>Category: </th>
<td>$item->category</td>
</tr>
<tr>
<th>Place: </th>
<td>$item->place</td>
</tr>
<tr>
<th>Date: </th>
<td>$item->date</td>
</tr>
<tr>
<td colspan='2' >$item->description</td>
</tr>
</table>";
}
return $result;
}
}
?>
答案 0 :(得分:0)
//changed like to = more accurate
$query = "SELECT * FROM items WHERE category = '$category'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$itemArray = array();
//Get data from database.
//note this
$i=0;
while ($row = mysqli_fetch_array($result)) {
//gave then a record number so you get the right data
$name = $row[$i];
$category = $row[$i];
$place = $row[$i];
$date = $row[$i];
$description = $row[$i];
$image = $row[$i];
$item = new ItemEntity($i, $name, $category, $place, $date, $description, $image);
$i++;