我有一个数据库,其结构如下:
<table border="2">
<tbody>
<tr>
<td>ID</td>
<td>numbering</td>
<td>country</td>
<td>operator</td>
<td>rate</td>
</tr>
<tr>
<td>1</td>
<td>12345</td>
<td>country1</td>
<td>operator1</td>
<td> </td>
</tr>
<tr>
<td>2</td>
<td>12345</td>
<td>country1</td>
<td>operator1</td>
<td> </td>
</tr>
<tr>
<td>3</td>
<td>12345</td>
<td>country1</td>
<td>operator1</td>
<td> </td>
</tr>
<tr>
<td>4</td>
<td>12345</td>
<td>country1</td>
<td>operator2</td>
<td> </td>
</tr>
<tr>
<td>5</td>
<td>12345</td>
<td>country1</td>
<td>operator2</td>
<td> </td>
</tr>
<tr>
<td>6</td>
<td>12345</td>
<td>country1</td>
<td>operator2</td>
<td> </td>
</tr>
</tbody>
</table>
我首先根据用户选择的国家查询运算符,并显示它们,但是我还想显示每个运算符的相应编号集。这是我到目前为止实现的代码:
<?php
$operator_rates= $wpdb->get_results("SELECT DISTINCT operator, rate FROM database WHERE country='$_GET[country]'");
foreach ( $operator_rates as $operator_rate ) {
$numbering = $wpdb->get_results("SELECT numbering FROM database");
echo '
<strong>'.$operator_rate->operator.'</strong><br/>'.$numbering;
} ?>
我得到的结果如下:
操作员1阵列
操作员2阵列
操作员3阵列
我期望的结果是每个运算符都有一个编号集,并且正好在它的下面是每个运算符的编号集:
操作员1
12345,12345,12345,12345
操作员2
12345,12345,12345,12345
操作员3
12345,12345,12345,12345
最佳答案
$numbering
是一个数组,因为您使用的是$wpdb->get_results
,所以您需要在这样的数组上使用join
。
foreach ($operator_rates as $operator_rate) {
$numbering = $wpdb->get_results("SELECT numbering FROM melitawordpress.prepaid_telephony_rates");
echo '
<strong>' . $operator_rate->operator . '</strong><br/>';
$numbers = array();
foreach ($numbering as $number) {
$numbers[] = $number->numbering;
}
echo join(", ", $numbers) . "<br />";
}
编辑:
$wpdb->get_results
返回对象。关于php - PHP/MySQL查询/循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27522560/