【问题标题】:How to concatenate two records in JSON如何连接JSON中的两条记录
【发布时间】:2015-06-22 13:54:25
【问题描述】:

下面的代码对我有用,但它只给了我一个自动完成键。如何显示两个键并打印它们。就我而言,我打算在建议框中打印 Country Name and Rates。

我们将不胜感激任何帮助。

rates.php

<?php
include "includes/header.php";
?>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jqueryui/jquery-ui.min.js"></script>

<script type="text/javascript">

    $(function () {

        $("#dd_user_input").autocomplete({
            source: "rates_config.php",
            minLength: 2,
            select: function (event, ui) {
                var getUrl = ui.item.id;
                if (getUrl != '#') {
                    location.href = getUrl;
                }
            },

            html: true,

            open: function (event, ui) {
                $(".ui-autocomplete").css("z-index", 1000);
            }
        });

    });
</script>
<div class="domain-prices">
    <div class="row">
        <div class="small-12 columns">
            <h2>ForFreeCalls offers cheap calling or texting to any mobile or landline phone in the world. What country
                would you like to call?</h2>

            <form>
                <input id="dd_user_input" class="search_form" onblur="if(this.value=='')this.value=this.defaultValue;"
                       onfocus="if(this.value==this.defaultValue)this.value='';" placeholder="Enter a Country Name...."
                       type="text">
            </form>
        </div>

    </div>
</div>

<?php
?>

rates_config.php

<?php
/*====================== Database Connection Code Start Here ======================= */

define ("DB_HOST", "xxxxxxxx"); // set database host
define ("DB_USER", "xxxxxxxx"); // set database user
define ("DB_PASS", "xxxxxxxx"); // set database password
define ("DB_NAME", "xxxxxxxx"); // set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

/*====================== Database Connection Code End Here ========================== */

// Here, we will get user input data and trim it, if any space in that user input data
$user_input = trim($_REQUEST['term']);

// Define two array, one is to store output data and other is for display
$display_json = array();
$json_arr = array();


$user_input = preg_replace('/s+/', ' ', $user_input);

$query = 'SELECT vr.CountryName,vr.SellingRate FROM voip_routes as vr WHERE vr.CountryName LIKE "%' . $user_input . '%"';

$recSql = mysql_query($query, $link);
if (mysql_num_rows($recSql) > 0) {
    while ($recResult = mysql_fetch_assoc($recSql)) {
        $json_arr["name"] = $recResult['CountryName'];
        $json_arr["value"] = $recResult['SellingRate'];
        array_push($display_json, $json_arr);
    }
} else {
    // $json_arr["Id"] = "#";
    $json_arr["name"] = "";
    $json_arr["value"] = "No Result Found !";
    array_push($display_json, $json_arr);
}

// json_encode($recSql);


// json_encode($display_json);
$jsonWrite = json_encode($display_json); //encode that search data
print $jsonWrite;
?>

【问题讨论】:

    标签: php html json autosuggest


    【解决方案1】:

    您可以通过
    $json_arr["name"] = $recResult['CountryName']." ".$recResult['SellingRate'];

    【讨论】:

      【解决方案2】:

      您可能正在寻找这样的东西:

      $json_arr["name"] = $recResult['CountryName'].' '.$recResult['SellingRate'];
      

      或者可能是这样的组合:

      $json_arr["name"] = $recResult['CountryName'].' '.$recResult['SellingRate'];
      $json_arr["value"] = $recResult['CountryName'].' '.$recResult['SellingRate'];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-06
        • 2023-03-12
        • 1970-01-01
        • 2021-11-25
        • 2017-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多