1

背景

好的,我有一个页面,它显示来自 mysql 数据库的行的简单信息。例如,表格有 7 列,但页面上只显示 # 的 1、3 和 4。在该行的右侧是打开模式窗口的 href 链接,我正在尝试在带有 html/css/etc 的格式良好的窗口中显示所有行...

在这方面花了大约 4 个小时的教程之后,我想出的最接近的东西是下面的代码库,它完成了将“id”传递给 json 脚本的工作,(几乎)正确地提取信息,将其放入单个对象行,并将其传递回我的 html/php 页面以读入模式窗口。

我礼貌地寻求帮助以转换我的代码库,以便它单独传输所有对象,而不是一行巨大的 php 以回显到 html 中(arse-backwards)。我对 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

If I understand this correctly, then I would advise you to change the PHP to generate JSON directly rather than sending back HTML. Something like:

$first = 1;
$json = '[';
while ($row = mysql_fetch_assoc($result)) {
    if ($first == 1) $first = 0; else $json = $json + ',';
    $json = $json.'{';
    $json = $json.'"AgencyName":'.$row['agency_name'];
    $json = $json.', "AccountNumber":'.$row[account_number];
    $json = $json.', "Phone":'.$row[phone];
    $json = $json.'}';
}
$json = $json.']';

I have done this more in MVC and am rusty on PHP syntax, but hopefully this gets the gist across. Then your javascript can parse the results as just JSON. If you have "bad" characters in your data, you may have to html encode some, but the data should be easier to work with as data. You will have to provide template HTML for your modal and then fill it in.

于 2013-04-25T22:38:28.627 回答
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]”中提取一个对象。这就是为什么您只会返回一个对象。

编辑:您需要更改:

$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中:

编辑:然后在js中添加你的html,如果你想这样做的话

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 回答