1

我正在再次研究这个主题,但我还没有找到任何解决方案

一种。我所做的操作:我使用 PDO 构建查询以使用 PHP 从 DB 获取信息,然后将结果添加到关联数组中并对其进行编码,以便使用 ajax 发送它们。最后,我使用 $.ajax 来获取信息并构建 jquery collapsible

湾。问题:信息未附加到可折叠的结构内。ajax接收到的数据的console.log没有显示任何信息

数据库应用

表:错误 ID、类型、状态、严重性、系统、浏览器、标题、描述、创建日期

表:项目 ID、项目名称、状态、描述、开始日期、到期日期

该应用程序的结构是:

  • 公司/errorList.php
  • 公司/连接.php
  • index.html(多页文件)

文件:errorList.php

<?php
    // Import the connection to the database  
    require_once 'connection.php';

    // Get the instance
    $conn = dbConnect();

    // Build the query 
    $sql = " SELECT bg.bug_id, py.project, bg.type, bg.status, bg.severity, bg.system, bg.browser, bg.title, bg.description,bg.creation_date FROM bugs bg, projects py WHERE bg.projectid = py.id ";

    // Run the query and gets the results
    $result = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);

    // Build an iteration and create an associative array   
    foreach ($result as $row){

    $return[]=array(
                'bug_id'=>$row['bug_id'],
                'project'=>$row['project'],
                'type'=>$row['type'],
                'status'=>$row['status'],
                'severity'=>$row['severity'],
                'system'=>$row['system'],
                'browser'=>$row['browser'],
                'title'=>$row['title'],
                'description' => $row['description'],
                'creation_date'=>$row['creation_date']);
     }

     // Encode the array 
     echo json_encode($return);

?>

文件:index.html / 页:#bugs

在 html 文件中,我添加了 div "SET"

<div data-role="collapsible-set" id="set"><div>
    <script type="text/javascript">

        $(document).on('pagebeforeshow', '#bugs', function (){

                $.ajax({
                    url: 'inc/errorList.php',
                    data: "",
                    dataType: 'json',
                    success: function(data)
                    {
                        // Create the variable content to concatenate the results
                        var content = "";

                        // Build an iteration of the information received in data

                        for (var i = 0; i < data.length; i++) {

                            // Add the header of the collapsible  
                            content = "<div data-role='collapsible' data-collapsed-icon='arrow-r'   data-expanded-icon='arrow-d' data-iconpos='right' data-theme='a'><h3> " + data[i].bug_id + ' - ' + data[i].title + "</h3>" + "<ul data-role='listview' data-inset='true'  data-theme='a'>";

                            // Concatenate the header with the information retreived
                             content = content +
                            '<li>Project: ' + data[i].project + '</li>' +
                            '<li>Type: ' + data[i].type + '</li>' +
                            '<li>Status: ' + data[i].status + '</li>' +
                            '<li>Severity: ' + data[i].severity + '</li>' +
                            '<li>Browser: ' + data[i].browser + '</li>' +
                            '<li>Creation Date ' + data[i].creation_date + '</li>' +
                            '<li>Detail: ' + data[i].description + '</li>' ;

                            content = content + '</ul>';
                            content = content + "</div>";

                            // Append the content into the div     
                            $("#set").append(content);
                        }

                        // Refresh the Collapsible 
                        $("#set").collapsibleset("refresh").enhanceWithin();
                       }
                    });
            });
    </script>  
4

1 回答 1

0

是的,我终于找到了这个错误!问题是“描述”字段中某些单词的重音符号,下面我添加最终代码:

文件:errorList.php

<?php
// Import the connection to the database  
require_once 'connection.php';

// Get the instance
$conn = dbConnect();

// Build the query 
$sql = " SELECT bg.bug_id, py.project, bg.type, bg.status, bg.severity, bg.system, bg.browser, bg.title, bg.description,bg.creation_date FROM bugs bg, projects py WHERE bg.projectid = py.id ";

// Run the query and gets the results
$result = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);

// Build an iteration and create an associative array   
foreach ($result as $row){

$return[]=array(
            'bug_id'=>$row['bug_id'],
            'project'=>$row['project'],
            'type'=>$row['type'],
            'status'=>$row['status'],
            'severity'=>$row['severity'],
            'system'=>$row['system'],
            'browser'=>$row['browser'],
            'title'=>$row['title'],
            'description' => $row['description'],
            'creation_date'=>$row['creation_date']);
 }

header("Content-type: application/json", true);
echo json_encode($return);
exit;

?>

文件:json.js

address = 'inc/errorList.php';


$.ajax({url: address,
        cache: false,
        data: "",
        dataType: 'json',
        success: function(new_data){

  // Create the variable content to concatenate the results
                    var content = "";

                    // Build an iteration of the information received in data

                    for (var i = 0; i < data.length; i++) {

                        // Add the header of the collapsible  
                        content = "<div data-role='collapsible' data-collapsed-icon='arrow-r'   data-expanded-icon='arrow-d' data-iconpos='right' data-theme='a'><h3> " + data[i].bug_id + ' - ' + data[i].title + "</h3>" + "<ul data-role='listview' data-inset='true'  data-theme='a'>";

                        // Concatenate the header with the information retreived
                         content = content +
                        '<li>Project: ' + data[i].project + '</li>' +
                        '<li>Type: ' + data[i].type + '</li>' +
                        '<li>Status: ' + data[i].status + '</li>' +
                        '<li>Severity: ' + data[i].severity + '</li>' +
                        '<li>Browser: ' + data[i].browser + '</li>' +
                        '<li>Creation Date ' + data[i].creation_date + '</li>' +
                        '<li>Detail: ' + data[i].description + '</li>' ;

                        content = content + '</ul>';
                        content = content + "</div>";

                        // Append the content into the div     
                        $("#set").append(content);
                    }

                    // Refresh the Collapsible 
                    $("#set").collapsibleset("refresh").enhanceWithin();
                   },
        error: function (request,error) {

            //var err = eval("(" + xhr.responseText + ")");
            //alert(err.Message);
            // This callback function will trigger on unsuccessful action                
            alert('Connection Error!');
            alert('Error: ' + error);
            //console.log(new_data);

        }

    });           

 });
于 2014-11-14T04:02:54.693 回答