我有一个包含多个数据的数据库,但它们当然是独一无二的。这些数据位于每个表格中, telcoCall , telcoData , telcoSMS ,具体取决于其类别。
然后我使用json_encode
将这些数据合并为1个单个数组。在 telcoCall 内,数据处于适当的位置。但是, telcoData 和 telcoSMS 是混乱的。这些表中的数据正在重复。这是它的外观,
以下是代码:
<?PHP
include '../initialization.php';
$mysqli = @mysqli_connect($host, $username, $password, $db);
$query = 'SELECT t.*, c.*, d.*, s.* '.
'FROM telco t '.
'INNER JOIN telcoCall c ON t.telcoId = c.telcoId '.
'INNER JOIN telcoData d ON t.telcoId = d.telcoId '.
'INNER JOIN telcoSMS s ON t.telcoId = s.telcoId '.
'ORDER BY t.telcoName, c.callName, d.dataName, s.smsName';
//setup array to hold information
$telcos = array();
//setup holders for the different types so that we can filter out the data
$telcoId = 0;
$callId = 0;
$dataId = 0;
$smsId = 0;
//setup to hold our current index
$telcoIndex = -1;
$callIndex = -1;
$dataIndex = -1;
$smsIndex = -1;
if ($result = mysqli_query($mysqli, $query)) {
//go through the rows
while($row = mysqli_fetch_assoc($result)) {
if($telcoId != $row['telcoId']) {
$telcoIndex++;
$callIndex = -1;
$dataIndex = -1;
$smsIndex = -1;
$telcoId = $row['telcoId'];
//add the console
$telcos[$telcoIndex]['Telco'] = $row['telcoName'];
//setup the information array
$telcos[$telcoIndex]['Call Promo'] = array();
$telcos[$telcoIndex]['Data Promo'] = array();
$telcos[$telcoIndex]['SMS Promo'] = array();
}
if($callId != $row['callId']) {
$callIndex++;
$callId = $row['callId'];
//add the model to the console
$telcos[$telcoIndex]['Call Promo'][$callIndex]['Call Name'] = $row['callName'];
//setup the title array
$telcos[$telcoIndex]['Call Promo'][$callIndex]['Call'] = array();
//add the game to the current console and model
$telcos[$telcoIndex]['Call Promo'][$callIndex]['Call'][] = array(
'Keyword' => $row['callKeyword'],
'Description' => $row['callDescription'],
'Number' => $row['callNumber'],
'Validity' => $row['callValidity'],
'Price' => $row['callPrice']
);
}
if($dataId != $row['dataId']) {
$dataIndex++;
$dataId = $row['dataId'];
//add the model to the console
$telcos[$telcoIndex]['Data Promo'][$dataIndex]['Data Name'] = $row['dataName'];
//setup the title array
$telcos[$telcoIndex]['Data Promo'][$dataIndex]['Data'] = array();
//add the game to the current console and model
$telcos[$telcoIndex]['Data Promo'][$dataIndex]['Data'][] = array(
'Keyword' => $row['dataKeyword'],
'Description' => $row['dataDescription'],
'Number' => $row['dataNumber'],
'Validity' => $row['dataValidity'],
'Volume' => $row['dataVolume'],
'Price' => $row['dataPrice']
);
}
if($smsId != $row['smsId']) {
$smsIndex++;
$smsId = $row['smsId'];
//add the model to the console
$telcos[$telcoIndex]['SMS Promo'][$smsIndex]['SMS Name'] = $row['smsName'];
//setup the title array
$telcos[$telcoIndex]['SMS Promo'][$smsIndex]['SMS'] = array();
//add the game to the current console and model
$telcos[$telcoIndex]['SMS Promo'][$smsIndex]['SMS'][] = array(
'Keyword' => $row['smsKeyword'],
'Description' => $row['smsDescription'],
'Number' => $row['smsNumber'],
'Validity' => $row['smsValidity'],
'Price' => $row['smsPrice']
);
}
}
mysqli_free_result($result);
}
echo json_encode($telcos);
mysqli_close($mysqli);
?>
我真的不知道为什么会这样。
答案 0 :(得分:1)
答案 1 :(得分:0)
它看起来telcoId在telco表中是唯一的,但在其他表中可以是多个(这是有意义的),不幸的是,如果你尝试将single(1)连接到multiple1(2)到multiple2(2)到multiple3(3) ),它创建1 * 2 * 2 * 3 = 12行,这解释了凌乱的multiple2和multiple3。
因此,通过运行单独的查询,首先获取电信公司信息,然后在telcoCall,telcoData,telcoSMS上为每个电信公司进行单独的数据库调用,您将不会进行单个数据库调用。
或者如果你只是想要速度并且不关心干净的代码,你可以立即使用你现有的东西,但是叠加if条件:
编辑1:编辑修复逻辑漏洞,出于某种原因,脑海中出现了高斯消除
// Stacking order determined by your SQL ORDER BY: t.telcoName, c.callName, d.dataName, s.smsName
if($telcoId == $row['telcoId']) continue;
//telco stuff...
if($callId == $row['callId']) continue;
//call stuff...
if($dataId == $row['dataId']) continue;
//data stuff...
if($smsId == $row['smsId']) continue;
//sms stuff...
编辑2:另一种需要较少脑力劳动的较慢方式
if($telcoId != $row['telcoId']) {
//telco stuff...
callArrayTemp = array();
dataArrayTemp = array();
smsArrayTemp = array();
}
if(!in_array($row['callId'], callArrayTemp[])) {
callArrayTemp[] = $row['callId'];
//call stuff...
}
if(!in_array($row['dataId'], dataArrayTemp[])) {
dataArrayTemp[] = $row['dataId'];
//data stuff...
}
if(!in_array($row['smsId'], smsArrayTemp[])) {
smsArrayTemp[] = $row['smsId'];
//sms stuff...
}