我想要什么
我希望数组看起来像这个
array
(
[0]=>country
(
[id] =>
[name] =>
[url] =>
)
[city] => Array
(
[0] => Array
(
[id] => 68870
[name] => Abu Dhabi
[url] => abudhabi-ae
)
[1] => Array
(
[id] => 68872
[name] => Das Island
[url] => dasisland-ae
)
[2] => Array
(
[id] => 68873
[name] => Dubai
[url] => dubai-ae
)
[1]=>country
(
[id] =>
[name] =>
[url] =>
)
[city] => Array
(
[0] => Array
(
[id] => 68870
[name] => Abu Dhabi
[url] => abudhabi-ae
)
[1] => Array
(
[id] => 68872
[name] => Das Island
[url] => dasisland-ae
)
[2] => Array
(
[id] => 68873
[name] => Dubai
[url] => dubai-ae
)
)
我写了这样的代码
<?php
require('/home/indiamart/public_html/serve-biztradeshows-com/db.php');
$query="select distinct ct.name name , listing_combo.country country from country ct join listing_combo on ct.id=listing_combo.country ";
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{
$country_id= $row['country'];
$arr = array();
$arr[]=$row;
$arr1['country']=json_encode($arr);
$select_city="select id,name,url from city where country like'$country_id' ";
$city=mysql_query($select_city);
while($row1=mysql_fetch_assoc($city))
{
$arr[]=$row1;
}
$arr1['city']=json_encode($arr);
}
print_r($arr1);
echo "</br>";
?>
但它只打印查询的最后结果 和ites格式是这样的
数组也可以创建,所以我可以最后编码它,但它的格式应该没问题。
阵列(
[国家] =&GT;
[城市] =&GT;
)
请帮助
答案 0 :(得分:0)
是。这是因为您总是在循环中重新初始化$arr = array();
。将此行放在while
循环之外。
另外请不要使用mysql函数,因为它们已被弃用。请改用mysqli或PDO。
避免sql注入,转义查询中的变量。
我无法测试,但你需要这样的东西:
require('/home/indiamart/public_html/serve-biztradeshows-com/db.php');
//In your db.php create a $conn variable, what is a connection (resource) to
//your database with mysqli_connect function.
//See mysqli in php manual.
$countryQuery = "SELECT DISTINCT ct.name name , listing_combo.country country "
. "FROM country ct "
. "JOIN listing_combo ON ct.id=listing_combo.country";
//Use mysqli instead of mysql functions since they are deprecated.
$result = mysqli_query($conn, $countryQuery);
$countries = array();
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
//Do not need to create a new variable, what you do not use anywhere else.
//$country_id= $row['country']; You can use $row['country']
//in your query cityQuery later.
//$arr1['country']=json_encode($arr);
//Json do not need here, since, you want an array, not a string.
$countries[$i]['country'] = $row;
//Avoid sql injection, so use mysqli_real_escape_string
$cityQuery = "SELECT id,name,url "
. "FROM city "
. "WHERE country like'" . mysqli_real_escape_string($conn, $row['country']) . "'";
$cityResult = mysqli_query($conn, $cityQuery);
$j = 0;
while ($cityRow = mysql_fetch_assoc($cityResult)) {
//Add all cities to the current country
$countries[$i]['country']['city'][$j] = $cityRow;
//Incrase the key of the array cities
$j++;
}
//Incrase the key of countries
$i++;
}
print_r($countries);
我在你的代码中做了一些修改。重命名所有变量,告诉它们包括什么。
我正在使用mysqli函数而不是mysql函数。
删除未使用的变量。
添加了评论,以便您可以看到我在做什么。
答案 1 :(得分:0)
我无法连接到您的数据库,所以我无法测试。但感觉应该是:
<?php
require('/home/indiamart/public_html/serve-biztradeshows-com/db.php');
$query="select distinct ct.name name , listing_combo.country country from country ct join listing_combo on ct.id=listing_combo.country ";
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{
$countries[$row['country']]=$row;
}
foreach ($countries as $country_id => $country_row) {
$select_city="select id,name,url from city where country like'$country_id' ";
$city=mysql_query($select_city);
while($row1=mysql_fetch_assoc($city))
{
$cities[]=$row1;
}
foreach ($cities as $city_id => $city_row) {
$countries[$country_id][$city_id]=$city_row;
}
}
print_r($countries);
echo "</br>";
?>
您最好先查询国家/地区以避免重置数据。感谢这位优秀的评论者。