php给留言分配id_PHP做一个数组列表匹配ID

I have my table like this

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

You can see I have categories id in the table separted by | . I have an another table with those categories name. I want to get all the categories and make them an array. So far now I did like this

$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();

?>

After this how can I get the categories name and make them an array for the rows?

解决方案$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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值