Reputation: 141
I have a database having the following structure:
<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>
I am first querying the operators based on the country selected by the user, and display them, however I also want to display the corresponding set of numbering for each operator. This is the code I implemented so far :
<?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;
} ?>
The Result I am getting is as per the below:
Operator 1
Array
Operator 2
Array
Operator 3
Array
The result I am expecting is having each operator and exactly underneath it the set of numbering for each operator:
Operator 1
12345, 12345, 12345, 12345
Operator 2
12345, 12345, 12345, 12345
Operator 3
12345, 12345, 12345, 12345
Upvotes: 2
Views: 123
Reputation: 664
change this line
$numbering = $wpdb->get_results("SELECT numbering FROM database", ARRAY_A);
and change this line
<strong>'.$operator_rate->operator.'</strong><br/>'.$numbering;
with this
<strong>'.$operator_rate->operator.'</strong><br/>'.implode(',', $numbering['numbering']);
Upvotes: 0
Reputation: 12391
$numbering
is an array, because you are using $wpdb->get_results
so you need to use join
on that like this.
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 />";
}
EDIT: $wpdb->get_results
returns with objects.
Upvotes: 1
Reputation: 14266
Use implode function, e.g
foreach ($operator_rates as $operator_rate) {
$numbering = $wpdb->get_results("SELECT numbering FROM melitawordpress.prepaid_telephony_rates");
echo '
<strong>' . $operator_rate->operator . '</strong><br/>';
echo implode(", ", $numbering) . "<br />";
}
Upvotes: 0
Reputation: 23978
Your query is returning multiple rows.
And array is getting printed, so its showing Array
.
You need to get formatted string by concatenating array elements with comma ,
.
Use group_contact()
IN
$numbering = $wpdb->get_results("SELECT GROUP_CONCAT(numbering SEPARATOR ', ') AS numbering
FROM melitawordpress.prepaid_telephony_rates");
Upvotes: 0