バックグラウンド
さて、mysql データベースの行に関する簡単な情報を表示するページがあります。たとえば、表には 7 つの列がありますが、ページには # の 1、3、および 4 のみが表示されます。行の右側には、モーダル ウィンドウを開くための href リンクがあり、html/css/etc を使用して適切にフォーマットされたウィンドウにすべての行を表示しようとしています...
これに関するチュートリアルで約 4 時間を費やした後、私が思いついた最も近いものは、「id」を json スクリプトに渡すジョブを実行する以下のコード ベースであり、(ほぼ) 適切に情報を取得し、単一のオブジェクト行を作成し、それを html/php ページに戻して、モーダル ウィンドウに読み込ませます。
コードベースを変換して、すべてのオブジェクトを個別に転送し、HTML にエコーする (arse-backwards) 1 行の巨大な php の代わりに、丁寧に助けを求めています。私は JSON/AJAX/jquery にかなり慣れていませんが、これを理解できれば、html/css/php をナビゲートして操作することができます。
コード例が少しこもっていたり、構文が貧弱だったりする場合は、申し訳ありません。
AJAX を含む PHP ページ
$('.view_information').click(function(e) { // Button which will activate our modal
//On Clicking the function, dynamically load the data for the viewing
var data_id = $(this).data('id');
$.ajax({
url: 'view_agency_info.php',
type: 'POST',
data: {id: data_id},
dataType: 'json',
success: function(data){
$('.view_modal_content').html(data.html); // LOAD THE DATA INTO THIS DIV
//I want to be able to use...
//$('.placeholder_name')xxxx
//$('.placeholder_accountnumber')etc
},
error: function(jqXHR, textStatus, errorThrown){
$('.view_modal_content').html('');
alert('Error Loading Information');
}
});
PHPページからわかるように、現時点では、mysqlは配列に取り込まれ、単一のHTMLオブジェクトに個別に挿入され、PHPに戻されて出力されます. このコードを変換して複数のオブジェクトを出力するにはどうすればよいですか?
JSON
<?php
$customer_id=$_SESSION['customer']['customer_id'];
$id = (int)$_POST['id'];
$query = "SELECT * FROM collections_list WHERE id={$id} && customer_id=$customer_id LIMIT 1"; //expecting one row
$result = mysql_query( $query );
//$message = mysql_fetch_assoc( $result ); //expecting just one row
$message=array();
while ($row = mysql_fetch_assoc($result)) {
$message[]=$row['agency_name'];
$message[]=$row['account_number'];
$message[]=$row['phone'];
}
$json = array();
$json['html'] = '<p><pre><code>id:'.$id.'.<br>Agency Name: '.$message[0].'<br>Account Number:'.$message[1]."<br>Phone:".$message[2].'</code></pre></p>'.'<br><br>test';
header('Content-Type: application/json');
echo json_encode( $json );
?>