在foreach循环中对数据库值进行分组

时间:2018-09-17 11:14:26

标签: php mysql sql

我有一个成功的查询,正在建立一个选择列表,但它会将每个location_name值添加为带有相应display_name的标签,而不是只有一个带有相应显示的位置标签。

我的查询:

  <?php
  defined('BASEPATH') OR exit('No direct script access allowed');
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;
class Paypal extends MY_Controller {

public function __construct() {
    parent::__construct();
}

public function createPayment() {
    $apiContext = new \PayPal\Rest\ApiContext(
            new \PayPal\Auth\OAuthTokenCredential(
                'AUvPOPFRg002AdyvE-gbrELklxpyP-XDz2vsolVPdQ-aIp26fgkppiIc-O9ODzD1n29JUC4VpWbrmTVu',
                'ELhNYBFWOYiP_vtVHp8ibHtBjGiRRAODu6evmNWQPz3fQpmS2aGd34YiARLQLQ-Tjjk5z2NL8sMuuajc'
            )
    );

    $apiContext->setConfig(
          array(
            'log.LogEnabled' => true,
            'log.FileName' => 'PayPal.log',
            'log.LogLevel' => 'DEBUG',
          )
    );

    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $amount = new Amount();
    $amount->setCurrency('INR');
    $amount->setTotal(11.00);

    $transaction = new Transaction();
    $transaction->setAmount($amount);
    $transaction->setDescription("Payment description");

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl(base_url()."paypal/executePayment?success=true")
        ->setCancelUrl(base_url()."paypal/executePayment?success=false");
    // dd($redirectUrls);
    $payment = new Payment();
    $payment->setIntent('sale')
        ->setPayer($payer)
        ->setTransactions(array($transaction))
        ->setRedirectUrls($redirectUrls);

    // Create payment with valid API context
    try {
      $payment->create($apiContext);

      // Get PayPal redirect URL and redirect the customer
      $approvalUrl = $payment->getApprovalLink();
      // Redirect the customer to $approvalUrl
    } catch (PayPal\Exception\PayPalConnectionException $ex) {
     // echo $ex->getCode();
     // echo $ex->getData();

      die($ex);
    } catch (Exception $ex) {

      die($ex);
    }

}

返回

$getDisplayByLocation = "
SELECT l.id as locationID, location_name, d.id as displayID, display_name
from locations l
inner join displays d
    on l.id = d.location_id;
";

$displayLocation = $mysqlConn->query($getDisplayByLocation);

但是我的清单随建筑物而建

locationID  |  location_name  |  displayID  |  display_name 
    4             building4         1              Room One
    4             building4         2              Room Two
    1             building1         3              Room One
    1             building1         4              Room Two

代替

building4
->Room One
building4
->Room Two
building1
->Room One
building1
->Room Two

我的选择列表

building4
->Room One
->Room Two
building1
->Room One
->Room Two

0 个答案:

没有答案