1

バックグラウンド

さて、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 );
?>
4

2 に答える 2

1

行: $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';

結果 "$message[0]" 配列の最初の要素から 1 つのオブジェクトのみを取得します。これが、1 つのオブジェクトのみが返される理由です。

編集:変更する必要があります:

$message=array();
while ($row = mysql_fetch_assoc($result)) {
    $message[]= ('name'    => $row['agency_name'],
                 'account' => $row['account_number'],
                 'phone'   => $message[]=$row['phone']
                 );
    }
print json_encode($message);  

これにより、jsで解析できるjsonオブジェクトが返されます

次に、あなたのjsで:

編集:それがやりたい場合は、htmlをjsに追加します

var html; 
$.each(data, function(info) {
   html += "<p><pre><code>id:"+ data_id +".<br>Agency Name: "+ info.name +"<br>Account Number:"+ info.number +"<br>Phone:"+ info.phone +"</code></pre></p><br><br>";
});

$('.view_modal_content').html(html);

?????????????????????????????????????????????????????? ??????????????????

あなたはやろうとしていますか:

var divObj = {}; //make sure this is in the correct scope
//anonymous array
$.each(data, function(info) {
   divObj.push("<p><pre><code>id:"+ data_id +".<br>Agency Name: "+ info.name +"<br>Account Number:"+ info.number +"<br>Phone:"+ info.phone +"</code></pre></p><br><br>");
});
//or associative array
$.each(data, function(info) {
   divObj[info.name] ="<p><pre><code>id:"+ data_id +".<br>Agency Name: "+ info.name +"<br>Account Number:"+ info.number +"<br>Phone:"+ info.phone +"</code></pre></p><br><br>";
});

または、div 内の既存のコード ブロックの要素を単純に更新しようとしていますか?

于 2013-04-25T22:40:01.643 に答える