如何在JSON中连接两个记录

时间:2015-04-16 09:00:57

标签: php html json autosuggest

以下代码对我有用,但它只给我一个自动完成的密钥。如何显示两个键并打印它们。在我的情况下,我打算在建议框中打印国家名称和费率。

任何帮助都将受到高度赞赏。

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;
?>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

或者可能是这种组合:

$json_arr["name"] = $recResult['CountryName'].' '.$recResult['SellingRate'];
$json_arr["value"] = $recResult['CountryName'].' '.$recResult['SellingRate'];
相关问题