0

いくつかのデータを含むデータベースがありますが、もちろんそれらは一意です。これらのデータは、クラスに応じて、各テーブルtelcoCalltelcoDatatelcoSMS内にあります。

次に、json_encodeこれらのデータを 1 つの配列にマージするために使用します。telcoCall内では、データは適切な場所にあります。ただし、telcoDatatelcoSMSは厄介です。これらのテーブル内のデータは複製されています。こう見えて、

ここに画像の説明を入力

コードは次のとおりです。

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

なぜこれが起こっているのか本当にわかりません。

4

2 に答える 2

1

そうです、スコットは正しかったです。そのスニペットは氷山の一角にすぎませんでした。そのため、彼が提案した冗長なエントリを削除して、一意のエントリを残すようにしました。 https://gist.github.com/jwara/fdc805240cf03027ae20 -- コードはきれいではありませんが、最適化の対象です

http://cl.ly/image/2I3v2O29341J/o

于 2013-03-12T18:04:18.260 に答える
0

telcoId は telco テーブルでは一次固有であるように見えますが、他のテーブルでは複数になる可能性があります (これは理にかなっています)。 'd create 1 * 2 * 2 * 3 = 12 行で、乱雑な multiple2 と multiple3 を説明しています。

そのため、単一の DB 呼び出しを行う代わりに、個別のクエリを実行し、最初に電話会社の情報を取得してから、telcoCall、telcoData、telcoSMS で電話会社ごとに個別の DB 呼び出しを行うことで、効率が低下します。

または、速度だけが必要で、きれいなコードを気にしない場合は、現在持っているものをそのまま使用できますが、if 条件を積み重ねることができます。

EDIT 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...
}
于 2013-03-12T05:15:19.887 に答える