PHP做一个数组列表匹配ID [英] php make an array list for matching id
本文介绍了PHP做一个数组列表匹配ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的表像这样
ID类
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
您可以看到我的类别编号由separted表| 。我有这些类别名称的另一个表。我想获得的所有类别,使他们的数组。到目前为止,我现在做这样的
< PHP
$服务器名=localhost的;
$用户名=根;
$密码=根;
$ DBNAME =清单;
$库MySQLi =新的mysqli($服务器名,用户名$,$密码,$ DBNAME);如果(mysqli_connect_errno()){
的printf(连接失败:%S \\ n,mysqli_connect_error());
出口();
}$ listing_array =阵列();
$ category_array =阵列();$查询=SELECT * FROM`listings` ORDER BY listing_id
$ category_array =阵列();
如果($结果= $ mysqli->查询($查询)){
而($ OBJ = $ result-> fetch_object()){
$类= $ obj->类;
}
的foreach($ category_array为$ category_names){
$测试=爆炸('|',$ category_names);
}
$ result->关闭();
}$ mysqli->关闭();
?>
这之后我怎么能得到的类别名称并使其成为数组的行?
解决方案
$查询=SELECT * FROM`listings` ORDER BY listing_id
如果($结果= $ mysqli->查询($查询))
{
而($ OBJ = $ result-> fetch_object())
{
$类=阵列();
$猫=爆炸(|,$ obj->类);
$ querycat =选择类别名称FROM`category` CATEGORY_ID的地方在'$猫。';
而($ objcat = $ resultcat - > fetch_object())
{
array_push($类,$ objcat - >类别名称);
}
}
}
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
<?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();
?>
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);
}
}
}
这篇关于PHP做一个数组列表匹配ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文