jEditable选择数据源

时间:2012-08-28 04:55:59

标签: php jquery mysql jeditable

我正在尝试使用jEditable选择更新字段。我从我的数据库中提取jEditable select的数据。查询数组如下所示:

数组([0] =>数组([conference_id] => 1 [conference_name] => Allegheny Mountain)等......

我想在下拉列表中显示会议名称,但使用会议ID作为插入数据库的值。目前,我得到的只是一个空白的选择值列表。有没有人对我如何做到这一点做任何想法?

以下是我的代码:

<?php
try {
    // Connect to the database
    $db = new PDO('mysql:host=localhost;dbname=football;', 'fbdbuser', 'fbdbpass');
} catch(PDOException $e) {
    echo $e->getMessage();
}
try {
    // Retrieve all team information
    $query = $db->prepare("SELECT * FROM team ORDER BY team_name");
    $query->execute();
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    // Retrieve all conferences
    $query_conference = $db->prepare("SELECT conference_id, conference_name FROM conference ORDER BY conference_name");
    $query_conference->execute();
    $result_conference = $query_conference->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script src="js/jquery.js"></script>
    <script src="js/jeditable.js"></script>
    <script>
    $(document).ready(function() {
        $(".editable_conference").editable('update_conference.php', {
            data        : '<?php echo json_encode($result_conference); ?>',
            type        : 'select',
            indicator   : 'Saving...', 
            submit      : "OK", 
            tooltip     : "Click to Update" 
        });
    });
    </script>
    <title></title>
</head>
<body>
<h1 align="center">Update Teams Below</h1>
<?php //echo '<pre>' . print_r($result_conference) . '</pre>'; ?>
<table align="center" width="90%" border="1" cellpadding="5" cellspacing="0">
<tr bgcolor="#CCCCCC">
    <td>TEAM</td>
    <td>MASCOT</td>
    <td>STATE</td>
    <td>CONFERENCE</td>
    <td>DISTRICT</td>
    <td>CLASS</td>
</tr>
<?php foreach ($result as $value) { ?>
    <tr>
        <td><?php echo $value['team_name']; ?></td>
        <td class="editable_mascot" id="<?php echo $value['team_id']; ?>"><?php echo $value['team_conference']; ?></td>
        <td class="editable_state" id="<?php echo $value['team_id']; ?>"><?php echo $value['team_mascot']; ?></td>
        <td class="editable_conference" id="<?php echo $value['team_id']; ?>"><?php echo $value['team_state']; ?></td>
        <td class="editable_district" id="<?php echo $value['team_id']; ?>"><?php echo $value['team_district']; ?></td>
        <td class="editable_classification" id="<?php echo $value['team_id']; ?>"><?php echo $value['team_classification']; ?></td>
    </tr>
<?php } ?>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您应该了解jeditable的现象。

您使用的是<?php echo json_encode($result_conference); ?>,此变量是多维数组。您可以将其设为一维并将其传递给工作正常的数据。

$result_conference = $query_conference->fetchAll(PDO::FETCH_ASSOC);
$conf = array();
foreach($result_conference as $val){
    $conf[$val['conference_id']] = $val['conference_name'];
}

使用$conf变量代替$result_conference