gpt4 book ai didi

PHP/MySQL 查询/循环

转载 作者:行者123 更新时间:2023-11-29 02:58:27 25 4
gpt4 key购买 nike

我有一个具有以下结构的数据库:

<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
数组

我期望的结果是每个运算符都在其下方,每个运算符的编号集如下:

运算符(operator) 1
12345, 12345, 12345, 12345

运算符(operator) 2
12345, 12345, 12345, 12345

运算符(operator) 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/MySQL 查询/循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27522560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com