json配列にmysqlレコードを作成していますが、jsonデータセットに問題があるようです。 PFBコード: -JSONデータセット構造
JSON出力が来る: - :私は代わりに、各レコードに来る、これらは来るべき[]これらのブラケットを意味
[{
"ID": "1",
"Coupon_ID": "ABC1",
"Campaign_ID": "353",
"Campaign_Name": "Test1",
"Title": "Test1",
"Description": "Test1",
"Category": "Test1",
"Type": "Coupon",
"Type_Value": "1231",
"Tracking_URL": "http://abc1.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}
{
"ID": "2",
"Coupon_ID": "ABC2",
"Campaign_ID": "3532",
"Campaign_Name": "Test2",
"Title": "Test2",
"Description": "Test2",
"Category": "Test2",
"Type": "Coupon",
"Type_Value": "1232",
"Tracking_URL": "http://abc2.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}]
-
[{
"ID": "1",
"Coupon_ID": "ABC1",
"Campaign_ID": "353",
"Campaign_Name": "Test1",
"Title": "Test1",
"Description": "Test1",
"Category": "Test1",
"Type": "Coupon",
"Type_Value": "1231",
"Tracking_URL": "http://abc1.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}][
{
"ID": "2",
"Coupon_ID": "ABC2",
"Campaign_ID": "3532",
"Campaign_Name": "Test2",
"Title": "Test2",
"Description": "Test2",
"Category": "Test2",
"Type": "Coupon",
"Type_Value": "1232",
"Tracking_URL": "http://abc2.com",
"Added_Date": "2016-07-13",
"Expiry_Date": "2016-07-28"
}]
私はこのような何かをしたいです1回だけ。
PHPコード: - あなたが初期化するために/あなたのJSON配列エコーしばらく外に必要
<?php
header('Content-type: application/json');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "couponsdeals";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$apikey = $_GET['API_KEY'];
$sql = "SELECT AFF_ID FROM api WHERE API_KEY= '$apikey' ";
//$sql1= "SELECT ID,Coupon_ID,Campaign_ID,Campaign_Name,Title,Description,Category,Type,Type_Value,Tracking_URL,Added_Date,Expiry_Date FROM couponcode_table ";
$sql1 = "SELECT * FROM couponcode_table ";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$data = $row["AFF_ID"];
if ($result1) {
while ($row1 = $result1->fetch_assoc()) {
$data1 = $row1["Tracking_URL"];
$mydata = explode("&", $data1);
if (isset($mydata[2])) {
$json1 = array();
$mydata2 = $mydata[0] . "&aff_id=" . $data . "&" . $mydata[2];
$values = array(
'ID' => $row1['ID'],
'Coupon_ID' => $row1['Coupon_ID'],
'Campaign_ID' => $row1['Campaign_ID'],
'Campaign_Name' => $row1['Campaign_Name'],
'Title' => $row1['Title'],
'Description' => $row1['Description'],
'Category' => $row1['Category'],
'Type' => $row1['Type'],
'Type_Value' => $row1['Type_Value'],
'Tracking_URL' => $mydata2,
'Added_Date' => $row1['Added_Date'],
'Expiry_Date' => $row1['Expiry_Date']
);
array_push($json1, $values);
echo json_encode($json1, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} else {
$json = array();
$mydata1 = $mydata[0] . "&aff_id=" . $data;
$value = array(
'ID' => $row1['ID'],
'Coupon_ID' => $row1['Coupon_ID'],
'Campaign_ID' => $row1['Campaign_ID'],
'Campaign_Name' => $row1['Campaign_Name'],
'Title' => $row1['Title'],
'Description' => $row1['Description'],
'Category' => $row1['Category'],
'Type' => $row1['Type'],
'Type_Value' => $row1['Type_Value'],
'Tracking_URL' => $mydata1,
'Added_Date' => $row1['Added_Date'],
'Expiry_Date' => $row1['Expiry_Date']
);
array_push($json, $value);
echo json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
}
}
}
} elseif (!$result) {
echo "0 results";
}
$conn->close();
?>
は、ループ内でjson_encodeを使用しないでください示しています。代わりに、配列を一時配列$ resultArray [] = array(...)に格納します。ループの後、echo json_encode($ resultArray)を実行します。 – vishwakarma09
$ json1 = array();を初期化します。外側ループ –