jEditable Select Data Source

2019-08-22 01:05发布

问题:

I am trying to update a field using a jEditable select. I am pulling the data for the jEditable select from my database. The query array looks like this:

Array ([0] => Array([conference_id] => 1 [conference_name] => Allegheny Mountain) etc…

I want to show the conference name in the drop down but use the conference id as the value that gets inserted into the database. Currently all that I am getting is a blank list of select values. Does anybody have any ideas on how I can get this to do what I want?

Here is my code that I have below:

<?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:

You should understand the phenomenon of jeditable.

You are using <?php echo json_encode($result_conference); ?> and this variable is multi-dimesional array. You can make it one dimensional and pass it data that works fine.

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

use $conf variable to instead of $result_conference