我有这样的表
id categories
1 80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106
2 15|85|49|22|6
你可以看到我在表格中有类别ID 。我有一个具有这些类别名称的另一个表。我想获得所有类别并使它们成为一个数组。到目前为止,我确实喜欢这个
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "listings";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$listing_array = array();
$category_array = array();
$query = "SELECT * FROM `listings` ORDER by listing_id";
$category_array = array();
if ($result = $mysqli->query($query)) {
while ($obj = $result->fetch_object()) {
$categories = $obj->categories;
}
foreach( $category_array as $category_names ) {
$test = explode('|', $category_names);
}
$result->close();
}
$mysqli->close();
?>
在此之后如何获取类别名称并使它们成为行的数组?
答案 0 :(得分:0)
$query = "SELECT * FROM `listings` ORDER by listing_id";
if ($result = $mysqli->query($query))
{
while ($obj = $result->fetch_object())
{
$categories =array();
$cat = explode("|",$obj->categories);
$querycat = "SELECT CategoryName FROM `category` where category_id in '".$cat."'";
while($objcat = $resultcat ->fetch_object())
{
array_push($categories,$objcat ->CategoryName);
}
}
}